In order to create a Cipher object, the application calls the
cipher's getInstance method, and passes the name of the
requested transformation to it. Optionally, the name of a provider
may be specified.
A transformation is a string that describes the operation (or set of operations) to be performed on the given input, to produce some output. A transformation always includes the name of a cryptographic algorithm (e.g., AES), and may be followed by a feedback mode and padding scheme.
A transformation is of the form:
(in the latter case, provider-specific default values for the mode and padding scheme are used). For example, the following is a valid transformation:
Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
Using modes such as CFB and OFB, block
ciphers can encrypt data in units smaller than the cipher's actual
block size. When requesting such a mode, you may optionally specify
the number of bits to be processed at a time by appending this number
to the mode name as shown in the "AES/CFB8/NoPadding" and
"AES/OFB32/PKCS5Padding" transformations. If no such
number is specified, a provider-specific default is used.
(See the
JDK Providers Documentation
for the JDK Providers default values.)
Thus, block ciphers can be turned into byte-oriented stream ciphers by
using an 8 bit mode such as CFB8 or OFB8.
Modes such as Authenticated Encryption with Associated Data (AEAD)
provide authenticity assurances for both confidential data and
Additional Associated Data (AAD) that is not encrypted. (Please see
RFC 5116 for more
information on AEAD and AAD algorithms.) Both
confidential and AAD data can be used when calculating the
authentication tag (similar to a Mac). This tag is appended
to the ciphertext during encryption, and is verified on decryption.
AEAD modes perform all AAD authenticity calculations
before starting the ciphertext authenticity calculations. To avoid
implementations having to internally buffer ciphertext, all AAD data
must be supplied to their implementations (via the updateAAD
methods) before the ciphertext is processed (via
the update and doFinal methods).
When a doFinal method completes the operation, the Cipher object will attempt
to reset the state to the most recent call to init, allowing for additional
operations. A successful reset depends on the mode (ENCRYPT_MODE or
DECRYPT_MODE) and the algorithm. AEAD algorithms may not reset, in order to prevent
forgery attacks due to Key and IV uniqueness requirements.
An IllegalStateException will be thrown when calling update
or doFinal methods if a reset did not occur. A call to init will
re-initialize the Cipher object with new parameters.
Every implementation of the Java platform is required to support
the following standard Cipher object transformations with
the keysizes in parentheses:
AES/CBC/NoPadding (128)AES/CBC/PKCS5Padding (128)AES/ECB/NoPadding (128)AES/ECB/PKCS5Padding (128)AES/GCM/NoPadding (128, 256)ChaCha20-Poly1305PBEWithHmacSHA256AndAES_128PBEWithHmacSHA256AndAES_256RSA/ECB/OAEPWithSHA-1AndMGF1Padding (1024, 2048)RSA/ECB/OAEPWithSHA-256AndMGF1Padding (1024, 2048)KeyGenerator, SecretKey
| Modifier and Type | Class and Description |
|---|---|
| private static class | Cipher.
Nested class to deal with modes and paddings. |
| Modifier and Type | Field and Description |
|---|---|
| private static final String | |
| private static final String | |
| private CryptoPermission | |
| private static final Debug | |
| public static final int | DECRYPT_MODE
Constant used to initialize cipher to decryption mode. |
| public static final int | ENCRYPT_MODE
Constant used to initialize cipher to encryption mode. |
| private ExemptionMechanism | |
| private Provider. | |
| private CipherSpi | |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final int | |
| private boolean | |
| private final Object | |
| private int | |
| private static final Debug | |
| public static final int | PRIVATE_KEY
Constant used to indicate the to-be-unwrapped key is a "private key". |
| private Provider | |
| public static final int | PUBLIC_KEY
Constant used to indicate the to-be-unwrapped key is a "public key". |
| private static final int | |
| private static final int | |
| private static final int | |
| public static final int | SECRET_KEY
Constant used to indicate the to-be-unwrapped key is a "secret key". |
| private Iterator | |
| private static final boolean | |
| private CipherSpi | |
| private final String | |
| private List | |
| public static final int | UNWRAP_MODE
Constant used to initialize cipher to key-unwrapping mode. |
| private static int | |
| public static final int | WRAP_MODE
Constant used to initialize cipher to key-wrapping mode. |
| Access | Constructor and Description |
|---|---|
| protected | |
| pack-priv | |
| private |
| Modifier and Type | Method and Description |
|---|---|
| private void | checkCipherState()
Ensures that |
| private void | |
| private void | |
| private void | |
| private static void | |
| pack-priv void | |
| private void | chooseProvider(int initType, int opmode, Key key, AlgorithmParameterSpec paramSpec, AlgorithmParameters params, SecureRandom random)
|
| public final byte[] | Returns: the new buffer with the resultFinishes a multiple-part encryption or decryption operation, depending
on how this |
| public final int | Returns: the number of bytes stored inoutputthe buffer for the result output, int the offset in outputOffset)output where the result
is storedFinishes a multiple-part encryption or decryption operation, depending
on how this |
| public final byte[] | Returns: the new buffer with the resultthe input buffer input)Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation. |
| public final byte[] | Returns: the new buffer with the resultthe input buffer input, int the offset in inputOffset, int input where the input
startsthe input length inputLen)Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation. |
| public final int | Returns: the number of bytes stored inoutputthe input buffer input, int the offset in inputOffset, int input where the input
startsthe input length inputLen, byte[] the buffer for the result output)Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation. |
| public final int | Returns: the number of bytes stored inoutputthe input buffer input, int the offset in inputOffset, int input where the input
startsthe input length inputLen, byte[] the buffer for the result output, int the offset in outputOffset)output where the result
is storedEncrypts or decrypts data in a single-part operation, or finishes a multiple-part operation. |
| public final int | Returns: the number of bytes stored inoutputthe input ByteBuffer input, ByteBuffer the output ByteBuffer output)Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation. |
| public final String | Returns: the algorithm name of thisCipher objectReturns the algorithm name of this |
| private AlgorithmParameterSpec | |
| public final int | Returns: the block size (in bytes), or 0 if this cipher is not a block cipherReturns the block size (in bytes). |
| private static CryptoPermission | |
| public final ExemptionMechanism | Returns: the exemption mechanism object used with thisCipher
object, or null if this Cipher object does not use any
exemption mechanism.Returns the exemption mechanism object used with this |
| public static final Cipher | Returns: aCipher object that implements the requested
transformationthe name of the transformation, e.g.,
AES/CBC/PKCS5Padding.
See the Cipher section in the
Java Security Standard Algorithm Names Specification
for information about standard transformation names. transformation)Returns a |
| public static final Cipher | Returns: aCipher object that implements the requested
transformationthe name of the transformation,
e.g., AES/CBC/PKCS5Padding.
See the Cipher section in the
Java Security Standard Algorithm Names Specification
for information about standard transformation names. transformation, String the name of the provider provider)Returns a |
| public static final Cipher | Returns: aCipher object that implements the requested
transformationthe name of the transformation,
e.g., AES/CBC/PKCS5Padding.
See the Cipher section in the
Java Security Standard Algorithm Names Specification
for information about standard transformation names. transformation, Provider the provider provider)Returns a |
| public final byte[] | Returns: the initialization vector in a new buffer, ornull if
this cipher does not use an IV, or if the IV has not yet
been set.Returns the initialization vector (IV) in a new buffer. |
| public static final int | Returns: the maximum key length in bits orInteger.MAX_VALUEthe cipher transformation transformation)Returns the maximum key length for the specified transformation according to the installed JCE jurisdiction policy files. |
| public static final AlgorithmParameterSpec | Returns: anAlgorithmParameterSpec object which holds the maximum
value or nullthe cipher transformation transformation)Returns an |
| public final int | Returns: the required output buffer size (in bytes)the input length (in bytes) inputLen)Returns the length in bytes that an output buffer would need to be in
order to hold the result of the next |
| public final AlgorithmParameters | Returns: the parameters used with this cipher, ornullReturns the parameters used with this |
| public final Provider | |
| private String | |
| private static Cipher. | |
| private static List | |
| private void | implInit(CipherSpi thisSpi, int type, int opmode, Key key, AlgorithmParameterSpec paramSpec, AlgorithmParameters params, SecureRandom random)
|
| pack-priv static int | |
| public final void | |
| public final void | init(int
the operation mode of this opmode, Key Cipher object
(this is one of the following:
ENCRYPT_MODE, DECRYPT_MODE,
WRAP_MODE or UNWRAP_MODE)the encryption key key, SecureRandom the source of randomness random)Initializes this |
| public final void | init(int
the operation mode of this opmode, Key Cipher object
(this is one of the following:
ENCRYPT_MODE, DECRYPT_MODE,
WRAP_MODE or UNWRAP_MODE)the encryption key key, AlgorithmParameterSpec the algorithm parameters params)Initializes this |
| public final void | init(int
the operation mode of this opmode, Key Cipher object
(this is one of the following:
ENCRYPT_MODE, DECRYPT_MODE,
WRAP_MODE or UNWRAP_MODE)the encryption key key, AlgorithmParameterSpec the algorithm parameters params, SecureRandom the source of randomness random)Initializes this |
| public final void | init(int
the operation mode of this opmode, Key Cipher object
this is one of the following: ENCRYPT_MODE,
DECRYPT_MODE, WRAP_MODE
or UNWRAP_MODE)the encryption key key, AlgorithmParameters the algorithm parameters params)Initializes this |
| public final void | init(int
the operation mode of this opmode, Key Cipher object
(this is one of the following: ENCRYPT_MODE,
DECRYPT_MODE, WRAP_MODE
or UNWRAP_MODE)the encryption key key, AlgorithmParameters the algorithm parameters params, SecureRandom the source of randomness random)Initializes this |
| public final void | init(int
the operation mode of this opmode, Certificate Cipher object
(this is one of the following:
ENCRYPT_MODE, DECRYPT_MODE,
WRAP_MODE or UNWRAP_MODE)the certificate certificate)Initializes this |
| public final void | init(int
the operation mode of this opmode, Certificate Cipher object
(this is one of the following:
ENCRYPT_MODE, DECRYPT_MODE,
WRAP_MODE or UNWRAP_MODE)the certificate certificate, SecureRandom the source of randomness random)Initializes this |
| private void | |
| private boolean | |
| pack-priv static String | |
| private static String[] | |
| public String | Returns: a String describing thisCipher objectOverrides java. Returns a |
| public final Key | |
| public final byte[] | Returns: the new buffer with the result, ornull if this
cipher is a block cipher and the input data is too short to result in a
new blockthe input buffer input)Continues a multiple-part encryption or decryption operation
(depending on how this |
| public final byte[] | Returns: the new buffer with the result, ornull if this
cipher is a block cipher and the input data is too short to result in a
new block.the input buffer input, int the offset in inputOffset, int input where the input
startsthe input length inputLen)Continues a multiple-part encryption or decryption operation
(depending on how this |
| public final int | Returns: the number of bytes stored inoutputthe input buffer input, int the offset in inputOffset, int input where the input
startsthe input length inputLen, byte[] the buffer for the result output)Continues a multiple-part encryption or decryption operation
(depending on how this |
| public final int | Returns: the number of bytes stored inoutputthe input buffer input, int the offset in inputOffset, int input where the input
startsthe input length inputLen, byte[] the buffer for the result output, int the offset in outputOffset)output where the result
is storedContinues a multiple-part encryption or decryption operation
(depending on how this |
| public final int | Returns: the number of bytes stored inoutputthe input ByteBuffer input, ByteBuffer the output ByteByffer output)Continues a multiple-part encryption or decryption operation
(depending on how this |
| public final void | updateAAD(byte[]
the buffer containing the Additional Authentication Data src)Continues a multi-part update of the Additional Authentication Data (AAD). |
| public final void | updateAAD(byte[]
the buffer containing the AAD src, int the offset in offset, int src where the AAD input startsthe number of AAD bytes len)Continues a multi-part update of the Additional Authentication Data (AAD), using a subset of the provided buffer. |
| public final void | updateAAD(ByteBuffer
the buffer containing the AAD src)Continues a multi-part update of the Additional Authentication Data (AAD). |
| public final byte[] |