Class RsaEncDataContentBuilder
java.lang.Object
zeroecho.sdk.builders.alg.RsaEncDataContentBuilder
- All Implemented Interfaces:
DataContentBuilder<DataContent>
public final class RsaEncDataContentBuilder
extends Object
implements DataContentBuilder<DataContent>
RsaEncDataContentBuilder builds streaming RSA encryption or decryption
pipelines that operate while an InputStream is consumed.
Overview
This builder producesDataContent backed by an
EncryptionContext created from
CryptoAlgorithms. In encrypt mode it transforms upstream plaintext
into ciphertext; in decrypt mode it transforms ciphertext back to plaintext.
Padding and OAEP parameters are configured via RsaEncSpec.
Key material
Keys can be supplied directly, generated fromRsaKeyGenSpec, or imported from standard encodings: PKCS#8 for
private keys and X.509 SubjectPublicKeyInfo for public keys. If both
generation and import are requested, the last configuration applied before
build(boolean) takes effect.
Examples
// Encrypt with OAEP-SHA-256 and a generated 2048-bit key pair
DataContent enc = RsaEncDataContentBuilder.builder()
.oaepSha256()
.generateKeyPair(RsaKeyGenSpec.rsa2048())
.build(true);
// Decrypt with an imported PKCS#8 private key
DataContent dec = RsaEncDataContentBuilder.builder()
.importPrivatePkcs8(pkcs8Bytes)
.build(false);
// Encrypt with OAEP-SHA-512 and an application label
DataContent enc2 = RsaEncDataContentBuilder.builder()
.oaep(RsaEncSpec.Hash.SHA512)
.oaepLabelUtf8("session-1")
.withPublicKey(pub)
.build(true);
Thread-safety
Instances are mutable and not thread-safe. Configure and use each builder instance from a single thread.- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionbuild(boolean encrypt) Builds a streaming encryption or decryption pipeline according to the current configuration.static RsaEncDataContentBuilderbuilder(ZeroEchoSession session) Creates a new builder for constructing RSA encryption or decryption pipelines.generateKeyPair(int bits, int publicExponent) Requests generation of a fresh RSA key pair using the given modulus size and public exponent.generateKeyPair(RsaKeyGenSpec spec) Requests generation of a fresh RSA key pair with the provided key generation specification.importPrivatePkcs8(byte[] pkcs8) Imports a private key from PKCS#8-encoded bytes.importPublicX509(byte[] x509) Imports a public key from X.509 SubjectPublicKeyInfo bytes.Selects RSA-OAEP with the given hash algorithm for MGF1 and hash.oaepLabel(byte[] label) Sets the OAEP label as arbitrary bytes.Sets the OAEP label from a UTF-8 string.Selects RSA-OAEP with SHA-256 for MGF1 and hash.pkcs1v15()Selects PKCS#1 v1.5 padding for RSA.spec(RsaEncSpec s) Sets the RSA encryption specification, including padding and OAEP parameters.Supplies the private key to use for decryption.Supplies the public key to use for encryption.
-
Method Details
-
builder
Creates a new builder for constructing RSA encryption or decryption pipelines.RsaEncDataContentBuilder b = RsaEncDataContentBuilder.builder();- Parameters:
session- active session used to create cryptographic contexts- Returns:
- a new
RsaEncDataContentBuilderinstance
-
spec
Sets the RSA encryption specification, including padding and OAEP parameters.- Parameters:
s- the RSA encryption spec; must not be null- Returns:
thisbuilder for chaining- Throws:
NullPointerException- ifsis null
-
oaepSha256
Selects RSA-OAEP with SHA-256 for MGF1 and hash.- Returns:
thisbuilder for chaining
-
oaep
Selects RSA-OAEP with the given hash algorithm for MGF1 and hash.- Parameters:
h- the OAEP hash function- Returns:
thisbuilder for chaining- Throws:
NullPointerException- ifhis null
-
oaepLabel
Sets the OAEP label as arbitrary bytes.The label is included in the OAEP parameter set. It is not encrypted and must be supplied identically during decryption.
- Parameters:
label- the OAEP label bytes; may be null to clear- Returns:
thisbuilder for chaining
-
oaepLabelUtf8
Sets the OAEP label from a UTF-8 string.- Parameters:
s- the label text; may be null to clear- Returns:
thisbuilder for chaining
-
pkcs1v15
Selects PKCS#1 v1.5 padding for RSA.- Returns:
thisbuilder for chaining
-
withPublicKey
Supplies the public key to use for encryption.- Parameters:
k- the public key; must not be null- Returns:
thisbuilder for chaining- Throws:
NullPointerException- ifkis null
-
withPrivateKey
Supplies the private key to use for decryption.- Parameters:
k- the private key; must not be null- Returns:
thisbuilder for chaining- Throws:
NullPointerException- ifkis null
-
generateKeyPair
Requests generation of a fresh RSA key pair with the provided key generation specification.- Parameters:
spec- the RSA key generation spec; must not be null- Returns:
thisbuilder for chaining- Throws:
NullPointerException- ifspecis null
-
generateKeyPair
Requests generation of a fresh RSA key pair using the given modulus size and public exponent.- Parameters:
bits- modulus size in bitspublicExponent- public exponent (for example 65537)- Returns:
thisbuilder for chaining
-
importPrivatePkcs8
Imports a private key from PKCS#8-encoded bytes.- Parameters:
pkcs8- PKCS#8 PrivateKeyInfo bytes; must not be null- Returns:
thisbuilder for chaining- Throws:
NullPointerException- ifpkcs8is null
-
importPublicX509
Imports a public key from X.509 SubjectPublicKeyInfo bytes.- Parameters:
x509- X.509 public key bytes; must not be null- Returns:
thisbuilder for chaining- Throws:
NullPointerException- ifx509is null
-
build
Builds a streaming encryption or decryption pipeline according to the current configuration.When
encryptis true, a public key must be available from prior configuration, generation, or import. Whenencryptis false, a private key must be available.DataContent enc = RsaEncDataContentBuilder.builder() .oaepSha256() .generateKeyPair(2048, 65537) .build(true);- Specified by:
buildin interfaceDataContentBuilder<DataContent>- Parameters:
encrypt-trueto build an encrypting pipeline,falsefor a decrypting pipeline- Returns:
- a
DataContentthat performs the requested operation when its stream is consumed - Throws:
IllegalStateException- if a required key is missing or if key setup fails
-