Class CipherUtil

java.lang.Object
org.cryptacular.util.CipherUtil

public final class CipherUtil extends Object
Utility class that performs encryption and decryption operations using a block cipher.
  • Field Details

    • MAC_SIZE_BITS

      private static final int MAC_SIZE_BITS
      Mac size in bits.
      See Also:
  • Constructor Details

    • CipherUtil

      private CipherUtil()
      Private constructor of utility class.
  • Method Details

    • encrypt

      public static byte[] encrypt(org.bouncycastle.crypto.modes.AEADBlockCipher cipher, SecretKey key, Nonce nonce, byte[] data) throws CryptoException
      Encrypts data using an AEAD cipher. A CiphertextHeader is prepended to the resulting ciphertext and used as AAD (Additional Authenticated Data) passed to the AEAD cipher.
      Parameters:
      cipher - AEAD cipher.
      key - Encryption key.
      nonce - Nonce generator.
      data - Plaintext data to be encrypted.
      Returns:
      Concatenation of encoded CiphertextHeader and encrypted data that completely fills the returned byte array.
      Throws:
      CryptoException - on encryption errors.
    • encrypt

      public static void encrypt(org.bouncycastle.crypto.modes.AEADBlockCipher cipher, SecretKey key, Nonce nonce, InputStream input, OutputStream output) throws CryptoException, StreamException
      Encrypts data using an AEAD cipher. A CiphertextHeader is prepended to the resulting ciphertext and used as AAD (Additional Authenticated Data) passed to the AEAD cipher.
      Parameters:
      cipher - AEAD cipher.
      key - Encryption key.
      nonce - Nonce generator.
      input - Input stream containing plaintext data.
      output - Output stream that receives a CiphertextHeader followed by ciphertext data produced by the AEAD cipher in encryption mode.
      Throws:
      CryptoException - on encryption errors.
      StreamException - on IO errors.
    • decrypt

      public static byte[] decrypt(org.bouncycastle.crypto.modes.AEADBlockCipher cipher, SecretKey key, byte[] data) throws CryptoException, EncodingException
      Decrypts data using an AEAD cipher.
      Parameters:
      cipher - AEAD cipher.
      key - Encryption key.
      data - Ciphertext data containing a prepended CiphertextHeader. The header is treated as AAD input to the cipher that is verified during decryption.
      Returns:
      Decrypted data that completely fills the returned byte array.
      Throws:
      CryptoException - on encryption errors.
      EncodingException - on decoding cyphertext header.
    • decrypt

      public static void decrypt(org.bouncycastle.crypto.modes.AEADBlockCipher cipher, SecretKey key, InputStream input, OutputStream output) throws CryptoException, EncodingException, StreamException
      Decrypts data using an AEAD cipher.
      Parameters:
      cipher - AEAD cipher.
      key - Encryption key.
      input - Input stream containing a CiphertextHeader followed by ciphertext data. The header is treated as AAD input to the cipher that is verified during decryption.
      output - Output stream that receives plaintext produced by block cipher in decryption mode.
      Throws:
      CryptoException - on encryption errors.
      EncodingException - on decoding cyphertext header.
      StreamException - on IO errors.
    • encrypt

      public static byte[] encrypt(org.bouncycastle.crypto.BlockCipher cipher, SecretKey key, Nonce nonce, byte[] data) throws CryptoException
      Encrypts data using the given block cipher with PKCS5 padding. A CiphertextHeader is prepended to the resulting ciphertext.
      Parameters:
      cipher - Block cipher.
      key - Encryption key.
      nonce - IV generator. Callers must take care to ensure that the length of generated IVs is equal to the cipher block size.
      data - Plaintext data to be encrypted.
      Returns:
      Concatenation of encoded CiphertextHeader and encrypted data that completely fills the returned byte array.
      Throws:
      CryptoException - on encryption errors.
    • encrypt

      public static void encrypt(org.bouncycastle.crypto.BlockCipher cipher, SecretKey key, Nonce nonce, InputStream input, OutputStream output) throws CryptoException, StreamException
      Encrypts data using the given block cipher with PKCS5 padding. A CiphertextHeader is prepended to the resulting ciphertext.
      Parameters:
      cipher - Block cipher.
      key - Encryption key.
      nonce - IV generator. Callers must take care to ensure that the length of generated IVs is equal to the cipher block size.
      input - Input stream containing plaintext data.
      output - Output stream that receives ciphertext produced by block cipher in encryption mode.
      Throws:
      CryptoException - on encryption errors.
      StreamException - on IO errors.
    • decrypt

      public static byte[] decrypt(org.bouncycastle.crypto.BlockCipher cipher, SecretKey key, byte[] data) throws CryptoException, EncodingException
      Decrypts data using the given block cipher with PKCS5 padding.
      Parameters:
      cipher - Block cipher.
      key - Encryption key.
      data - Ciphertext data containing a prepended CiphertextHeader.
      Returns:
      Decrypted data that completely fills the returned byte array.
      Throws:
      CryptoException - on encryption errors.
      EncodingException - on decoding cyphertext header.
    • decrypt

      public static void decrypt(org.bouncycastle.crypto.BlockCipher cipher, SecretKey key, InputStream input, OutputStream output) throws CryptoException, EncodingException, StreamException
      Decrypts data using the given block cipher with PKCS5 padding.
      Parameters:
      cipher - Block cipher.
      key - Encryption key.
      input - Input stream containing a CiphertextHeader followed by ciphertext data.
      output - Output stream that receives plaintext produced by block cipher in decryption mode.
      Throws:
      CryptoException - on encryption errors.
      EncodingException - on decoding cyphertext header.
      StreamException - on IO errors.
    • decodeHeader

      public static CiphertextHeader decodeHeader(byte[] data, Function<String,SecretKey> keyLookup)
      Decodes the ciphertext header at the start of the given byte array. Supports both original (deprecated) and v2 formats.
      Parameters:
      data - Ciphertext data with prepended header.
      keyLookup - Decryption key lookup function.
      Returns:
      Ciphertext header instance.
    • decodeHeader

      public static CiphertextHeader decodeHeader(InputStream in, Function<String,SecretKey> keyLookup)
      Decodes the ciphertext header at the start of the given input stream. Supports both original (deprecated) and v2 formats.
      Parameters:
      in - Ciphertext stream that is positioned at the start of the ciphertext header.
      keyLookup - Decryption key lookup function.
      Returns:
      Ciphertext header instance.
    • encrypt

      private static byte[] encrypt(BlockCipherAdapter cipher, byte[] header, byte[] data)
      Encrypts the given data.
      Parameters:
      cipher - Adapter for either a block or AEAD cipher.
      header - Encoded ciphertext header.
      data - Plaintext data to encrypt.
      Returns:
      Concatenation of encoded header and encrypted data that completely fills the returned byte array.
    • decrypt

      private static byte[] decrypt(BlockCipherAdapter cipher, byte[] data, int inOff)
      Decrypts the given data.
      Parameters:
      cipher - Adapter for either a block or AEAD cipher.
      data - Ciphertext data containing prepended header bytes.
      inOff - Offset into ciphertext at which encrypted data starts (i.e. after header).
      Returns:
      Decrypted data that completely fills the returned byte array.
    • process

      private static void process(BlockCipherAdapter cipher, InputStream input, OutputStream output)
      Performs encryption or decryption on the given input stream based on the underlying cipher mode and writes the result to the given output stream.
      Parameters:
      cipher - Adapter for either a block or AEAD cipher.
      input - Input stream containing data to be processed by the cipher.
      output - Output stream that receives the output of the cipher acting on the input.
    • writeHeader

      private static void writeHeader(byte[] header, OutputStream output)
      Writes a ciphertext header to the output stream.
      Parameters:
      header - Ciphertext header bytes.
      output - Output stream.