public class Cipher extends java.lang.Object implements CipherConst, Cleanable
Modifier and Type | Field and Description |
---|---|
static char |
SEPARATOR
Character, separating algorithm name, mode and padding
|
static java.lang.String |
STR_SEPARATOR
String, separating algorithm name, mode and padding
|
CBC, CFB, CNT, DECRYPT_MODE, ECB, ENCRYPT_MODE, MODES, PADDING_ANSI_X923, PADDING_ISO1012, PADDING_NO, PADDING_PKCS5, PADDING_RANDOM, PADDING_ZERO, PADDINGS, PRIVATE_KEY, PUBLIC_KEY, SECRET_KEY, UNWRAP_MODE, WRAP_MODE
Constructor and Description |
---|
Cipher(CipherInterface internal)
ctor.
|
Modifier and Type | Method and Description |
---|---|
void |
clean()
clean all important content.
|
byte[] |
doFinal()
Finishes a multiple-part encryption or decryption operation
|
byte[] |
doFinal(byte[] input)
Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.
|
int |
doFinal(byte[] output,
int outputOffset)
Finishes a multiple-part encryption or decryption operation
|
byte[] |
doFinal(byte[] input,
int inputOffset,
int inputLen)
Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.
|
int |
doFinal(byte[] input,
int inputOffset,
int inputLen,
byte[] output)
Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.
|
int |
doFinal(byte[] input,
int inputOffset,
int inputLen,
byte[] output,
int outputOffset)
Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.
|
int |
doFinal(java.nio.ByteBuffer input,
java.nio.ByteBuffer output)
Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.
|
java.lang.String |
getAlgorithm()
get algorithm name.
|
int |
getBlockSize()
Returns the block size (in bytes).
|
javax.crypto.ExemptionMechanism |
getExemptionMechanism()
Returns the exemption mechanism object used with this cipher.
|
static Cipher |
getInstance(java.lang.String algorithm)
Algorithm factory, create object by algorithm name.
|
static Cipher |
getInstance(java.lang.String algorithm,
java.security.Provider provider)
Algorithm factory, create object by algorithm name.
|
static Cipher |
getInstance(java.lang.String algorithm,
java.lang.String provider)
Algorithm factory, create object by algorithm name.
|
byte[] |
getIV()
Returns the initialization vector (IV) in a new buffer.
|
static int |
getMaxAllowedKeyLength(java.lang.String transformation)
Returns the maximum key length for the specified transformation
according to the installed JCE jurisdiction policy files.
|
static java.security.spec.AlgorithmParameterSpec |
getMaxAllowedParameterSpec(java.lang.String transformation)
Returns an AlgorithmParameterSpec object which contains
the maximum cipher parameter value according to the
jurisdiction policy file.
|
static java.lang.String |
getMode(int i) |
static int |
getMode(java.lang.String mode) |
int |
getOutputSize(int inputLen)
Returns the length in bytes that an output buffer would need to be in order to hold the result of the next
update or doFinal operation |
static java.lang.String |
getPadding(int i) |
static int |
getPadding(java.lang.String padding) |
java.security.AlgorithmParameters |
getParameters()
Returns the parameters used with this cipher.
|
java.security.Provider |
getProvider()
unused.
|
static java.lang.String[] |
getTransformation(java.lang.String algorithm) |
void |
init(int opmode,
java.security.cert.Certificate certificate)
Initializes this cipher with the public key from the given certificate.
|
void |
init(int opmode,
java.security.cert.Certificate certificate,
java.security.SecureRandom random)
Initializes this cipher with the public key from the given certificate and a source of randomness.
|
void |
init(int opmode,
java.security.Key key)
Initializes this cipher with a key.
|
void |
init(int opmode,
java.security.Key key,
java.security.AlgorithmParameters params)
Initializes this cipher with a key and a set of algorithm parameters.
|
void |
init(int opmode,
java.security.Key key,
java.security.spec.AlgorithmParameterSpec params)
Initializes this cipher with a key and a set of algorithm parameters.
|
void |
init(int opmode,
java.security.Key key,
java.security.spec.AlgorithmParameterSpec params,
java.security.SecureRandom random)
Initializes this cipher with a key, a set of algorithm parameters, and a source of randomness.
|
void |
init(int opmode,
java.security.Key key,
java.security.AlgorithmParameters params,
java.security.SecureRandom random)
Initializes this cipher with a key, a set of algorithm parameters, and a source of randomness.
|
void |
init(int opmode,
java.security.Key key,
java.security.SecureRandom random)
Initializes this cipher with a key and a source of randomness.
|
static java.lang.String[] |
splitTransformation(java.lang.String transformation)
parse string like "rt11GOST28147/CFB/NO_PADDING" to algorithm/mode/padding
|
java.security.Key |
unwrap(byte[] wrappedKey,
java.lang.String wrappedKeyAlgorithm,
int wrappedKeyType)
Unwrap a previously wrapped key.
|
byte[] |
update(byte[] input)
Continues a multiple-part encryption or decryption operation, processing another data part.
|
byte[] |
update(byte[] input,
int inputOffset,
int inputLen)
Continues a multiple-part encryption or decryption operation, processing another data part.
|
int |
update(byte[] input,
int inputOffset,
int inputLen,
byte[] output)
Continues a multiple-part encryption or decryption operation, processing another data part.
|
int |
update(byte[] input,
int inputOffset,
int inputLen,
byte[] output,
int outputOffset)
Continues a multiple-part encryption or decryption operation, processing another data part.
|
int |
update(java.nio.ByteBuffer input,
java.nio.ByteBuffer output)
Continues a multiple-part encryption or decryption operation, processing another data part.
|
byte[] |
wrap(java.security.Key key)
Wrap a key.
|
public static final char SEPARATOR
public static final java.lang.String STR_SEPARATOR
public Cipher(CipherInterface internal)
internal
- implementation.public static Cipher getInstance(java.lang.String algorithm) throws java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException
Cipher.getInstance(String)
.algorithm
- algorithm name.java.security.NoSuchAlgorithmException
- if the specified algorithm is not availablejavax.crypto.NoSuchPaddingException
- if the requested padding mechanism does not existpublic static Cipher getInstance(java.lang.String algorithm, java.lang.String provider) throws java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException, java.security.NoSuchProviderException
Cipher.getInstance(String)
.
Algorithm parameter can be set here "GOST28147/1.2.643.2.2.31.1".algorithm
- algorithm name.provider
- unusedjava.security.NoSuchAlgorithmException
- if the specified algorithm is not availablejavax.crypto.NoSuchPaddingException
- if the requested padding mechanism does not existjava.security.NoSuchProviderException
- if the specified provider is not availablepublic static Cipher getInstance(java.lang.String algorithm, java.security.Provider provider) throws java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException
Cipher.getInstance(String)
.
Algorithm parameter can be set here "GOST28147/1.2.643.2.2.31.1".algorithm
- algorithm name.provider
- unusedjava.security.NoSuchAlgorithmException
- if the specified algorithm is not availablejavax.crypto.NoSuchPaddingException
- if the requested padding mechanism does not existpublic java.security.Provider getProvider()
public java.lang.String getAlgorithm()
public int getBlockSize()
public static java.lang.String[] splitTransformation(java.lang.String transformation) throws java.security.NoSuchAlgorithmException
transformation
- string like "rt11GOST28147/CFB/NO_PADDING"java.security.NoSuchAlgorithmException
public static java.lang.String[] getTransformation(java.lang.String algorithm) throws java.security.NoSuchAlgorithmException
java.security.NoSuchAlgorithmException
public int getOutputSize(int inputLen)
update
or doFinal
operationinputLen
- the input length (in bytes)java.lang.IllegalStateException
- if this cipher is in a wrong statepublic byte[] getIV()
public java.security.AlgorithmParameters getParameters()
public javax.crypto.ExemptionMechanism getExemptionMechanism()
public void init(int opmode, java.security.Key key) throws java.security.InvalidKeyException
opmode
- the operation mode of this cipherkey
- the keyjava.security.InvalidKeyException
- if the given key is inappropriate for initializing this cipherpublic void init(int opmode, java.security.Key key, java.security.SecureRandom random) throws java.security.InvalidKeyException
opmode
- the operation mode of this cipherkey
- the encryption keyrandom
- the source of randomnessjava.security.InvalidKeyException
- if the given key is inappropriate for initializing this cipherpublic void init(int opmode, java.security.Key key, java.security.spec.AlgorithmParameterSpec params) throws java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterException
opmode
- the operation mode of this cipherkey
- the encryption keyparams
- the algorithm parametersjava.security.InvalidKeyException
- if the given key is inappropriate for initializing this cipherjava.security.InvalidAlgorithmParameterException
- if the given algorithm parameters are inappropriate for this cipherpublic void init(int opmode, java.security.Key key, java.security.spec.AlgorithmParameterSpec params, java.security.SecureRandom random) throws java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterException
opmode
- the operation mode of this cipherkey
- the encryption keyparams
- the algorithm parametersrandom
- the source of randomnessjava.security.InvalidKeyException
- if the given key is inappropriate for initializing this cipherjava.security.InvalidAlgorithmParameterException
- if the given algorithm parameters are inappropriate for this cipherpublic void init(int opmode, java.security.Key key, java.security.AlgorithmParameters params) throws java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterException
opmode
- the operation mode of this cipherkey
- the encryption keyparams
- the algorithm parametersjava.security.InvalidKeyException
- if the given key is inappropriate for initializing this cipherjava.security.InvalidAlgorithmParameterException
- if the given algorithm parameters are inappropriate for this cipherpublic void init(int opmode, java.security.Key key, java.security.AlgorithmParameters params, java.security.SecureRandom random) throws java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterException
opmode
- the operation mode of this cipherkey
- the encryption keyparams
- the algorithm parametersrandom
- the source of randomnessjava.security.InvalidKeyException
- if the given key is inappropriate for initializing this cipherjava.security.InvalidAlgorithmParameterException
- if the given algorithm parameters are inappropriate for this cipherpublic void init(int opmode, java.security.cert.Certificate certificate) throws java.security.InvalidKeyException
opmode
- the operation mode of this ciphercertificate
- the certificatejava.security.InvalidKeyException
- if the public key in the given certificate is inappropriate for initializing this cipherpublic void init(int opmode, java.security.cert.Certificate certificate, java.security.SecureRandom random) throws java.security.InvalidKeyException
opmode
- the operation mode of this ciphercertificate
- the certificaterandom
- the source of randomnessjava.security.InvalidKeyException
- if the public key in the given certificate is inappropriate for initializing this cipherpublic byte[] update(byte[] input)
input
- the input bufferjava.lang.IllegalStateException
- if this cipher is in a wrong state * (e.g., has not been initialized)public byte[] update(byte[] input, int inputOffset, int inputLen)
input
- the input bufferinputOffset
- the offset in input
where the input startsinputLen
- the input lengthjava.lang.IllegalStateException
- if this cipher is in a wrong state (e.g., has not been initialized)public int update(byte[] input, int inputOffset, int inputLen, byte[] output) throws javax.crypto.ShortBufferException
input
- the input bufferinputOffset
- the offset in input
where the input startsinputLen
- the input lengthoutput
- the buffer for the resultoutput
java.lang.IllegalStateException
- if this cipher is in a wrong state (e.g., has not been initialized)javax.crypto.ShortBufferException
- if the given output buffer is too small to hold the resultpublic int update(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws javax.crypto.ShortBufferException
input
- the input bufferinputOffset
- the offset in input
where the input startsinputLen
- the input lengthoutput
- the buffer for the resultoutputOffset
- the offset in output
where the result is storedoutput
java.lang.IllegalStateException
- if this cipher is in a wrong state (e.g., has not been initialized)javax.crypto.ShortBufferException
- if the given output buffer is too small to hold the resultpublic int update(java.nio.ByteBuffer input, java.nio.ByteBuffer output) throws javax.crypto.ShortBufferException
input
- the input ByteBufferoutput
- the output ByteBufferoutput
java.lang.IllegalStateException
- if this cipher is in a wrong state (e.g., has not been initialized)java.lang.IllegalArgumentException
- if input and output are the same objectjava.nio.ReadOnlyBufferException
- if the output buffer is read-onlyjavax.crypto.ShortBufferException
- if there is insufficient space in the output bufferpublic byte[] doFinal() throws javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingException
java.lang.IllegalStateException
- if this cipher is in a wrong state (e.g., has not been initialized)javax.crypto.IllegalBlockSizeException
- if this cipher is a block cipher,
no padding has been requested (only in encryption mode), and the total
input length of the data processed by this cipher is not a multiple of
block size; or if this encryption algorithm is unable to
process the input data provided.javax.crypto.BadPaddingException
- if this cipher is in decryption mode,
and (un)padding has been requested, but the decrypted data is not
bounded by the appropriate padding bytespublic int doFinal(byte[] output, int outputOffset) throws javax.crypto.IllegalBlockSizeException, javax.crypto.ShortBufferException, javax.crypto.BadPaddingException
output
- the buffer for the resultoutputOffset
- the offset in output
where the result is storedoutput
java.lang.IllegalStateException
- if this cipher is in a wrong state (e.g., has not been initialized)javax.crypto.IllegalBlockSizeException
- if this cipher is a block cipher,
no padding has been requested (only in encryption mode), and the total
input length of the data processed by this cipher is not a multiple of
block size; or if this encryption algorithm is unable to
process the input data provided.javax.crypto.ShortBufferException
- if the given output buffer is too small to hold the resultjavax.crypto.BadPaddingException
- if this cipher is in decryption mode,
and (un)padding has been requested, but the decrypted data is not
bounded by the appropriate padding bytespublic byte[] doFinal(byte[] input) throws javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingException
input
- the input bufferjava.lang.IllegalStateException
- if this cipher is in a wrong state (e.g., has not been initialized)javax.crypto.IllegalBlockSizeException
- if this cipher is a block cipher,
no padding has been requested (only in encryption mode), and the total
input length of the data processed by this cipher is not a multiple of
block size; or if this encryption algorithm is unable to
process the input data provided.javax.crypto.BadPaddingException
- if this cipher is in decryption mode,
and (un)padding has been requested, but the decrypted data is not
bounded by the appropriate padding bytespublic byte[] doFinal(byte[] input, int inputOffset, int inputLen) throws javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingException
input
- the input bufferinputOffset
- the offset in input
where the input startsinputLen
- the input lengthjava.lang.IllegalStateException
- if this cipher is in a wrong state (e.g., has not been initialized)javax.crypto.IllegalBlockSizeException
- if this cipher is a block cipher,
no padding has been requested (only in encryption mode), and the total
input length of the data processed by this cipher is not a multiple of
block size; or if this encryption algorithm is unable to
process the input data provided.javax.crypto.BadPaddingException
- if this cipher is in decryption mode,
and (un)padding has been requested, but the decrypted data is not
bounded by the appropriate padding bytespublic int doFinal(byte[] input, int inputOffset, int inputLen, byte[] output) throws javax.crypto.ShortBufferException, javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingException
input
- the input bufferinputOffset
- the offset in input
where the input startsinputLen
- the input lengthoutput
- the buffer for the resultoutput
java.lang.IllegalStateException
- if this cipher is in a wrong state (e.g., has not been initialized)javax.crypto.IllegalBlockSizeException
- if this cipher is a block cipher,
no padding has been requested (only in encryption mode), and the total
input length of the data processed by this cipher is not a multiple of
block size; or if this encryption algorithm is unable to
process the input data provided.javax.crypto.ShortBufferException
- if the given output buffer is too small
to hold the resultjavax.crypto.BadPaddingException
- if this cipher is in decryption mode,
and (un)padding has been requested, but the decrypted data is not
bounded by the appropriate padding bytespublic int doFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws javax.crypto.ShortBufferException, javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingException
input
- the input bufferinputOffset
- the offset in input
where the input startsinputLen
- the input lengthoutput
- the buffer for the resultoutputOffset
- the offset in output
where the result is storedoutput
java.lang.IllegalStateException
- if this cipher is in a wrong state (e.g., has not been initialized)javax.crypto.IllegalBlockSizeException
- if this cipher is a block cipher,
no padding has been requested (only in encryption mode), and the total
input length of the data processed by this cipher is not a multiple of
block size; or if this encryption algorithm is unable to
process the input data provided.javax.crypto.ShortBufferException
- if the given output buffer is too small
to hold the resultjavax.crypto.BadPaddingException
- if this cipher is in decryption mode,
and (un)padding has been requested, but the decrypted data is not
bounded by the appropriate padding bytespublic int doFinal(java.nio.ByteBuffer input, java.nio.ByteBuffer output) throws javax.crypto.ShortBufferException, javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingException
input
- the input ByteBufferoutput
- the output ByteBufferoutput
java.lang.IllegalStateException
- if this cipher is in a wrong state
(e.g., has not been initialized)java.lang.IllegalArgumentException
- if input and output are the same objectjava.nio.ReadOnlyBufferException
- if the output buffer is read-onlyjavax.crypto.IllegalBlockSizeException
- if this cipher is a block cipher,
no padding has been requested (only in encryption mode), and the total
input length of the data processed by this cipher is not a multiple of
block size; or if this encryption algorithm is unable to
process the input data provided.javax.crypto.ShortBufferException
- if there is insufficient space in the output bufferjavax.crypto.BadPaddingException
- if this cipher is in decryption mode,
and (un)padding has been requested, but the decrypted data is not
bounded by the appropriate padding bytespublic byte[] wrap(java.security.Key key) throws javax.crypto.IllegalBlockSizeException, java.security.InvalidKeyException
key
- the key to be wrapped.java.lang.IllegalStateException
- if this cipher is in a wrong state (e.g., has not been initialized).javax.crypto.IllegalBlockSizeException
- if this cipher is a block cipher, no padding has been requested,
and the length of the encoding of the key to be wrapped is not a
multiple of the block size.java.security.InvalidKeyException
- if it is impossible or unsafe to wrap the key with this cipher
(e.g., a hardware protected key is being passed to a software-only cipher).public java.security.Key unwrap(byte[] wrappedKey, java.lang.String wrappedKeyAlgorithm, int wrappedKeyType) throws java.security.InvalidKeyException, java.security.NoSuchAlgorithmException
wrappedKey
- the key to be unwrapped.wrappedKeyAlgorithm
- the algorithm associated with the wrapped key.wrappedKeyType
- the type of the wrapped key. This must be one of
SECRET_KEY
, PRIVATE_KEY
, or
PUBLIC_KEY
.java.lang.IllegalStateException
- if this cipher is in a wrong state (e.g., has not been initialized).java.security.NoSuchAlgorithmException
- if no installed providers can create keys of type wrappedKeyType
for the wrappedKeyAlgorithm
.java.security.InvalidKeyException
- if wrappedKey
does not represent a wrapped key of type
wrappedKeyType
for the wrappedKeyAlgorithm
.public static int getMaxAllowedKeyLength(java.lang.String transformation) throws java.security.NoSuchAlgorithmException
transformation
- the cipher transformation.java.lang.NullPointerException
- if transformation
is null.java.security.NoSuchAlgorithmException
- if transformation
is not a valid transformation,
i.e. in the form of "algorithm" or "algorithm/mode/padding".public static java.security.spec.AlgorithmParameterSpec getMaxAllowedParameterSpec(java.lang.String transformation) throws java.security.NoSuchAlgorithmException
transformation
- the cipher transformation.java.lang.NullPointerException
- if transformation
is null.java.security.NoSuchAlgorithmException
- if transformation
is not a valid transformation,
i.e. in the form of "algorithm" or "algorithm/mode/padding".public static int getMode(java.lang.String mode)
public static java.lang.String getMode(int i)
public static int getPadding(java.lang.String padding)
public static java.lang.String getPadding(int i)