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
AESCipher
is 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, usesetCoder
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 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.String
decrypt(String input)
Decrypt the given string.byte[]
encrypt(byte[] input)
Encrypt the given byte array.String
encrypt(String input)
Encrypt the given string.String
getAlgorithm()
Get the cipher algorithm name.Cipher
getCipher()
Get the cipher which this instance internally holds.org.apache.commons.codec.BinaryDecoder
getDecoder()
Get the decoder which this instance internally holds.org.apache.commons.codec.BinaryEncoder
getEncoder()
Get the encoder which this instance internally holds.CodecCipher
setCipher(Cipher cipher)
Set a cipher instance.<TCoder extends org.apache.commons.codec.BinaryEncoder & org.apache.commons.codec.BinaryDecoder>
CodecCiphersetCoder(TCoder coder)
Set a coder.CodecCipher
setDecoder(org.apache.commons.codec.BinaryDecoder decoder)
Set a decoder.CodecCipher
setEncoder(org.apache.commons.codec.BinaryEncoder encoder)
Set an encoder.CodecCipher
setInit(Certificate certificate)
Set cipher initialization parameters.CodecCipher
setInit(Certificate certificate, SecureRandom random)
Set cipher initialization parameters.CodecCipher
setInit(Key key)
Set cipher initialization parameters.CodecCipher
setInit(Key key, AlgorithmParameters params)
Set cipher initialization parameters.CodecCipher
setInit(Key key, AlgorithmParameters params, SecureRandom random)
Set cipher initialization parameters.CodecCipher
setInit(Key key, SecureRandom random)
Set cipher initialization parameters.CodecCipher
setInit(Key key, AlgorithmParameterSpec spec)
Set cipher initialization parameters.CodecCipher
setInit(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. Ifnull
is 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. Ifnull
is given,Base64
is used as the default encoder.decoder
- A decoder used indecrypt(String)
anddecrypt(byte[])
to decode an encoded input byte array. Ifnull
is given,Base64
is 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. Ifnull
is given,setCipher(Cipher)
must be called later with a valid cipher.coder
- A coder which works as both an encoder and a decoder. Ifnull
is given,Base64
is 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. Ifnull
is 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. Ifnull
is given,Base64
is used as the default encoder.decoder
- A decoder used indecrypt(String)
anddecrypt(byte[])
to decode an encoded input byte array. Ifnull
is given,Base64
is used as the default decoder.- Throws:
IllegalArgumentException
- The given transformation is not supported. This exception wraps the original exception (such asNoSuchAlgorithmException
andNoSuchPaddingException
) 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. Ifnull
is given,Base64
is used as the default coder.- Throws:
IllegalArgumentException
- The given transformation is not supported. This exception wraps the original exception (such asNoSuchAlgorithmException
andNoSuchPaddingException
) 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 asNoSuchAlgorithmException
andNoSuchPaddingException
) 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. Ifnull
is given,Base64
is used as the default encoder.decoder
- A decoder used indecrypt(String)
anddecrypt(byte[])
to decode an encoded input byte array. Ifnull
is given,Base64
is used as the default decoder.- Throws:
IllegalArgumentException
- The given transformation is not supported. This exception wraps the original exception (such asNoSuchAlgorithmException
,NoSuchPaddingException
andNoSuchProviderException
) 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. Ifnull
is given,Base64
is used as the default coder.- Throws:
IllegalArgumentException
- The given transformation is not supported. This exception wraps the original exception (such asNoSuchAlgorithmException
,NoSuchPaddingException
andNoSuchProviderException
) 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
,NoSuchPaddingException
andNoSuchProviderException
) 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. Ifnull
is given,Base64
is used as the default encoder.decoder
- A decoder used indecrypt(String)
anddecrypt(byte[])
to decode an encoded input byte array. Ifnull
is given,Base64
is used as the default decoder.- Throws:
IllegalArgumentException
- The given transformation is not supported. This exception wraps the original exception (such asNoSuchAlgorithmException
andNoSuchPaddingException
) 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. Ifnull
is given,Base64
is used as the default coder.- Throws:
IllegalArgumentException
- The given transformation is not supported. This exception wraps the original exception (such asNoSuchAlgorithmException
andNoSuchPaddingException
) 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 asNoSuchAlgorithmException
andNoSuchPaddingException
) 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
Cipher
instance.
-
setCipher
public CodecCipher setCipher(Cipher cipher)
Set a cipher instance.- Parameters:
cipher
- A cipher instance.- Returns:
this
object.
-
getAlgorithm
public String getAlgorithm()
Get the cipher algorithm name.If the internal cipher is
null
,null
is 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:
this
object.
-
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:
this
object.
-
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. Ifnull
is given,Base64
is used as the default coder.- Returns:
this
object.
-
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
/decrypt
methods.- Parameters:
key
-- Returns:
this
object.- Throws:
IllegalArgumentException
-key
isnull
.
-
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
/decrypt
methods.- Parameters:
key
-random
-- Returns:
this
object.- Throws:
IllegalArgumentException
-key
isnull
.
-
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
/decrypt
methods.- Parameters:
key
-params
-- Returns:
this
object.- Throws:
IllegalArgumentException
-key
isnull
.
-
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
/decrypt
methods.- Parameters:
key
-params
-random
-- Returns:
this
object.- Throws:
IllegalArgumentException
-key
isnull
.
-
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
/decrypt
methods.- Parameters:
key
-spec
-- Returns:
this
object.- Throws:
IllegalArgumentException
-key
isnull
.
-
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
/decrypt
methods.- Parameters:
key
-spec
-random
-- Returns:
this
object.- Throws:
IllegalArgumentException
-key
isnull
.
-
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
/decrypt
methods.- Parameters:
certificate
-- Returns:
this
object.- Throws:
IllegalArgumentException
-certificate
isnull
.
-
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
/decrypt
methods.- Parameters:
certificate
-random
-- Returns:
this
object.- Throws:
IllegalArgumentException
-certificate
isnull
.
-
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
input
isnull
,null
is returned. - Throws:
IllegalStateException
- Failed to encrypt the input. TheIllegalStateException
wraps the original exception (such asInvalidKeyException
andEncoderException
) as the cause, if any.- A cipher instance has not been set.
setInit
method has not been called.- The cipher failed to encrypt the input (probably initialization
parameters set by
setInit
method 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
input
isnull
,null
is returned. - Throws:
IllegalStateException
- Failed to decrypt the input. TheIllegalStateException
wraps the original exception (such asInvalidKeyException
andDecoderException
) as the cause, if any.- A cipher instance has not been set.
setInit
method has not been called.- The decoder failed to decode the input.
- The cipher failed to decrypt the byte array (probably initialization
parameters set by
setInit
method are wrong).
-
encrypt
public byte[] encrypt(byte[] input) throws IllegalStateException
Encrypt the given byte array.- Parameters:
input
- Plain byte array before encryption.- Returns:
- Encrypted byte array encoded by the encoder.
If
input
isnull
,null
is returned. - Throws:
IllegalStateException
- Failed to encrypt the input. TheIllegalStateException
wraps the original exception (such asInvalidKeyException
andEncoderException
) as the cause, if any.- A cipher instance has not been set.
setInit
method has not been called.- The cipher failed to encrypt the input (probably initialization
parameters set by
setInit
method are wrong). - The encoder failed to encode the encrypted byte array.
-
decrypt
public byte[] decrypt(byte[] input) throws IllegalStateException
Decrypt the given byte array.- Parameters:
input
- Encrypted byte array encoded by an encoder.- Returns:
- Decrypted plain byte array.
If
input
isnull
,null
is returned. - Throws:
IllegalStateException
- Failed to decrypt the input. TheIllegalStateException
wraps the original exception (such asInvalidKeyException
andDecoderException
) as the cause, if any.- A cipher instance has not been set.
setInit
method has not been called.- The decoder failed to decode the input.
- The cipher failed to decrypt the byte array (probably initialization
parameters set by
setInit
method are wrong).
-
-