Class ElgamalCipherContext
- All Implemented Interfaces:
Closeable, AutoCloseable, CryptoContext, EncryptionContext
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
0x00byte 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 Summary
ConstructorsConstructorDescriptionElgamalCipherContext(CryptoAlgorithm algorithm, Key key, ElgamalEncSpec spec, boolean encrypt) Creates a new ElGamal cipher context. -
Method Summary
Modifier and TypeMethodDescriptionReturns the associated algorithm object for metadata and auditing.attach(InputStream upstream) Returns an input stream that transforms bytes on-the-fly using ElGamal.voidclose()Closes this context.key()Returns the key bound to this context.
-
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"forElgamalEncSpec.Padding.NOPADDINGand"ElGamal/None/PKCS1Padding"forElgamalEncSpec.Padding.PKCS1. The JCE provider must support the chosen transformation and the providedkeyfor the requested direction.- Parameters:
algorithm- the owning algorithm for metadata; must not benullkey- public key for encrypt or private key for decrypt; must not benullspec- ElGamal encoding/padding specification; must not benullencrypt-truefor encryption,falsefor decryption- Throws:
NullPointerException- if any argument isnull
-
-
Method Details
-
attach
Returns an input stream that transforms bytes on-the-fly using ElGamal.A fresh
Cipheris 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:
attachin interfaceEncryptionContext- Parameters:
upstream- the upstream source to transform; must not benull- Returns:
- a transforming input stream
- Throws:
IOException- if the cipher cannot be created or initializedNullPointerException- ifupstreamisnull
-
algorithm
Returns the associated algorithm object for metadata and auditing.- Specified by:
algorithmin interfaceCryptoContext- Returns:
- the algorithm that created this context
-
key
Returns the key bound to this context.- Specified by:
keyin interfaceCryptoContext- Returns:
- the key used for encryption or decryption
-
close
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
CryptoContextcontract and may be used by higher layers for lifecycle hooks.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceCryptoContext- Throws:
IOException- if the underlying provider encounters an I/O error during cleanup
-