Cipher (Java SE 15 & JDK 15) (original) (raw)

Direct Known Subclasses:

[NullCipher](NullCipher.html "class in javax.crypto")


public class Cipher extends Object

This class provides the functionality of a cryptographic cipher for encryption and decryption. It forms the core of the Java Cryptographic Extension (JCE) framework.

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 theJDK 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 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 transformations with the keysizes in parentheses:

These transformations are described in the Cipher section of the Java Security Standard Algorithm Names Specification. Consult the release documentation for your implementation to see if any other transformations are supported.

Since:

1.4

See Also:

KeyGenerator, SecretKey

Fields

Modifier and Type Field Description
static int DECRYPT_MODE Constant used to initialize cipher to decryption mode.
static int ENCRYPT_MODE Constant used to initialize cipher to encryption mode.
static int PRIVATE_KEY Constant used to indicate the to-be-unwrapped key is a "private key".
static int PUBLIC_KEY Constant used to indicate the to-be-unwrapped key is a "public key".
static int SECRET_KEY Constant used to indicate the to-be-unwrapped key is a "secret key".
static int UNWRAP_MODE Constant used to initialize cipher to key-unwrapping mode.
static int WRAP_MODE Constant used to initialize cipher to key-wrapping mode.

Constructors

Modifier Constructor Description
protected Cipher​(CipherSpi cipherSpi,Provider provider,String transformation) Creates a Cipher object.
Modifier and Type Method Description
byte[] doFinal() Finishes a multiple-part encryption or decryption operation, depending on how this cipher was initialized.
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, depending on how this cipher was initialized.
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​(ByteBuffer input,ByteBuffer output) Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.
String getAlgorithm() Returns the algorithm name of this Cipher object.
int getBlockSize() Returns the block size (in bytes).
ExemptionMechanism getExemptionMechanism() Returns the exemption mechanism object used with this cipher.
static Cipher getInstance​(String transformation) Returns a Cipher object that implements the specified transformation.
static Cipher getInstance​(String transformation,String provider) Returns a Cipher object that implements the specified transformation.
static Cipher getInstance​(String transformation,Provider provider) Returns a Cipher object that implements the specified transformation.
byte[] getIV() Returns the initialization vector (IV) in a new buffer.
static int getMaxAllowedKeyLength​(String transformation) Returns the maximum key length for the specified transformation according to the installed JCE jurisdiction policy files.
static AlgorithmParameterSpec getMaxAllowedParameterSpec​(String transformation) Returns an AlgorithmParameterSpec object which contains the maximum cipher parameter value according to the jurisdiction policy file.
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 ordoFinal operation, given the input lengthinputLen (in bytes).
AlgorithmParameters getParameters() Returns the parameters used with this cipher.
Provider getProvider() Returns the provider of this Cipher object.
void init​(int opmode,Certificate certificate) Initializes this cipher with the public key from the given certificate.
void init​(int opmode,Certificate certificate,SecureRandom random) Initializes this cipher with the public key from the given certificate and a source of randomness.
void init​(int opmode,Key key) Initializes this cipher with a key.
void init​(int opmode,Key key,AlgorithmParameters params) Initializes this cipher with a key and a set of algorithm parameters.
void init​(int opmode,Key key,AlgorithmParameters params,SecureRandom random) Initializes this cipher with a key, a set of algorithm parameters, and a source of randomness.
void init​(int opmode,Key key,SecureRandom random) Initializes this cipher with a key and a source of randomness.
void init​(int opmode,Key key,AlgorithmParameterSpec params) Initializes this cipher with a key and a set of algorithm parameters.
void init​(int opmode,Key key,AlgorithmParameterSpec params,SecureRandom random) Initializes this cipher with a key, a set of algorithm parameters, and a source of randomness.
String toString() Returns a String representation of this Cipher.
Key unwrap​(byte[] wrappedKey,String wrappedKeyAlgorithm, int wrappedKeyType) Unwrap a previously wrapped key.
byte[] update​(byte[] input) Continues a multiple-part encryption or decryption operation (depending on how this cipher was initialized), processing another data part.
byte[] update​(byte[] input, int inputOffset, int inputLen) Continues a multiple-part encryption or decryption operation (depending on how this cipher was initialized), processing another data part.
int update​(byte[] input, int inputOffset, int inputLen, byte[] output) Continues a multiple-part encryption or decryption operation (depending on how this cipher was initialized), processing another data part.
int update​(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) Continues a multiple-part encryption or decryption operation (depending on how this cipher was initialized), processing another data part.
int update​(ByteBuffer input,ByteBuffer output) Continues a multiple-part encryption or decryption operation (depending on how this cipher was initialized), processing another data part.
void updateAAD​(byte[] src) Continues a multi-part update of the Additional Authentication Data (AAD).
void updateAAD​(byte[] src, int offset, int len) Continues a multi-part update of the Additional Authentication Data (AAD), using a subset of the provided buffer.
void updateAAD​(ByteBuffer src) Continues a multi-part update of the Additional Authentication Data (AAD).
byte[] wrap​(Key key) Wrap a key.