Class JcePGPDataEncryptorBuilder

java.lang.Object
org.bouncycastle.openpgp.operator.jcajce.JcePGPDataEncryptorBuilder
All Implemented Interfaces:
PGPDataEncryptorBuilder

public class JcePGPDataEncryptorBuilder extends Object implements PGPDataEncryptorBuilder
PGPDataEncryptorBuilder implementation that sources cryptographic primitives using the JCE APIs.

By default, cryptographic primitives will be loaded using the default JCE load order (i.e. without specifying a provider). A specific provider can be specified using one of the setProvider(String) methods.

  • Constructor Details

  • Method Details

    • setWithIntegrityPacket

      public JcePGPDataEncryptorBuilder setWithIntegrityPacket(boolean withIntegrityPacket)
      Sets whether or not the resulting encrypted data will be protected using an integrity packet.
      Specified by:
      setWithIntegrityPacket in interface PGPDataEncryptorBuilder
      Parameters:
      withIntegrityPacket - true if an integrity packet is to be included, false otherwise.
      Returns:
      the current builder.
    • setWithAEAD

      public JcePGPDataEncryptorBuilder setWithAEAD(int aeadAlgorithm, int chunkSize)
      Description copied from interface: PGPDataEncryptorBuilder
      Sets whether the resulting encrypted data will be protected using an AEAD mode. The chunkSize is used as a power of two, result in blocks (1 << chunkSize) containing data with an extra 16 bytes for the tag. The minimum chunkSize is 6.
      Specified by:
      setWithAEAD in interface PGPDataEncryptorBuilder
      Parameters:
      aeadAlgorithm - the AEAD mode to use.
      chunkSize - the size of the chunks to be processed with each nonce.
      Returns:
      the current builder
    • setUseV5AEAD

      public JcePGPDataEncryptorBuilder setUseV5AEAD()
      Description copied from interface: PGPDataEncryptorBuilder
      Specify we are using V5 AEAD.
      Specified by:
      setUseV5AEAD in interface PGPDataEncryptorBuilder
      Returns:
      the current builder
    • setUseV6AEAD

      public JcePGPDataEncryptorBuilder setUseV6AEAD()
      Description copied from interface: PGPDataEncryptorBuilder
      Specify we are using V6 AEAD.
      Specified by:
      setUseV6AEAD in interface PGPDataEncryptorBuilder
      Returns:
      the current builder
    • setProvider

      public JcePGPDataEncryptorBuilder setProvider(Provider provider)
      Sets the JCE provider to source cryptographic primitives from.
      Parameters:
      provider - the JCE provider to use.
      Returns:
      the current builder.
    • setProvider

      public JcePGPDataEncryptorBuilder setProvider(String providerName)
      Sets the JCE provider to source cryptographic primitives from.
      Parameters:
      providerName - the name of the JCE provider to use.
      Returns:
      the current builder.
    • setSecureRandom

      public JcePGPDataEncryptorBuilder setSecureRandom(SecureRandom random)
      Provide a user defined source of randomness.

      If no SecureRandom is configured, a default SecureRandom will be used.

      Parameters:
      random - the secure random to be used.
      Returns:
      the current builder.
    • getAlgorithm

      public int getAlgorithm()
      Description copied from interface: PGPDataEncryptorBuilder
      The encryption algorithm used by data encryptors created by this builder.
      Specified by:
      getAlgorithm in interface PGPDataEncryptorBuilder
      Returns:
      one of the symmetric encryption algorithms.
    • getAeadAlgorithm

      public int getAeadAlgorithm()
      Specified by:
      getAeadAlgorithm in interface PGPDataEncryptorBuilder
    • getChunkSize

      public int getChunkSize()
      Specified by:
      getChunkSize in interface PGPDataEncryptorBuilder
    • isV5StyleAEAD

      public boolean isV5StyleAEAD()
      Specified by:
      isV5StyleAEAD in interface PGPDataEncryptorBuilder
    • getSecureRandom

      public SecureRandom getSecureRandom()
      Description copied from interface: PGPDataEncryptorBuilder
      Gets the SecureRandom instance used by this builder.

      If a SecureRandom has not been explicitly configured, a default SecureRandom is constructed and retained by the this builder.

      Specified by:
      getSecureRandom in interface PGPDataEncryptorBuilder
    • build

      public PGPDataEncryptor build(byte[] keyBytes) throws PGPException
      Description copied from interface: PGPDataEncryptorBuilder
      Builds a data encryptor using the algorithm configured for this builder.
      Specified by:
      build in interface PGPDataEncryptorBuilder
      Parameters:
      keyBytes - the bytes of the key to use for the cipher.
      Returns:
      a data encryptor with an initialised cipher.
      Throws:
      PGPException - if an error occurs initialising the configured encryption.
    • build

      public PGPDataEncryptor build(byte[] key, byte[] salt) throws PGPException
      Description copied from interface: PGPDataEncryptorBuilder
      Builds a data encryptor for OpenPGP v2 SEIPD (AEAD), deriving the message key and IV from the supplied session key and salt per RFC 9580 sec. 5.13.2, using this builder's crypto stack. Only meaningful when the builder is configured for v2 SEIPD (v6) AEAD; the derivation is owned here rather than by the caller so it runs through the same provider as the cipher.
      Specified by:
      build in interface PGPDataEncryptorBuilder
      Parameters:
      key - the session key.
      salt - the 32-octet salt carried in the v2 SEIPD packet.
      Returns:
      a data encryptor with an initialised AEAD cipher.
      Throws:
      PGPException - if an error occurs deriving the message key or initialising the cipher.