Class ElgamalCipherContext

java.lang.Object
zeroecho.core.alg.elgamal.ElgamalCipherContext
All Implemented Interfaces:
Closeable, AutoCloseable, CryptoContext, EncryptionContext

public final class ElgamalCipherContext extends Object implements EncryptionContext
Streaming ElGamal cipher context that adapts an upstream stream into an encrypted or decrypted stream.

The context applies ElGamal block-by-block over a pull pipeline. For a modulus of size pBytes, each ciphertext block is fixed at 2 * pBytes bytes (two group elements), while the plaintext-per-block depends on the padding:

  • NOPADDING: ptBlock = pBytes - 1
  • PKCS1: ptBlock = pBytes - 11

Padding subtleties

  • NOPADDING: some providers encode integers with a leading 0x00 byte for positive sign. This implementation trims a leading sign byte and enforces a fixed plaintext length for non-last blocks only. The last block is emitted as-is.
  • PKCS1: the last plaintext block may be shorter by design; it is never padded on output.

EOF rules

  • Encrypt: a short final plaintext block is allowed and produces a single ciphertext block.
  • Decrypt: ciphertext must be an integral number of full ciphertext blocks; a partial tail is rejected.

Thread-safety

Instances are not thread-safe. Create one context per pipeline.
  • Constructor Details

    • ElgamalCipherContext

      public ElgamalCipherContext(CryptoAlgorithm algorithm, Key key, ElgamalEncSpec spec, boolean encrypt)
      Creates a new ElGamal cipher context.

      The transformation used is "ElGamal/None/NOPADDING" for ElgamalEncSpec.Padding.NOPADDING and "ElGamal/None/PKCS1Padding" for ElgamalEncSpec.Padding.PKCS1. The JCE provider must support the chosen transformation and the provided key for the requested direction.

      Parameters:
      algorithm - the owning algorithm for metadata; must not be null
      key - public key for encrypt or private key for decrypt; must not be null
      spec - ElGamal encoding/padding specification; must not be null
      encrypt - true for encryption, false for decryption
      Throws:
      NullPointerException - if any argument is null
  • Method Details

    • attach

      public InputStream attach(InputStream upstream) throws IOException
      Returns an input stream that transforms bytes on-the-fly using ElGamal.

      A fresh Cipher is created and initialized per call. The returned stream implements the block model and EOF rules described in the class documentation. Callers must close the returned stream to finalize the transformation and release resources.

      Specified by:
      attach in interface EncryptionContext
      Parameters:
      upstream - the upstream source to transform; must not be null
      Returns:
      a transforming input stream
      Throws:
      IOException - if the cipher cannot be created or initialized
      NullPointerException - if upstream is null
    • algorithm

      public CryptoAlgorithm algorithm()
      Returns the associated algorithm object for metadata and auditing.
      Specified by:
      algorithm in interface CryptoContext
      Returns:
      the algorithm that created this context
    • key

      public Key key()
      Returns the key bound to this context.
      Specified by:
      key in interface CryptoContext
      Returns:
      the key used for encryption or decryption
    • close

      public void close() throws IOException
      Closes this context.

      The returned transforming streams are independent of the context object, so closing the context has no effect on previously created streams. This method is provided to satisfy the CryptoContext contract and may be used by higher layers for lifecycle hooks.

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface CryptoContext
      Throws:
      IOException - if the underlying provider encounters an I/O error during cleanup