Class AESCipher
- java.lang.Object
-
- com.authlete.common.util.security.CodecCipher
-
- com.authlete.common.util.security.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, usesetCodermethod. // 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 Summary
Fields Modifier and Type Field Description static intDEFAULT_KEY_SIZEThe default key size.static StringDEFAULT_TRANSFORMATIONThe default transformation.
-
Constructor Summary
Constructors Constructor Description AESCipher()Constructor.AESCipher(String transformation)Constructor.AESCipher(String transformation, org.apache.commons.codec.BinaryEncoder encoder, org.apache.commons.codec.BinaryDecoder decoder)Constructor with a transformation, an encoder and a decoder.AESCipher(String transformation, TCoder coder)Constructor with a transformation and a coder.AESCipher(org.apache.commons.codec.BinaryEncoder encoder, org.apache.commons.codec.BinaryDecoder decoder)Constructor with an encoder and a decoder.AESCipher(TCoder coder)Constructor with a coder.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description AESCiphersetKey(byte[] key)Set cipher initialization parameters.AESCiphersetKey(byte[] key, byte[] iv)Set cipher initialization parameters.AESCiphersetKey(byte[] key, byte[] iv, int keySize)Set cipher initialization parameters.AESCiphersetKey(byte[] key, int keySize)Set cipher initialization parameters.AESCiphersetKey(byte[] key, String iv)Set cipher initialization parameters.AESCiphersetKey(byte[] key, String iv, int keySize)Set cipher initialization parameters.AESCiphersetKey(String key)Set cipher initialization parameters.AESCiphersetKey(String key, byte[] iv)Set cipher initialization parameters.AESCiphersetKey(String key, byte[] iv, int keySize)Set cipher initialization parameters.AESCiphersetKey(String key, int keySize)Set cipher initialization parameters.AESCiphersetKey(String key, String iv)Set cipher initialization parameters.AESCiphersetKey(String key, String iv, int keySize)Set cipher initialization parameters.AESCiphersetKey(SecretKey key, IvParameterSpec iv)Set cipher initialization parameters.-
Methods inherited from class com.authlete.common.util.security.CodecCipher
decrypt, decrypt, encrypt, encrypt, getAlgorithm, getCipher, getDecoder, getEncoder, setCipher, setCoder, setDecoder, setEncoder, setInit, setInit, setInit, setInit, setInit, setInit, setInit, setInit
-
-
-
-
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()
Constructor.This constructor just performs
super("AES/CBC/PKCS5Padding").
-
AESCipher
public AESCipher(String transformation)
Constructor.This constructor just performs
super(transformation).
-
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 inencrypt(String)andencrypt(byte[])to encode an encrypted byte array. Ifnullis given,Base64is used as the default encoder.decoder- A decoder used indecrypt(String)anddecrypt(byte[])to decode an encoded input byte array. Ifnullis given,Base64is 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 inencrypt(String)andencrypt(byte[])to encode an encrypted byte array. Ifnullis given,Base64is used as the default encoder.decoder- A decoder used indecrypt(String)anddecrypt(byte[])to decode an encoded input byte array. Ifnullis given,Base64is 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. Ifnullis given,Base64is 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. Ifnullis given,Base64is used as the default coder.
-
-
Method Detail
-
setKey
public AESCipher setKey(SecretKey key, IvParameterSpec iv)
Set cipher initialization parameters.This method is an alias of
setInit(key, iv).- Parameters:
key- Secret key.iv- Initial vector.- Returns:
thisobject.- Throws:
IllegalArgumentException-keyisnull.
-
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:
thisobject.
-
setKey
public AESCipher setKey(byte[] key, byte[] iv, int keySize)
Set cipher initialization parameters. OthersetKeymethod variants exceptsetKey(SecretKey, IvParameterSpec)eventually call this method.This method constructs a
SecretKeyinstance and anIvParameterSpecinstance from the arguments, and then callssetKey(SecretKey, IvParameterSpec).- Parameters:
key- Secret key. Ifnullis given,new byte[16]is used. If notnulland the length is less than 16, a byte array of size 16 is allocated and the content ofkeyis copied to the newly allocated byte array, and the resultant byte array is used.iv- Initial vector. Ifnullis given,nullis used, meaning thatIvParameterSepcargument passed tosetKey(SecretKey, IvParameterSpec)isnull. In that case, you will want to obtain the auto-generated initial vector by callinggetCipher().getIV()in order to decrypt the encrypted data.If
ivis notnulland the length is less than 16, a byte array of size 16 is allocated and the content ofivis 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 anIvParameterSpecinstance.keySize- The size of the secret key in bytes.- Returns:
thisobject.
-
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:
thisobject.
-
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 bykey.getBytes("UTF-8")and used as the first argument ofsetKey(byte[], byte[], int).iv- Initial vector.keySize- The size of the secret key in bytes.- Returns:
thisobject.
-
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:
thisobject.
-
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 bykey.getBytes("UTF-8")and used as the first argument ofsetKey(byte[], byte[], int).iv- Initial vector. The value is converted to a byte array byiv.getBytes("UTF-8")and used as the second argument ofsetKey(byte[], byte[], int).keySize- The size of the secret key in bytes.- Returns:
thisobject.
-
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:
thisobject.
-
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 bykey.getBytes("UTF-8")and used as the first argument ofsetKey(byte[], byte[], int).keySize- The size of the secret key in bytes.- Returns:
thisobject.
-
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:
thisobject.
-
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 byiv.getBytes("UTF-8")and used as the second argument ofsetKey(byte[], byte[], int).keySize- The size of the secret key in bytes.- Returns:
thisobject.
-
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:
thisobject.
-
-