Class CodecCipher
- java.lang.Object
-
- com.authlete.common.util.security.CodecCipher
-
- Direct Known Subclasses:
AESCipher
public class CodecCipher extends Object
Cipher with encoder and decoder.// This example shows encryption/decryption using "AES/CBC/PKCS5Padding", // but
AESCipheris much easier to use for "AES/CBC/PKCS5Padding". // Prepare cipher initialization parameters. Key key = new SecretKeySpec("1234567890123456".getBytes("UTF-8"), "AES"); AlgorithmParameterSpec spec = new IvParameterSpec("abcdefghijklmnop".getBytes("UTF-8")); // Create a cipher using "AES/CBC/PKCS5Padding". String transformation =StandardCipherTransformations.AES_CBC_PKCS5PADDING; CodecCipher cipher = newCodecCipher(transformation); // Set initialization parameters. cipher.setInit(key, spec); // 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 parameter of some constructors. cipher = new CodecCipher("AES/CBC/PKCS5Padding", hex); // If you want, an encoder and a decoder can be set separately. cipher.setEncoder(hex); cipher.setDecoder(hex);- Since:
- 4.23
- Author:
- Takahiko Kawasaki
- See Also:
- javax.crypto.Cipher
-
-
Constructor Summary
Constructors Constructor Description CodecCipher()The default constructor.CodecCipher(String transformation)Constructor with a transformation.CodecCipher(String transformation, String provider)Constructor with a transformation and a provider.CodecCipher(String transformation, String provider, org.apache.commons.codec.BinaryEncoder encoder, org.apache.commons.codec.BinaryDecoder decoder)Constructor with a transformation, a provider, an encoder and a decoder.CodecCipher(String transformation, String provider, TCoder coder)Constructor with a transformation, a provider and a coder.CodecCipher(String transformation, Provider provider)Constructor with a transformation and a provider.CodecCipher(String transformation, Provider provider, org.apache.commons.codec.BinaryEncoder encoder, org.apache.commons.codec.BinaryDecoder decoder)Constructor with a transformation, a provider, an encoder and a decoder.CodecCipher(String transformation, Provider provider, TCoder coder)Constructor with a transformation, a provider and a coder.CodecCipher(String transformation, org.apache.commons.codec.BinaryEncoder encoder, org.apache.commons.codec.BinaryDecoder decoder)Constructor with a transformation, an encoder and a decoder.CodecCipher(String transformation, TCoder coder)Constructor with a transformation and a coder.CodecCipher(Cipher cipher)Constructor with a cipher.CodecCipher(Cipher cipher, org.apache.commons.codec.BinaryEncoder encoder, org.apache.commons.codec.BinaryDecoder decoder)Constructor with a cipher, an encoder and a decoder.CodecCipher(Cipher cipher, TCoder coder)Constructor with a cipher and a coder.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description byte[]decrypt(byte[] input)Decrypt the given byte array.Stringdecrypt(String input)Decrypt the given string.byte[]encrypt(byte[] input)Encrypt the given byte array.Stringencrypt(String input)Encrypt the given string.StringgetAlgorithm()Get the cipher algorithm name.CiphergetCipher()Get the cipher which this instance internally holds.org.apache.commons.codec.BinaryDecodergetDecoder()Get the decoder which this instance internally holds.org.apache.commons.codec.BinaryEncodergetEncoder()Get the encoder which this instance internally holds.CodecCiphersetCipher(Cipher cipher)Set a cipher instance.<TCoder extends org.apache.commons.codec.BinaryEncoder & org.apache.commons.codec.BinaryDecoder>
CodecCiphersetCoder(TCoder coder)Set a coder.CodecCiphersetDecoder(org.apache.commons.codec.BinaryDecoder decoder)Set a decoder.CodecCiphersetEncoder(org.apache.commons.codec.BinaryEncoder encoder)Set an encoder.CodecCiphersetInit(Certificate certificate)Set cipher initialization parameters.CodecCiphersetInit(Certificate certificate, SecureRandom random)Set cipher initialization parameters.CodecCiphersetInit(Key key)Set cipher initialization parameters.CodecCiphersetInit(Key key, AlgorithmParameters params)Set cipher initialization parameters.CodecCiphersetInit(Key key, AlgorithmParameters params, SecureRandom random)Set cipher initialization parameters.CodecCiphersetInit(Key key, SecureRandom random)Set cipher initialization parameters.CodecCiphersetInit(Key key, AlgorithmParameterSpec spec)Set cipher initialization parameters.CodecCiphersetInit(Key key, AlgorithmParameterSpec spec, SecureRandom random)Set cipher initialization parameters.
-
-
-
Constructor Detail
-
CodecCipher
public CodecCipher()
The default constructor.
-
CodecCipher
public CodecCipher(Cipher cipher, org.apache.commons.codec.BinaryEncoder encoder, org.apache.commons.codec.BinaryDecoder decoder)
Constructor with a cipher, an encoder and a decoder.- Parameters:
cipher- A cipher. Ifnullis given,setCipher(Cipher)must be called later with a valid cipher.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.
-
CodecCipher
public CodecCipher(Cipher cipher, TCoder coder)
Constructor with a cipher and a coder.This constructor is an alias of
CodecCipher(cipher, coder, coder).- Parameters:
cipher- A cipher. Ifnullis given,setCipher(Cipher)must be called later with a valid cipher.coder- A coder which works as both an encoder and a decoder. Ifnullis given,Base64is used as the default coder.
-
CodecCipher
public CodecCipher(Cipher cipher)
Constructor with a cipher.This constructor is an alias of
CodecCipher(cipher, (BinaryEncoder)null, (BinaryDecoder)null).- Parameters:
cipher- A cipher. Ifnullis given,setCipher(Cipher)must be called later with a valid cipher.
-
CodecCipher
public CodecCipher(String transformation, org.apache.commons.codec.BinaryEncoder encoder, org.apache.commons.codec.BinaryDecoder decoder) throws IllegalArgumentException
Constructor with a transformation, an encoder and a decoder.- Parameters:
transformation- A transformation in the form of either"algorithm/mode/padding"or"algorithm". For example,"AES/CBC/PKCS5Padding".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.- Throws:
IllegalArgumentException- The given transformation is not supported. This exception wraps the original exception (such asNoSuchAlgorithmExceptionandNoSuchPaddingException) as the cause.- See Also:
- Cipher (Encryption) Algorithms,
StandardCipherTransformations
-
CodecCipher
public CodecCipher(String transformation, TCoder coder) throws IllegalArgumentException
Constructor with a transformation and a coder.This constructor is an alias of
CodecCipher(transformation, coder, coder).- Parameters:
transformation- A transformation in the form of either"algorithm/mode/padding"or"algorithm". For example,"AES/CBC/PKCS5Padding".coder- A coder which works as both an encoder and a decoder. Ifnullis given,Base64is used as the default coder.- Throws:
IllegalArgumentException- The given transformation is not supported. This exception wraps the original exception (such asNoSuchAlgorithmExceptionandNoSuchPaddingException) as the cause.- See Also:
- Cipher (Encryption) Algorithms,
StandardCipherTransformations
-
CodecCipher
public CodecCipher(String transformation) throws IllegalArgumentException
Constructor with a transformation.This constructor is an alias of
CodecCipher(transformation, (BinaryEncoder)null, (BinaryDecoder)null).- Parameters:
transformation- A transformation in the form of either"algorithm/mode/padding"or"algorithm". For example,"AES/CBC/PKCS5Padding".- Throws:
IllegalArgumentException- The given transformation is not supported. This exception wraps the original exception (such asNoSuchAlgorithmExceptionandNoSuchPaddingException) as the cause.- See Also:
- Cipher (Encryption) Algorithms,
StandardCipherTransformations
-
CodecCipher
public CodecCipher(String transformation, String provider, org.apache.commons.codec.BinaryEncoder encoder, org.apache.commons.codec.BinaryDecoder decoder) throws IllegalArgumentException
Constructor with a transformation, a provider, an encoder and a decoder.- Parameters:
transformation- A transformation in the form of either"algorithm/mode/padding"or"algorithm". For example,"AES/CBC/PKCS5Padding".provider- A name of provider which provides the implementation of the transformation.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.- Throws:
IllegalArgumentException- The given transformation is not supported. This exception wraps the original exception (such asNoSuchAlgorithmException,NoSuchPaddingExceptionandNoSuchProviderException) as the cause.- See Also:
- Cipher (Encryption) Algorithms,
StandardCipherTransformations
-
CodecCipher
public CodecCipher(String transformation, String provider, TCoder coder) throws IllegalArgumentException
Constructor with a transformation, a provider and a coder.This constructor is an alias of
CodecCipher(transformation, provider, coder, coder).- Parameters:
transformation- A transformation in the form of either"algorithm/mode/padding"or"algorithm". For example,"AES/CBC/PKCS5Padding".provider- A name of provider which provides the implementation of the transformation.coder- A coder which works as both an encoder and a decoder. Ifnullis given,Base64is used as the default coder.- Throws:
IllegalArgumentException- The given transformation is not supported. This exception wraps the original exception (such asNoSuchAlgorithmException,NoSuchPaddingExceptionandNoSuchProviderException) as the cause.- See Also:
- Cipher (Encryption) Algorithms,
StandardCipherTransformations
-
CodecCipher
public CodecCipher(String transformation, String provider) throws IllegalArgumentException
Constructor with a transformation and a provider.This constructor is an alias of
CodecCipher(transformation, provider, (BinaryEncoder)null, (BinaryDecoder)null).- Parameters:
transformation- A transformation in the form of either"algorithm/mode/padding"or"algorithm". For example,"AES/CBC/PKCS5Padding".provider- A name of provider which provides the implementation of the transformation.- Throws:
IllegalArgumentException- The given transformation is not supported. This exception wraps the original exception (such asNoSuchAlgorithmException,NoSuchPaddingExceptionandNoSuchProviderException) as the cause.- See Also:
- Cipher (Encryption) Algorithms,
StandardCipherTransformations
-
CodecCipher
public CodecCipher(String transformation, Provider provider, org.apache.commons.codec.BinaryEncoder encoder, org.apache.commons.codec.BinaryDecoder decoder) throws IllegalArgumentException
Constructor with a transformation, a provider, an encoder and a decoder.- Parameters:
transformation- A transformation in the form of either"algorithm/mode/padding"or"algorithm". For example,"AES/CBC/PKCS5Padding".provider- A provider which provides the implementation of the transformation.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.- Throws:
IllegalArgumentException- The given transformation is not supported. This exception wraps the original exception (such asNoSuchAlgorithmExceptionandNoSuchPaddingException) as the cause.- See Also:
- Cipher (Encryption) Algorithms,
StandardCipherTransformations
-
CodecCipher
public CodecCipher(String transformation, Provider provider, TCoder coder) throws IllegalArgumentException
Constructor with a transformation, a provider and a coder.This constructor is an alias of
CodecCipher(transformation, provider, coder, coder).- Parameters:
transformation- A transformation in the form of either"algorithm/mode/padding"or"algorithm". For example,"AES/CBC/PKCS5Padding".provider- A provider which provides the implementation of the transformation.coder- A coder which works as both an encoder and a decoder. Ifnullis given,Base64is used as the default coder.- Throws:
IllegalArgumentException- The given transformation is not supported. This exception wraps the original exception (such asNoSuchAlgorithmExceptionandNoSuchPaddingException) as the cause.- See Also:
- Cipher (Encryption) Algorithms,
StandardCipherTransformations
-
CodecCipher
public CodecCipher(String transformation, Provider provider) throws IllegalArgumentException
Constructor with a transformation and a provider.This constructor is an alias of
CodecCipher(transformation, provider, (BinaryEncoder)null, (BinaryDecoder)null).- Parameters:
transformation- A transformation in the form of either"algorithm/mode/padding"or"algorithm". For example,"AES/CBC/PKCS5Padding".provider- A provider which provides the implementation of the transformation.- Throws:
IllegalArgumentException- The given transformation is not supported. This exception wraps the original exception (such asNoSuchAlgorithmExceptionandNoSuchPaddingException) as the cause.- See Also:
- Cipher (Encryption) Algorithms,
StandardCipherTransformations
-
-
Method Detail
-
getCipher
public Cipher getCipher()
Get the cipher which this instance internally holds.- Returns:
- The
Cipherinstance.
-
setCipher
public CodecCipher setCipher(Cipher cipher)
Set a cipher instance.- Parameters:
cipher- A cipher instance.- Returns:
thisobject.
-
getAlgorithm
public String getAlgorithm()
Get the cipher algorithm name.If the internal cipher is
null,nullis returned. Otherwise, the algorithm part of the cipher's transformation is returned.- Returns:
- The cipher algorithm name.
-
getEncoder
public org.apache.commons.codec.BinaryEncoder getEncoder()
Get the encoder which this instance internally holds.- Returns:
- The internal encoder. This may be
null, and in such a case,encrypt(String)andencrypt(byte[])useBase64.
-
setEncoder
public CodecCipher setEncoder(org.apache.commons.codec.BinaryEncoder encoder)
Set an encoder.- Parameters:
encoder- An encoder used byencrypt(String)andencrypt(byte[]).- Returns:
thisobject.
-
getDecoder
public org.apache.commons.codec.BinaryDecoder getDecoder()
Get the decoder which this instance internally holds.- Returns:
- The internal decoder. This may be
null, and in such a case,decrypt(String)anddecrypt(byte[])useBase64.
-
setDecoder
public CodecCipher setDecoder(org.apache.commons.codec.BinaryDecoder decoder)
Set a decoder.- Parameters:
decoder- A decoder used bydecrypt(String)anddecrypt(byte[]).- Returns:
thisobject.
-
setCoder
public <TCoder extends org.apache.commons.codec.BinaryEncoder & org.apache.commons.codec.BinaryDecoder> CodecCipher setCoder(TCoder coder)
Set a coder.- Parameters:
coder- A coder which works as both an encoder and a decoder. Ifnullis given,Base64is used as the default coder.- Returns:
thisobject.
-
setInit
public CodecCipher setInit(Key key) throws IllegalArgumentException
Set cipher initialization parameters.If this method is used to set initialization parameters,
Cipher.init(mode, (Key)key)is called later from withinencrypt/decryptmethods.- Parameters:
key-- Returns:
thisobject.- Throws:
IllegalArgumentException-keyisnull.
-
setInit
public CodecCipher setInit(Key key, SecureRandom random) throws IllegalArgumentException
Set cipher initialization parameters.If this method is used to set initialization parameters,
Cipher.init(mode, (Key)key, (SecureRandom)random)is called later from withinencrypt/decryptmethods.- Parameters:
key-random-- Returns:
thisobject.- Throws:
IllegalArgumentException-keyisnull.
-
setInit
public CodecCipher setInit(Key key, AlgorithmParameters params) throws IllegalArgumentException
Set cipher initialization parameters.If this method is used to set initialization parameters,
Cipher.init(mode, (Key)key, (AlgorithmParameters)params)is called later from withinencrypt/decryptmethods.- Parameters:
key-params-- Returns:
thisobject.- Throws:
IllegalArgumentException-keyisnull.
-
setInit
public CodecCipher setInit(Key key, AlgorithmParameters params, SecureRandom random) throws IllegalArgumentException
Set cipher initialization parameters.If this method is used to set initialization parameters,
Cipher.init(mode, (Key)key, (AlgorithmParameters)params, (SecureRandom)random)is called later from withinencrypt/decryptmethods.- Parameters:
key-params-random-- Returns:
thisobject.- Throws:
IllegalArgumentException-keyisnull.
-
setInit
public CodecCipher setInit(Key key, AlgorithmParameterSpec spec) throws IllegalArgumentException
Set cipher initialization parameters.If this method is used to set initialization parameters,
Cipher.init(mode, (Key)key, (AlgorithmParameterSpec)spec)is called later from withinencrypt/decryptmethods.- Parameters:
key-spec-- Returns:
thisobject.- Throws:
IllegalArgumentException-keyisnull.
-
setInit
public CodecCipher setInit(Key key, AlgorithmParameterSpec spec, SecureRandom random) throws IllegalArgumentException
Set cipher initialization parameters.If this method is used to set initialization parameters,
Cipher.init(mode, (Key)key, (AlgorithmParameterSpec)spec, (SecureRandom)random)is called later from withinencrypt/decryptmethods.- Parameters:
key-spec-random-- Returns:
thisobject.- Throws:
IllegalArgumentException-keyisnull.
-
setInit
public CodecCipher setInit(Certificate certificate) throws IllegalArgumentException
Set cipher initialization parameters.If this method is used to set initialization parameters,
Cipher.init(mode, (Certificate)certificate)is called later from withinencrypt/decryptmethods.- Parameters:
certificate-- Returns:
thisobject.- Throws:
IllegalArgumentException-certificateisnull.
-
setInit
public CodecCipher setInit(Certificate certificate, SecureRandom random) throws IllegalArgumentException
Set cipher initialization parameters.If this method is used to set initialization parameters,
Cipher.init(mode, (Certificate)certificate, (SecureRandom)random)is called later from withinencrypt/decryptmethods.- Parameters:
certificate-random-- Returns:
thisobject.- Throws:
IllegalArgumentException-certificateisnull.
-
encrypt
public String encrypt(String input) throws IllegalStateException
Encrypt the given string.- Parameters:
input- Plain text before encryption.- Returns:
- Encrypted string encoded by the encoder.
If
inputisnull,nullis returned. - Throws:
IllegalStateException- Failed to encrypt the input. TheIllegalStateExceptionwraps the original exception (such asInvalidKeyExceptionandEncoderException) as the cause, if any.- A cipher instance has not been set.
setInitmethod has not been called.- The cipher failed to encrypt the input (probably initialization
parameters set by
setInitmethod are wrong). - The encoder failed to encode the encrypted byte array.
-
decrypt
public String decrypt(String input) throws IllegalStateException
Decrypt the given string.- Parameters:
input- Encrypted string encoded by an encoder.- Returns:
- Decrypted plain text.
If
inputisnull,nullis returned. - Throws:
IllegalStateException- Failed to decrypt the input. TheIllegalStateExceptionwraps the original exception (such asInvalidKeyExceptionandDecoderException) as the cause, if any.- A cipher instance has not been set.
setInitmethod has not been called.- The decoder failed to decode the input.
- The cipher failed to decrypt the byte array (probably initialization
parameters set by
setInitmethod are wrong).
-
encrypt
public byte[] encrypt(byte[] input) throws IllegalStateExceptionEncrypt the given byte array.- Parameters:
input- Plain byte array before encryption.- Returns:
- Encrypted byte array encoded by the encoder.
If
inputisnull,nullis returned. - Throws:
IllegalStateException- Failed to encrypt the input. TheIllegalStateExceptionwraps the original exception (such asInvalidKeyExceptionandEncoderException) as the cause, if any.- A cipher instance has not been set.
setInitmethod has not been called.- The cipher failed to encrypt the input (probably initialization
parameters set by
setInitmethod are wrong). - The encoder failed to encode the encrypted byte array.
-
decrypt
public byte[] decrypt(byte[] input) throws IllegalStateExceptionDecrypt the given byte array.- Parameters:
input- Encrypted byte array encoded by an encoder.- Returns:
- Decrypted plain byte array.
If
inputisnull,nullis returned. - Throws:
IllegalStateException- Failed to decrypt the input. TheIllegalStateExceptionwraps the original exception (such asInvalidKeyExceptionandDecoderException) as the cause, if any.- A cipher instance has not been set.
setInitmethod has not been called.- The decoder failed to decode the input.
- The cipher failed to decrypt the byte array (probably initialization
parameters set by
setInitmethod are wrong).
-
-