Class RsaCipherContext
- All Implemented Interfaces:
Closeable, AutoCloseable, CryptoContext, EncryptionContext
An instance is created for a specific role (encrypt or decrypt) and a
concrete RsaEncSpec. The context exposes a pull-style pipeline via
attach(java.io.InputStream) that converts bytes on-the-fly, making
it suitable for large streams without buffering the whole message in memory.
Behavior
- Encryption reads plaintext in RSA-size blocks according to the selected padding, producing fixed-size ciphertext blocks equal to the modulus length.
- Decryption requires ciphertext in full modulus-length blocks and yields variable-size plaintext blocks determined by padding.
- Finalization occurs on end of stream; decryption rejects trailing partial ciphertext blocks.
Example
RsaEncSpec spec = RsaEncSpec.oaep(RsaEncSpec.Hash.SHA256);
try (RsaCipherContext ctx = new RsaCipherContext(algo, pubKey, true, spec);
InputStream in = Files.newInputStream(plaintextPath);
InputStream enc = ctx.attach(in)) {
Files.copy(enc, ciphertextPath);
}
Security note: OAEP with SHA-256 is preferred over PKCS#1 v1.5. Decryption enforces full ciphertext blocks to avoid truncation hazards. The context does not manage or destroy keys.
-
Constructor Summary
ConstructorsConstructorDescriptionRsaCipherContext(CryptoAlgorithm algorithm, Key key, boolean encrypt, RsaEncSpec spec) Creates a new RSA cipher context for encryption or decryption with the given parameters. -
Method Summary
Modifier and TypeMethodDescriptionReturns the algorithm descriptor associated with this context.attach(InputStream upstream) Attaches this cipher to an upstream stream and returns a transforming stream.voidclose()Closes the context and releases resources.key()Returns the key bound to this context.
-
Constructor Details
-
RsaCipherContext
Creates a new RSA cipher context for encryption or decryption with the given parameters.The
encryptflag selects the role of this context:truefor encryption (public key is typical),falsefor decryption (private key is required). Thespecdefines padding and hash parameters; for OAEP the hash is used both for the digest and MGF1 unless specified otherwise by the spec.- Parameters:
algorithm- the owning algorithm descriptor used for metadata and auditingkey- the RSA key for this role; public for encryption/verify, private for decrypt/signencrypt-trueto encrypt,falseto decryptspec- RSA encryption specification including padding mode and hash- Throws:
NullPointerException- if any argument isnull
-
-
Method Details
-
algorithm
Returns the algorithm descriptor associated with this context.- 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 RSA key used by this context
-
close
public void close()Closes the context and releases resources.This implementation is a no-op because the attached stream performs finalize-on-close.
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceCryptoContext
-
attach
Attaches this cipher to an upstream stream and returns a transforming stream.The returned stream pulls from
upstreamand emits encrypted or decrypted bytes according to the constructor-selected role andRsaEncSpec. On EOF, encryption will process a final short plaintext block if present, while decryption requires that the final buffered ciphertext forms a complete modulus-sized block.The caller is responsible for closing the returned stream to ensure finalization and resource cleanup.
- Specified by:
attachin interfaceEncryptionContext- Parameters:
upstream- the source stream to transform- Returns:
- a new input stream that produces transformed bytes
- Throws:
NullPointerException- ifupstreamisnullIOException- if the cipher cannot be initialized or a block transform fails
-