Class ElgamalEncDataContentBuilder
java.lang.Object
zeroecho.sdk.builders.alg.ElgamalEncDataContentBuilder
- All Implemented Interfaces:
DataContentBuilder<DataContent>
public final class ElgamalEncDataContentBuilder
extends Object
implements DataContentBuilder<DataContent>
ElgamalEncDataContentBuilder builds streaming ElGamal encryption or
decryption pipelines that operate as an InputStream is consumed.
Overview
This builder createsDataContent for ElGamal public key
encryption. In encrypt mode it returns content that transforms upstream
plaintext into ciphertext using a
EncryptionContext; in decrypt mode it produces
plaintext from ciphertext. Keys may be supplied, generated, or imported from
standard encodings. Padding mode is selected via ElgamalEncSpec.
Typical usage
// Encrypt with a generated 2048-bit key pair using PKCS#1-style padding:
DataContent enc = ElgamalEncDataContentBuilder.builder()
.generateKeyPair(ElgamalKeyGenSpec.elgamal2048())
.pkcs1()
.build(true);
// Decrypt with an imported private key (PKCS#8) and no padding:
DataContent dec = ElgamalEncDataContentBuilder.builder()
.noPadding()
.importPrivatePkcs8(pkcs8Bytes)
.build(false);
// Encrypt with an existing public key and parameter preset:
DataContent enc2 = ElgamalEncDataContentBuilder.builder()
.withPublicKey(pub)
.spec(ElgamalEncSpec.pkcs1())
.build(true);
Key material
You can:- Provide keys directly via
withPublicKey(PublicKey)orwithPrivateKey(PrivateKey). - Generate a pair using a size-based
ElgamalKeyGenSpecor a presetElgamalParamSpec. - Import from encodings with
importPublicX509(byte[])andimportPrivatePkcs8(byte[]).
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 ElgamalEncDataContentBuilderbuilder(ZeroEchoSession session) Creates a new builder for constructing ElGamal encryption or decryption pipelines.generateKeyPair(int bits) Requests generation of a fresh ElGamal key pair with the specified modulus length and default certainty.Requests generation of a fresh ElGamal key pair using the provided key generation specification.Requests generation of a fresh ElGamal key pair using a predefined parameter set.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 the no-padding variant of ElGamal encryption.pkcs1()Selects the PKCS#1-style padding variant of ElGamal encryption.Sets the encryption specification such as padding mode.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 ElGamal encryption or decryption pipelines.Example
ElgamalEncDataContentBuilder b = ElgamalEncDataContentBuilder.builder();- Parameters:
session- active session used to create cryptographic contexts- Returns:
- a new
ElgamalEncDataContentBuilderinstance
-
spec
Sets the encryption specification such as padding mode.- Parameters:
s- the ElGamal encryption spec to use; must not be null- Returns:
thisbuilder for chaining- Throws:
NullPointerException- ifsis null
-
noPadding
Selects the no-padding variant of ElGamal encryption.Use with caution. No padding may be appropriate only for specific protocols that add their own randomness and structure.
- Returns:
thisbuilder for chaining
-
pkcs1
Selects the PKCS#1-style padding variant of ElGamal encryption.- 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 ElGamal key pair using the provided key generation specification.This method clears any preset-parameter request. The generated keys will be used for subsequent
build(boolean)calls unless overridden by imports or explicit keys.- Parameters:
spec- the key generation spec; must not be null- Returns:
thisbuilder for chaining- Throws:
NullPointerException- ifspecis null
-
generateKeyPair
Requests generation of a fresh ElGamal key pair with the specified modulus length and default certainty.- Parameters:
bits- modulus size in bits- Returns:
thisbuilder for chaining
-
generateKeyPair
Requests generation of a fresh ElGamal key pair using a predefined parameter set.This method clears any size-based generation request. The generated keys will be used for subsequent
build(boolean)calls unless overridden.- Parameters:
spec- the predefined parameter set; must not be null- Returns:
thisbuilder for chaining- Throws:
NullPointerException- ifspecis null
-
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 either from prior configuration, generation, or import. Whenencryptis false, a private key must be available.Key resolution order
During build, the builder may generate keys, import keys, or use explicitly provided keys based on the configuration. Generation by size and generation by predefined parameters are mutually exclusive; the last selected mode wins.Examples
DataContent enc = ElgamalEncDataContentBuilder.builder() .generateKeyPair(2048) .pkcs1() .build(true); DataContent dec = ElgamalEncDataContentBuilder.builder() .importPrivatePkcs8(pkcs8) .build(false);- 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
-