Class AESCipher


  • public class AESCipher
    extends CodecCipher
    Cipher using "AES/CBC/PKCS5Padding".
     // Create a cipher with a secret key.
     AESCipher cipher = new AESCipher().setKey("secret key", "initial vector");
    
     // Encryption & decryption.
     // 'plaintext' and 'decrypted' have the same value.
     String plaintext = "plain text";
     String encrypted = cipher.encrypt(plaintext);
     String decrypted = cipher.decrypt(encrypted);
    
     // In the above example, 'encrypted' is encoded by Base64 (default).
     // If you want to change the format, use setCoder method.
     // For example, to change the format to hexadecimal:
     Hex hex = new org.apache.commons.codec.binary.Hex();
     cipher.setCoder(hex);
    
     // Binary representation (only "0"s and "1"s) also can be used.
     BinaryCodec binary = new org.apache.commons.codec.BinaryCodec();
     cipher.setCoder(binary);
    
     // Coder can be specified as a constructor parameter.
     cipher = new AESCipher(hex);
    
     // If you want, an encoder and a decoder can be set separately.
     cipher.setEncoder(hex);
     cipher.setDecoder(hex);
     
     // Another example which performs encryption without initial vector.
     String secretkey = "secret key";
     String plaintext = "plain text";
    
     // Create and set up without initial vector.
     AESCipher cipher = new AESCipher().setKey(secretkey);
    
     // Encrypt.
     String encrypted = cipher.encrypt(plaintext);
    
     // Get the auto-generated initial vector.
     byte[] iv = cipher.getCipher().getIV();
    
     // Decryption requires initial vector.
     cipher.setKey(secretkey, iv);
    
     // Decrypt.
     String decrypted = cipher.decrypt(encrypted);
     
    Since:
    4.23
    Author:
    Takahiko Kawasaki
    • Field Detail

      • DEFAULT_KEY_SIZE

        public static final int DEFAULT_KEY_SIZE
        The default key size. The value is 16, meaning 128 bits.
        See Also:
        Constant Field Values
      • DEFAULT_TRANSFORMATION

        public static final String DEFAULT_TRANSFORMATION
        The default transformation. The value is "AES/CBC/PKCS5Padding".
        See Also:
        Constant Field Values
    • Constructor Detail

      • AESCipher

        public AESCipher​(org.apache.commons.codec.BinaryEncoder encoder,
                         org.apache.commons.codec.BinaryDecoder decoder)
        Constructor with an encoder and a decoder.

        This constructor just performs super("AES/CBC/PKCS5Padding", encoder, decoder).

        Parameters:
        encoder - An encoder used in encrypt(String) and encrypt(byte[]) to encode an encrypted byte array. If null is given, Base64 is used as the default encoder.
        decoder - A decoder used in decrypt(String) and decrypt(byte[]) to decode an encoded input byte array. If null is given, Base64 is used as the default decoder.
      • AESCipher

        public AESCipher​(String transformation,
                         org.apache.commons.codec.BinaryEncoder encoder,
                         org.apache.commons.codec.BinaryDecoder decoder)
        Constructor with a transformation, an encoder and a decoder.

        This constructor just performs super(transformation, encoder, decoder).

        Parameters:
        encoder - An encoder used in encrypt(String) and encrypt(byte[]) to encode an encrypted byte array. If null is given, Base64 is used as the default encoder.
        decoder - A decoder used in decrypt(String) and decrypt(byte[]) to decode an encoded input byte array. If null is given, Base64 is used as the default decoder.
      • AESCipher

        public AESCipher​(TCoder coder)
        Constructor with a coder.

        This constructor just performs super("AES/CBC/PKCS5Padding", coder).

        Parameters:
        coder - A coder which works as both an encoder and a decoder. If null is given, Base64 is used as the default coder.
      • AESCipher

        public AESCipher​(String transformation,
                         TCoder coder)
        Constructor with a transformation and a coder.

        This constructor just performs super(transformation, coder).

        Parameters:
        coder - A coder which works as both an encoder and a decoder. If null is given, Base64 is used as the default coder.
    • Method Detail

      • setKey

        public AESCipher setKey​(byte[] key,
                                byte[] iv)
        Set cipher initialization parameters.

        This method is an alias of setKey(key, iv, DEFAULT_KEY_SIZE).

        Parameters:
        key - Secret key.
        iv - Initial vector.
        Returns:
        this object.
      • setKey

        public AESCipher setKey​(byte[] key,
                                byte[] iv,
                                int keySize)
        Set cipher initialization parameters. Other setKey method variants except setKey(SecretKey, IvParameterSpec) eventually call this method.

        This method constructs a SecretKey instance and an IvParameterSpec instance from the arguments, and then calls setKey(SecretKey, IvParameterSpec).

        Parameters:
        key - Secret key. If null is given, new byte[16] is used. If not null and the length is less than 16, a byte array of size 16 is allocated and the content of key is copied to the newly allocated byte array, and the resultant byte array is used.
        iv - Initial vector. If null is given, null is used, meaning that IvParameterSepc argument passed to setKey(SecretKey, IvParameterSpec) is null. In that case, you will want to obtain the auto-generated initial vector by calling getCipher().getIV() in order to decrypt the encrypted data.

        If iv is not null and the length is less than 16, a byte array of size 16 is allocated and the content of iv is copied to the newly allocated byte array, and the resultant byte array is used. Even if the length is greater than 16, only the first 16 bytes are used to construct an IvParameterSpec instance.

        keySize - The size of the secret key in bytes.
        Returns:
        this object.
      • setKey

        public AESCipher setKey​(String key,
                                byte[] iv)
        Set cipher initialization parameters.

        This method is an alias of setKey(key, iv, DEFAULT_KEY_SIZE).

        Parameters:
        key - Secret key.
        iv - Initial vector.
        Returns:
        this object.
      • setKey

        public AESCipher setKey​(String key,
                                byte[] iv,
                                int keySize)
        Set cipher initialization parameters.

        This method is an alias of setKey(byte[], byte[], int).

        Parameters:
        key - Secret key. The value is converted to a byte array by key.getBytes("UTF-8") and used as the first argument of setKey(byte[], byte[], int).
        iv - Initial vector.
        keySize - The size of the secret key in bytes.
        Returns:
        this object.
      • setKey

        public AESCipher setKey​(String key,
                                String iv)
        Set cipher initialization parameters.

        This method is an alias of setKey(key, iv, DEFAULT_KEY_SIZE).

        Parameters:
        key - Secret key.
        iv - Initial vector.
        Returns:
        this object.
      • setKey

        public AESCipher setKey​(String key,
                                String iv,
                                int keySize)
        Set cipher initialization parameters.

        This method is an alias of setKey(byte[], byte[], int).

        Parameters:
        key - Secret key. The value is converted to a byte array by key.getBytes("UTF-8") and used as the first argument of setKey(byte[], byte[], int).
        iv - Initial vector. The value is converted to a byte array by iv.getBytes("UTF-8") and used as the second argument of setKey(byte[], byte[], int).
        keySize - The size of the secret key in bytes.
        Returns:
        this object.
      • setKey

        public AESCipher setKey​(String key)
        Set cipher initialization parameters.

        This method is an alias of setKey(key, DEFAULT_KEY_SIZE)

        Parameters:
        key - Secret key.
        Returns:
        this object.
      • setKey

        public AESCipher setKey​(String key,
                                int keySize)
        Set cipher initialization parameters.

        This method is an alias of setKey(byte[], byte[], int).

        Parameters:
        key - Secret key. The value is converted to a byte array by key.getBytes("UTF-8") and used as the first argument of setKey(byte[], byte[], int).
        keySize - The size of the secret key in bytes.
        Returns:
        this object.
      • setKey

        public AESCipher setKey​(byte[] key,
                                String iv)
        Set cipher initialization parameters.

        This method is an alias of setKey(key, iv, DEFAULT_KEY_SIZE).

        Parameters:
        key - Secret key.
        iv - Initial vector.
        Returns:
        this object.
      • setKey

        public AESCipher setKey​(byte[] key,
                                String iv,
                                int keySize)
        Set cipher initialization parameters.

        This method is an alias of setKey(byte[], byte[], int).

        Parameters:
        key - Secret key.
        iv - Initial vector. The value is converted to a byte array by iv.getBytes("UTF-8") and used as the second argument of setKey(byte[], byte[], int).
        keySize - The size of the secret key in bytes.
        Returns:
        this object.
      • setKey

        public AESCipher setKey​(byte[] key)
        Set cipher initialization parameters.

        This method is an alias of setKey(key, DEFAULT_KEY_SIZE).

        Parameters:
        key - Secret key.
        Returns:
        this object.
      • setKey

        public AESCipher setKey​(byte[] key,
                                int keySize)
        Set cipher initialization parameters.

        This method is an alias of setKey(key, (byte[])null, keySize).

        Parameters:
        key - Secret key.
        keySize - The size of the secret key in bytes.
        Returns:
        this object.