Class AbstractEncryptionAlgorithm

java.lang.Object
edu.vt.middleware.crypt.AbstractAlgorithm
edu.vt.middleware.crypt.AbstractEncryptionAlgorithm
All Implemented Interfaces:
Algorithm, EncryptionAlgorithm
Direct Known Subclasses:
AsymmetricAlgorithm

public abstract class AbstractEncryptionAlgorithm extends AbstractAlgorithm implements EncryptionAlgorithm
Base class for symmetric and asymmetric encryption algorithms. This class is essentially a wrapper for the Cipher class.
Version:
$Revision: 2745 $
Author:
Middleware Services
  • Field Details

    • cipher

      protected Cipher cipher
      Encryption/decription cipher.
    • mode

      protected String mode
      Mode used for encryption and decryption.
    • padding

      protected String padding
      Padding used for encryption and decryption.
    • cipherMode

      protected int cipherMode
    • key

      protected Key key
      Key used for encryption or decryption.
  • Constructor Details

    • AbstractEncryptionAlgorithm

      protected AbstractEncryptionAlgorithm(String cipherAlgorithm, String cipherModeName, String cipherPadding)
      Creates a new encryption algorithm that uses a cipher of the given name.
      Parameters:
      cipherAlgorithm - Cipher algorithm name.
      cipherModeName - Cipher mode.
      cipherPadding - Cipher padding method.
  • Method Details

    • getMode

      public String getMode()
      Gets the encryption mode.
      Specified by:
      getMode in interface EncryptionAlgorithm
      Returns:
      Name of an encryption mode, e.g. CBC.
    • getPadding

      public String getPadding()
      Gets the encryption padding method.
      Specified by:
      getPadding in interface EncryptionAlgorithm
      Returns:
      Name of a padding method, e.g. PKCS5Padding.
    • setKey

      public void setKey(Key k)
      Sets the key used for encryption/decryption.
      Specified by:
      setKey in interface EncryptionAlgorithm
      Parameters:
      k - Public, private, or secret key used for encryption or decryption.
    • getCipherMode

      public int getCipherMode()
      Gets the cipher mode indicating whether this instance is currently initialized for encryption or decryption.
      Specified by:
      getCipherMode in interface EncryptionAlgorithm
      Returns:
      Cipher.ENCRYPT_MODE, Cipher.DECRYPT_MODE, or 0 if the cipher mode has not been initialized by calling either EncryptionAlgorithm.initEncrypt() or EncryptionAlgorithm.initDecrypt().
    • getBlockSize

      public int getBlockSize()
      Gets the block size of the encryption algorithm cipher in bytes.
      Specified by:
      getBlockSize in interface EncryptionAlgorithm
      Returns:
      Block size of cipher in bytes, or 0 if the cipher is not a block cipher.
    • initEncrypt

      public void initEncrypt() throws CryptException
      Initializes this instance for encryption operations.
      Specified by:
      initEncrypt in interface EncryptionAlgorithm
      Throws:
      CryptException - On cryptographic configuration errors.
    • initDecrypt

      public void initDecrypt() throws CryptException
      Initializes this instance for decryption operations.
      Specified by:
      initDecrypt in interface EncryptionAlgorithm
      Throws:
      CryptException - On cryptographic configuration errors.
    • encrypt

      public byte[] encrypt(byte[] plaintext) throws CryptException
      Encrypts the given plaintext bytes into a byte array of ciphertext using the encryption key.
      Specified by:
      encrypt in interface EncryptionAlgorithm
      Parameters:
      plaintext - Input plaintext bytes.
      Returns:
      Ciphertext resulting from plaintext encryption.
      Throws:
      CryptException - On encryption errors.
    • encrypt

      public String encrypt(byte[] plaintext, Converter converter) throws CryptException
      Encrypts the given plaintext bytes into a ciphertext string using the conversion strategy provided by the given converter.
      Specified by:
      encrypt in interface EncryptionAlgorithm
      Parameters:
      plaintext - Input plaintext bytes.
      converter - Converter that converts ciphertext output bytes to a string representation.
      Returns:
      Ciphertext string resulting from plaintext encryption.
      Throws:
      CryptException - On encryption errors.
    • encrypt

      public void encrypt(InputStream in, OutputStream out) throws CryptException, IOException
      Encrypts the data in the given plaintext input stream into ciphertext in the output stream. Use Base64FilterOutputStream or HexFilterOutputStream to produce ciphertext in the output stream in an encoded string repreprestation.
      Specified by:
      encrypt in interface EncryptionAlgorithm
      Parameters:
      in - Input stream of plaintext.
      out - Output stream of ciphertext.
      Throws:
      CryptException - On encryption errors.
      IOException - On stream read/write errors.
    • decrypt

      public byte[] decrypt(byte[] ciphertext) throws CryptException
      Decrypts the given ciphertext bytes into a byte array of plaintext using the decryption key.
      Specified by:
      decrypt in interface EncryptionAlgorithm
      Parameters:
      ciphertext - Input ciphertext bytes.
      Returns:
      Plaintext resulting from ciphertext decryption.
      Throws:
      CryptException - On decryption errors.
    • decrypt

      public byte[] decrypt(String ciphertext, Converter converter) throws CryptException
      Decrypts a string representation of ciphertext bytes into a byte array of plaintext using the decryption key.
      Specified by:
      decrypt in interface EncryptionAlgorithm
      Parameters:
      ciphertext - Input ciphertext bytes.
      converter - Converter that converts the ciphertext input string into raw bytes required by the cipher algorithm.
      Returns:
      Plaintext resulting from ciphertext decryption.
      Throws:
      CryptException - On decryption errors.
    • decrypt

      public void decrypt(InputStream in, OutputStream out) throws CryptException, IOException
      Decrypts the data in the given ciphertext input stream into plaintext in the output stream. Use Base64FilterInputStream or HexFilterInputStream to consume ciphertext in an encoded string representation.
      Specified by:
      decrypt in interface EncryptionAlgorithm
      Parameters:
      in - Input stream of ciphertext.
      out - Output stream of plaintext.
      Throws:
      CryptException - On decryption errors.
      IOException - On stream read/write errors.
    • toString

      public String toString()
      Overrides:
      toString in class AbstractAlgorithm
    • initCipher

      protected void initCipher() throws CryptException
      Initializes the underlying Cipher object using AbstractAlgorithm.algorithm, mode, and padding.
      Throws:
      CryptException - if the algorithm is not available from any provider or if the provider is not available in the environment.
    • init

      protected void init(int encryptOrDecrypt) throws CryptException
      Initializes cipher for either encryption or decryption.
      Parameters:
      encryptOrDecrypt - Either Cipher.ENCRYPT_MODE or Cipher.DECRYPT_MODE.
      Throws:
      CryptException - On cryptographic configuration errors.
    • crypt

      protected byte[] crypt(byte[] in) throws CryptException
      Based on current cipher mode, encrypts or decrypts the given input data.
      Parameters:
      in - Cipher input data.
      Returns:
      Cipher output data.
      Throws:
      CryptException - On encryption errors.
    • crypt

      protected void crypt(InputStream in, OutputStream out) throws CryptException, IOException
      Based on current cipher mode, encrypts or decrypts the data in the given input stream into resulting data in the output stream.
      Parameters:
      in - Input stream.
      out - Output stream.
      Throws:
      CryptException - On encryption errors.
      IOException - On stream read/write errors.
    • getAlgorithmParameterSpec

      protected abstract AlgorithmParameterSpec getAlgorithmParameterSpec()
      Gets the algorithm parameter specification for this algorithm.
      Returns:
      Algorithm parameter specification specific to this algorithm.
    • getChunkSize

      protected abstract int getChunkSize()
      Gets the chunk size for buffers using in stream-based encryption and decryption operations.
      Returns:
      Stream chunk size.