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 creates DataContent 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:

Thread-safety

Instances are mutable and not thread-safe. Configure and use each builder instance from a single thread.
See Also:
  • Method Details

    • builder

      public static ElgamalEncDataContentBuilder builder(ZeroEchoSession session)
      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 ElgamalEncDataContentBuilder instance
    • spec

      Sets the encryption specification such as padding mode.
      Parameters:
      s - the ElGamal encryption spec to use; must not be null
      Returns:
      this builder for chaining
      Throws:
      NullPointerException - if s is null
    • noPadding

      public ElgamalEncDataContentBuilder 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:
      this builder for chaining
    • pkcs1

      Selects the PKCS#1-style padding variant of ElGamal encryption.
      Returns:
      this builder for chaining
    • withPublicKey

      public ElgamalEncDataContentBuilder withPublicKey(PublicKey k)
      Supplies the public key to use for encryption.
      Parameters:
      k - the public key; must not be null
      Returns:
      this builder for chaining
      Throws:
      NullPointerException - if k is null
    • withPrivateKey

      public ElgamalEncDataContentBuilder withPrivateKey(PrivateKey k)
      Supplies the private key to use for decryption.
      Parameters:
      k - the private key; must not be null
      Returns:
      this builder for chaining
      Throws:
      NullPointerException - if k is null
    • generateKeyPair

      public ElgamalEncDataContentBuilder generateKeyPair(ElgamalKeyGenSpec spec)
      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:
      this builder for chaining
      Throws:
      NullPointerException - if spec is null
    • generateKeyPair

      public ElgamalEncDataContentBuilder generateKeyPair(int bits)
      Requests generation of a fresh ElGamal key pair with the specified modulus length and default certainty.
      Parameters:
      bits - modulus size in bits
      Returns:
      this builder for chaining
    • generateKeyPair

      public ElgamalEncDataContentBuilder generateKeyPair(ElgamalParamSpec spec)
      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:
      this builder for chaining
      Throws:
      NullPointerException - if spec is null
    • importPrivatePkcs8

      public ElgamalEncDataContentBuilder importPrivatePkcs8(byte[] pkcs8)
      Imports a private key from PKCS#8-encoded bytes.
      Parameters:
      pkcs8 - PKCS#8 PrivateKeyInfo bytes; must not be null
      Returns:
      this builder for chaining
      Throws:
      NullPointerException - if pkcs8 is null
    • importPublicX509

      public ElgamalEncDataContentBuilder importPublicX509(byte[] x509)
      Imports a public key from X.509 SubjectPublicKeyInfo bytes.
      Parameters:
      x509 - X.509 public key bytes; must not be null
      Returns:
      this builder for chaining
      Throws:
      NullPointerException - if x509 is null
    • build

      public DataContent build(boolean encrypt)
      Builds a streaming encryption or decryption pipeline according to the current configuration.

      When encrypt is true, a public key must be available either from prior configuration, generation, or import. When encrypt is 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:
      build in interface DataContentBuilder<DataContent>
      Parameters:
      encrypt - true to build an encrypting pipeline, false for a decrypting pipeline
      Returns:
      a DataContent that performs the requested operation when its stream is consumed
      Throws:
      IllegalStateException - if a required key is missing or if key setup fails