Interface CryptoCipher

All Superinterfaces:
AutoCloseable, Closeable
All Known Implementing Classes:
JceCipher, OpenSslCipher, OpenSslJnaCipher

public interface CryptoCipher extends Closeable
The interface of cryptographic cipher for encryption and decryption.

Note that implementations must provide a constructor that has 2 parameters: a Properties instance and a String (transformation)

  • Method Summary

    Modifier and Type
    Method
    Description
    int
    doFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset)
    Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.
    int
    doFinal(ByteBuffer inBuffer, ByteBuffer outBuffer)
    Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.
    Returns the algorithm name of this CryptoCipher object.
    int
    Returns the block size (in bytes).
    void
    init(int mode, Key key, AlgorithmParameterSpec params)
    Initializes the cipher with mode, key and iv.
    int
    update(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset)
    Continues a multiple-part encryption/decryption operation.
    int
    update(ByteBuffer inBuffer, ByteBuffer outBuffer)
    Continues a multiple-part encryption/decryption operation.
    default void
    updateAAD(byte[] aad)
    Continues a multi-part update of the Additional Authentication Data (AAD).
    default void
    Continues a multi-part update of the Additional Authentication Data (AAD).

    Methods inherited from interface java.io.Closeable

    close
  • Method Details

    • doFinal

      int doFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException
      Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.
      Parameters:
      input - the input byte array
      inputOffset - the offset in input where the input starts
      inputLen - the input length
      output - the byte array for the result
      outputOffset - the offset in output where the result is stored
      Returns:
      the number of bytes stored in output
      Throws:
      ShortBufferException - if the given output byte array is too small to hold the result
      BadPaddingException - if this cipher is in decryption mode, and (un)padding has been requested, but the decrypted data is not bounded by the appropriate padding bytes
      IllegalBlockSizeException - if this cipher is a block cipher, no padding has been requested (only in encryption mode), and the total input length of the data processed by this cipher is not a multiple of block size; or if this encryption algorithm is unable to process the input data provided.
    • doFinal

      Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.
      Parameters:
      inBuffer - the input ByteBuffer
      outBuffer - the output ByteBuffer
      Returns:
      int number of bytes stored in output
      Throws:
      BadPaddingException - if this cipher is in decryption mode, and (un)padding has been requested, but the decrypted data is not bounded by the appropriate padding bytes
      IllegalBlockSizeException - if this cipher is a block cipher, no padding has been requested (only in encryption mode), and the total input length of the data processed by this cipher is not a multiple of block size; or if this encryption algorithm is unable to process the input data provided.
      ShortBufferException - if the given output buffer is too small to hold the result
    • getAlgorithm

      String getAlgorithm()
      Returns the algorithm name of this CryptoCipher object.

      This is the same name that was specified in one of the CryptoCipherFactory#getInstance calls that created this CryptoCipher object..

      Returns:
      the algorithm name of this CryptoCipher object.
    • getBlockSize

      int getBlockSize()
      Returns the block size (in bytes).
      Returns:
      the block size (in bytes), or 0 if the underlying algorithm is not a block cipher
    • init

      Initializes the cipher with mode, key and iv.
      Parameters:
      mode - Cipher.ENCRYPT_MODE or Cipher.DECRYPT_MODE
      key - crypto key for the cipher
      params - the algorithm parameters
      Throws:
      InvalidKeyException - if the given key is inappropriate for initializing this cipher, or its keysize exceeds the maximum allowable keysize (as determined from the configured jurisdiction policy files).
      InvalidAlgorithmParameterException - if the given algorithm parameters are inappropriate for this cipher, or this cipher requires algorithm parameters and params is null, or the given algorithm parameters imply a cryptographic strength that would exceed the legal limits (as determined from the configured jurisdiction policy files).
    • update

      int update(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws ShortBufferException
      Continues a multiple-part encryption/decryption operation. The data is encrypted or decrypted, depending on how this cipher was initialized.
      Parameters:
      input - the input byte array
      inputOffset - the offset in input where the input starts
      inputLen - the input length
      output - the byte array for the result
      outputOffset - the offset in output where the result is stored
      Returns:
      the number of bytes stored in output
      Throws:
      ShortBufferException - if there is insufficient space in the output byte array
    • update

      int update(ByteBuffer inBuffer, ByteBuffer outBuffer) throws ShortBufferException
      Continues a multiple-part encryption/decryption operation. The data is encrypted or decrypted, depending on how this cipher was initialized.
      Parameters:
      inBuffer - the input ByteBuffer
      outBuffer - the output ByteBuffer
      Returns:
      int number of bytes stored in output
      Throws:
      ShortBufferException - if there is insufficient space in the output buffer
    • updateAAD

      Continues a multi-part update of the Additional Authentication Data (AAD).

      Calls to this method provide AAD to the cipher when operating in modes such as AEAD (GCM). If this cipher is operating in GCM mode, all AAD must be supplied before beginning operations on the ciphertext (via the update and doFinal methods).

      Parameters:
      aad - the buffer containing the Additional Authentication Data
      Throws:
      IllegalArgumentException - if the aad byte array is null
      IllegalStateException - if this cipher is in a wrong state (e.g., has not been initialized), does not accept AAD, or if operating in either GCM or CCM mode and one of the update methods has already been called for the active encryption/decryption operation
      UnsupportedOperationException - if the corresponding method has not been overridden by an implementation
    • updateAAD

      Continues a multi-part update of the Additional Authentication Data (AAD).

      Calls to this method provide AAD to the cipher when operating in modes such as AEAD (GCM). If this cipher is operating in GCM mode, all AAD must be supplied before beginning operations on the ciphertext (via the update and doFinal methods).

      Parameters:
      aad - the buffer containing the Additional Authentication Data
      Throws:
      IllegalArgumentException - if the aad byte array is null
      IllegalStateException - if this cipher is in a wrong state (e.g., has not been initialized), does not accept AAD, or if operating in either GCM or CCM mode and one of the update methods has already been called for the active encryption/decryption operation
      UnsupportedOperationException - if the corresponding method has not been overridden by an implementation