- Direct Known Subclasses:
NullCipher
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:
- "algorithm/mode/padding" or
- "algorithm"
(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 Moved out of a link with destination https://docs.oracle.com/pls/topic/lookup?ctx=javase20&id=security_guide_jdk_providers.Moved to a link with destination https://docs.oracle.com/pls/topic/lookup?ctx=javase21&id=security_guide_jdk_providers.JDK Providers DocumentationMoved out of a link with destination https://docs.oracle.com/pls/topic/lookup?ctx=javase20&id=security_guide_jdk_providers.Moved to a link with destination https://docs.oracle.com/pls/topic/lookup?ctx=javase21&id=security_guide_jdk_providers. 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 such as GCM/CCM.) 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 such as GCM/CCM 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 GCM/CCM implementations (via the updateAAD
methods) before the ciphertext is processed (via the update
and doFinal
methods).
Note that GCM mode has a uniqueness requirement on IVs used in encryption with a given key. When IVs are repeated for GCM encryption, such usages are subject to forgery attacks. Thus, after each encryption operation using GCM mode, callers should re-initialize the Cipher
objects with GCM parameters which have a different IV value.
GCMParameterSpec s = ...; cipher.init(..., s); // If the GCM parameters were generated by the provider, it can // be retrieved by: // cipher.getParameters().getParameterSpec(GCMParameterSpec.class); cipher.updateAAD(...); // AAD cipher.update(...); // Multi-part update cipher.doFinal(...); // conclusion of operation // Use a different IV value for every encryption byte[] newIv = ...; s = new GCMParameterSpec(s.getTLen(), newIv); cipher.init(..., s); ...The ChaCha20 and ChaCha20-Poly1305 algorithms have a similar requirement for unique nonces with a given key. After each encryption or decryption operation, callers should re-initialize their ChaCha20 or ChaCha20-Poly1305 ciphers with parameters that specify a different nonce value. Please see RFC 7539 for more information on the ChaCha20 and ChaCha20-Poly1305 algorithms.
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)DESede/CBC/NoPadding
(168)DESede/CBC/PKCS5Padding
(168)DESede/ECB/NoPadding
(168)DESede/ECB/PKCS5Padding
(168)RSA/ECB/PKCS1Padding
(1024, 2048)RSA/ECB/OAEPWithSHA-1AndMGF1Padding
(1024, 2048)RSA/ECB/OAEPWithSHA-256AndMGF1Padding
(1024, 2048)
- Since:
- 1.4
- See Also:
Fields
- ✓public static final int DECRYPT_MODE = 2
- ✓public static final int ENCRYPT_MODE = 1
- ✓public static final int PRIVATE_KEY = 2
- ✓public static final int PUBLIC_KEY = 1
- ✓public static final int SECRET_KEY = 3
- ✓public static final int UNWRAP_MODE = 4
- ✓public static final int WRAP_MODE = 3
Constructors
- ✓protected Cipher(javax.crypto.CipherSpi arg0, java.security.Provider arg1, java.lang.String arg2)
Methods
- ✓public final byte[] doFinal() throws javax.crypto.BadPaddingException, javax.crypto.IllegalBlockSizeException
- ✓public final byte[] doFinal(byte[] arg0) throws javax.crypto.BadPaddingException, javax.crypto.IllegalBlockSizeException
- ✓public final int doFinal(byte[] arg0, int arg1) throws javax.crypto.BadPaddingException, javax.crypto.IllegalBlockSizeException, javax.crypto.ShortBufferException
- ✓public final byte[] doFinal(byte[] arg0, int arg1, int arg2) throws javax.crypto.BadPaddingException, javax.crypto.IllegalBlockSizeException
- ✓public final int doFinal(byte[] arg0, int arg1, int arg2, byte[] arg3) throws javax.crypto.BadPaddingException, javax.crypto.IllegalBlockSizeException, javax.crypto.ShortBufferException
- ✓public final int doFinal(byte[] arg0, int arg1, int arg2, byte[] arg3, int arg4) throws javax.crypto.BadPaddingException, javax.crypto.IllegalBlockSizeException, javax.crypto.ShortBufferException
- ✓public final int doFinal(java.nio.ByteBuffer arg0, java.nio.ByteBuffer arg1) throws javax.crypto.BadPaddingException, javax.crypto.IllegalBlockSizeException, javax.crypto.ShortBufferException
- ✓public final java.lang.String getAlgorithm()
- ✓public final int getBlockSize()
- ✓public final javax.crypto.ExemptionMechanism getExemptionMechanism()
- ✓public final byte[] getIV()
- ✓public static final javax.crypto.Cipher getInstance(java.lang.String arg0) throws java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException
- ✗public static final javax.crypto.Cipher getInstance(java.lang.String arg0, java.lang.String arg1) throws java.security.NoSuchAlgorithmException, java.security.NoSuchProviderException, javax.crypto.NoSuchPaddingExceptionComparing jdk-20-ga and jdk-21+35
getInstance
public static final Cipher getInstance(String transformation, String provider) throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException Returns aCipher
object that implements the specified transformation.A new
Cipher
object encapsulating theCipherSpi
implementation from the specified provider is returned. The specified provider must be registered in the security provider list.Note that the list of registered providers may be retrieved via the
Security.getProviders()
method.- API Note:
- It is recommended to use a transformation that fully specifies the algorithm, mode, and padding. By not doing so, the provider will use a default for the mode and padding which may not meet the security requirements of your application.
- Implementation Note:
- See the Cipher Transformations section of the Moved out of a link with destination https://docs.oracle.com/pls/topic/lookup?ctx=javase20&id=security_guide_jdk_providers.Moved to a link with destination https://docs.oracle.com/pls/topic/lookup?ctx=javase21&id=security_guide_jdk_providers.JDK ProvidersMoved out of a link with destination https://docs.oracle.com/pls/topic/lookup?ctx=javase20&id=security_guide_jdk_providers.Moved to a link with destination https://docs.oracle.com/pls/topic/lookup?ctx=javase21&id=security_guide_jdk_providers. document for information on the transformation defaults used by JDK providers.
- Parameters:
transformation
- the 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.provider
- the name of the provider- Returns:
- a
Cipher
object that implements the requested transformation - Throws:
IllegalArgumentException
- if theprovider
isnull
or emptyNoSuchAlgorithmException
- iftransformation
isnull
, empty, in an invalid format, or if aCipherSpi
implementation for the specified algorithm is not available from the specified providerNoSuchPaddingException
- iftransformation
contains a padding scheme that is not availableNoSuchProviderException
- if the specified provider is not registered in the security provider list- See Also:
- ✗public static final javax.crypto.Cipher getInstance(java.lang.String arg0, java.security.Provider arg1) throws java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingExceptionComparing jdk-20-ga and jdk-21+35
getInstance
public static final Cipher getInstance(String transformation, Provider provider) throws NoSuchAlgorithmException, NoSuchPaddingException Returns aCipher
object that implements the specified transformation.A new
Cipher
object encapsulating theCipherSpi
implementation from the specifiedprovider
object is returned. Note that the specifiedprovider
object does not have to be registered in the provider list.- API Note:
- It is recommended to use a transformation that fully specifies the algorithm, mode, and padding. By not doing so, the provider will use a default for the mode and padding which may not meet the security requirements of your application.
- Implementation Note:
- See the Cipher Transformations section of the Moved out of a link with destination https://docs.oracle.com/pls/topic/lookup?ctx=javase20&id=security_guide_jdk_providers.Moved to a link with destination https://docs.oracle.com/pls/topic/lookup?ctx=javase21&id=security_guide_jdk_providers.JDK ProvidersMoved out of a link with destination https://docs.oracle.com/pls/topic/lookup?ctx=javase20&id=security_guide_jdk_providers.Moved to a link with destination https://docs.oracle.com/pls/topic/lookup?ctx=javase21&id=security_guide_jdk_providers. document for information on the transformation defaults used by JDK providers.
- Parameters:
transformation
- the 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.provider
- the provider- Returns:
- a
Cipher
object that implements the requested transformation - Throws:
IllegalArgumentException
- if theprovider
isnull
NoSuchAlgorithmException
- iftransformation
isnull
, empty, in an invalid format, or if aCipherSpi
implementation for the specified algorithm is not available from the specifiedprovider
objectNoSuchPaddingException
- iftransformation
contains a padding scheme that is not available- See Also:
- ✗public static final int getMaxAllowedKeyLength(java.lang.String arg0) throws java.security.NoSuchAlgorithmExceptionComparing jdk-20-ga and jdk-21+35
getMaxAllowedKeyLength
public static final int getMaxAllowedKeyLength(String transformation) throws NoSuchAlgorithmException Returns the maximum key length for the specified transformation according to the installed JCE jurisdiction policy files. If JCE unlimited strength jurisdiction policy files are installed,Integer.MAX_VALUE
will be returned. For more information on the default key sizes and the JCE jurisdiction policy files, please see the Cryptographic defaults and limitations in the Moved out of a link with destination https://docs.oracle.com/pls/topic/lookup?ctx=javase20&id=security_guide_jdk_providers.Moved to a link with destination https://docs.oracle.com/pls/topic/lookup?ctx=javase21&id=security_guide_jdk_providers.JDK Providers Documentation.- Parameters:
transformation
- the cipher transformation- Returns:
- the maximum key length in bits or
Integer.MAX_VALUE
- Throws:
NullPointerException
- iftransformation
isnull
NoSuchAlgorithmException
- iftransformation
is not a valid transformation, i.e. in the form of "algorithm" or "algorithm/mode/padding"- Since:
- 1.5
- ✓public static final java.security.spec.AlgorithmParameterSpec getMaxAllowedParameterSpec(java.lang.String arg0) throws java.security.NoSuchAlgorithmException
- ✓public final int getOutputSize(int arg0)
- ✓public final java.security.AlgorithmParameters getParameters()
- ✓public final java.security.Provider getProvider()
- ✓public final void init(int arg0, java.security.Key arg1) throws java.security.InvalidKeyException
- ✓public final void init(int arg0, java.security.Key arg1, java.security.AlgorithmParameters arg2) throws java.security.InvalidAlgorithmParameterException, java.security.InvalidKeyException
- ✓public final void init(int arg0, java.security.Key arg1, java.security.AlgorithmParameters arg2, java.security.SecureRandom arg3) throws java.security.InvalidAlgorithmParameterException, java.security.InvalidKeyException
- ✓public final void init(int arg0, java.security.Key arg1, java.security.SecureRandom arg2) throws java.security.InvalidKeyException
- ✓public final void init(int arg0, java.security.Key arg1, java.security.spec.AlgorithmParameterSpec arg2) throws java.security.InvalidAlgorithmParameterException, java.security.InvalidKeyException
- ✓public final void init(int arg0, java.security.Key arg1, java.security.spec.AlgorithmParameterSpec arg2, java.security.SecureRandom arg3) throws java.security.InvalidAlgorithmParameterException, java.security.InvalidKeyException
- ✓public final void init(int arg0, java.security.cert.Certificate arg1) throws java.security.InvalidKeyException
- ✓public final void init(int arg0, java.security.cert.Certificate arg1, java.security.SecureRandom arg2) throws java.security.InvalidKeyException
- ✓public java.lang.String toString()
- ✓public final java.security.Key unwrap(byte[] arg0, java.lang.String arg1, int arg2) throws java.security.InvalidKeyException, java.security.NoSuchAlgorithmException
- ✓public final byte[] update(byte[] arg0)
- ✓public final byte[] update(byte[] arg0, int arg1, int arg2)
- ✓public final int update(byte[] arg0, int arg1, int arg2, byte[] arg3) throws javax.crypto.ShortBufferException
- ✓public final int update(byte[] arg0, int arg1, int arg2, byte[] arg3, int arg4) throws javax.crypto.ShortBufferException
- ✓public final int update(java.nio.ByteBuffer arg0, java.nio.ByteBuffer arg1) throws javax.crypto.ShortBufferException
- ✓public final void updateAAD(byte[] arg0)
- ✓public final void updateAAD(byte[] arg0, int arg1, int arg2)
- ✓public final void updateAAD(java.nio.ByteBuffer arg0)
- ✓public final byte[] wrap(java.security.Key arg0) throws java.security.InvalidKeyException, javax.crypto.IllegalBlockSizeException
Summary
Elements | Comments | Descriptions | Total | |||||||
---|---|---|---|---|---|---|---|---|---|---|
Added | Changed | Removed | Added | Changed | Removed | Added | Changed | Removed | ||
Cipher | 2 | 2 | ||||||||
getInstance(String,String) | 2 | 2 | ||||||||
getInstance(String,Provider) | 2 | 2 | ||||||||
getMaxAllowedKeyLength(String) | 1 | 1 | ||||||||
Total | 7 | 7 |