Skip to content

Most visited

Recently visited

navigation
Added in API level 1

Cipher

public class Cipher
extends Object

java.lang.Object
   ↳ javax.crypto.Cipher
Known Direct Subclasses


This class provides access to implementations of cryptographic ciphers for encryption and decryption. Cipher classes can not be instantiated directly, one has to call the Cipher's getInstance method with the name of a requested transformation, optionally with a provider. A transformation specifies an operation (or a set of operations) as a string in the form:

algorithm is the name of a cryptographic algorithm, mode is the name of a feedback mode and padding is the name of a padding scheme. If mode and/or padding values are omitted, provider specific default values will be used.

A valid transformation would be:

When a block cipher is requested in stream cipher mode, the number of bits to be processed at a time can be optionally specified by appending it to the mode name. e.g. "AES/CFB8/NoPadding". If no number is specified, a provider specific default value is used.

Summary

Constants

int DECRYPT_MODE

Constant for decryption operation mode.

int ENCRYPT_MODE

Constant for encryption operation mode.

int PRIVATE_KEY

Constant indicating that the key to be unwrapped is a private key.

int PUBLIC_KEY

Constant indicating that the key to be unwrapped is a public key.

int SECRET_KEY

Constant indicating that the key to be unwrapped is a secret key.

int UNWRAP_MODE

Constant for key unwrapping operation mode.

int WRAP_MODE

Constant for key wrapping operation mode.

Protected constructors

Cipher(CipherSpi cipherSpi, Provider provider, String transformation)

Creates a new Cipher instance.

Public methods

final int doFinal(byte[] output, int outputOffset)

Finishes a multi-part transformation (encryption or decryption).

final byte[] doFinal(byte[] input, int inputOffset, int inputLen)

Finishes a multi-part transformation (encryption or decryption).

final int doFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset)

Finishes a multi-part transformation (encryption or decryption).

final int doFinal(byte[] input, int inputOffset, int inputLen, byte[] output)

Finishes a multi-part transformation (encryption or decryption).

final byte[] doFinal(byte[] input)

Finishes a multi-part transformation (encryption or decryption).

final int doFinal(ByteBuffer input, ByteBuffer output)

Finishes a multi-part transformation (encryption or decryption).

final byte[] doFinal()

Finishes a multi-part transformation (encryption or decryption).

final String getAlgorithm()

Returns the name of the algorithm of this cipher instance.

final int getBlockSize()

Returns this ciphers block size (in bytes).

final ExemptionMechanism getExemptionMechanism()

Returns the exemption mechanism associated with this cipher.

final byte[] getIV()

Returns the initialization vector for this cipher instance.

final static Cipher getInstance(String transformation, String provider)

Creates a new cipher for the specified transformation provided by the specified provider.

final static Cipher getInstance(String transformation, Provider provider)

Creates a new cipher for the specified transformation.

final static Cipher getInstance(String transformation)

Creates a new Cipher for the specified transformation.

final static int getMaxAllowedKeyLength(String transformation)

Returns the maximum key length for the specified transformation.

final static AlgorithmParameterSpec getMaxAllowedParameterSpec(String transformation)

Returns the maximum cipher parameter value for the specified transformation.

final int getOutputSize(int inputLen)

Returns the length in bytes an output buffer needs to be when this cipher is updated with inputLen bytes.

final AlgorithmParameters getParameters()

Returns the parameters that where used to create this cipher instance.

final Provider getProvider()

Returns the provider of this cipher instance.

final void init(int opmode, Key key)

Initializes this cipher instance with the specified key.

final void init(int opmode, Key key, SecureRandom random)

Initializes this cipher instance with the specified key and a source of randomness.

final void init(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random)

Initializes this cipher instance with the specified key, algorithm parameters and a source of randomness.

final void init(int opmode, Key key, AlgorithmParameters params, SecureRandom random)

Initializes this cipher instance with the specified key, algorithm parameters and a source of randomness.

final void init(int opmode, Key key, AlgorithmParameterSpec params)

Initializes this cipher instance with the specified key and algorithm parameters.

final void init(int opmode, Certificate certificate, SecureRandom random)

Initializes this cipher instance with the public key from the specified certificate and a source of randomness.

final void init(int opmode, Certificate certificate)

Initializes this cipher instance with the public key from the specified certificate.

final void init(int opmode, Key key, AlgorithmParameters params)

Initializes this cipher instance with the specified key and algorithm parameters.

final Key unwrap(byte[] wrappedKey, String wrappedKeyAlgorithm, int wrappedKeyType)

Unwraps a key using this cipher instance.

final int update(ByteBuffer input, ByteBuffer output)

Continues a multi-part transformation (encryption or decryption).

final byte[] update(byte[] input)

Continues a multi-part transformation (encryption or decryption).

final int update(byte[] input, int inputOffset, int inputLen, byte[] output)

Continues a multi-part transformation (encryption or decryption).

final byte[] update(byte[] input, int inputOffset, int inputLen)

Continues a multi-part transformation (encryption or decryption).

final int update(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset)

Continues a multi-part transformation (encryption or decryption).

final void updateAAD(ByteBuffer input)

Continues a multi-part transformation (encryption or decryption) with Authenticated Additional Data (AAD).

final void updateAAD(byte[] input, int inputOffset, int inputLen)

Continues a multi-part transformation (encryption or decryption) with Authenticated Additional Data (AAD).

final void updateAAD(byte[] input)

Continues a multi-part transformation (encryption or decryption) with Authenticated Additional Data (AAD).

final byte[] wrap(Key key)

Wraps a key using this cipher instance.

Inherited methods

From class java.lang.Object

Constants

DECRYPT_MODE

Added in API level 1
int DECRYPT_MODE

Constant for decryption operation mode.

Constant Value: 2 (0x00000002)

ENCRYPT_MODE

Added in API level 1
int ENCRYPT_MODE

Constant for encryption operation mode.

Constant Value: 1 (0x00000001)

PRIVATE_KEY

Added in API level 1
int PRIVATE_KEY

Constant indicating that the key to be unwrapped is a private key.

Constant Value: 2 (0x00000002)

PUBLIC_KEY

Added in API level 1
int PUBLIC_KEY

Constant indicating that the key to be unwrapped is a public key.

Constant Value: 1 (0x00000001)

SECRET_KEY

Added in API level 1
int SECRET_KEY

Constant indicating that the key to be unwrapped is a secret key.

Constant Value: 3 (0x00000003)

UNWRAP_MODE

Added in API level 1
int UNWRAP_MODE

Constant for key unwrapping operation mode.

Constant Value: 4 (0x00000004)

WRAP_MODE

Added in API level 1
int WRAP_MODE

Constant for key wrapping operation mode.

Constant Value: 3 (0x00000003)

Protected constructors

Cipher

Added in API level 1
Cipher (CipherSpi cipherSpi, 
                Provider provider, 
                String transformation)

Creates a new Cipher instance.

Parameters
cipherSpi CipherSpi: the implementation delegate of the cipher.
provider Provider: the provider of the implementation of this cipher.
transformation String: the name of the transformation that this cipher performs.
Throws
NullPointerException if either cipherSpi is null or provider is null and cipherSpi is a NullCipherSpi.

Public methods

doFinal

Added in API level 1
int doFinal (byte[] output, 
                int outputOffset)

Finishes a multi-part transformation (encryption or decryption).

Processes any bytes that may have been buffered in previous update calls.

The final transformed bytes are stored in the output buffer.

Parameters
output byte: the output buffer.
outputOffset int: the offset in the output buffer.
Returns
int the number of bytes placed in the output buffer.
Throws
IllegalBlockSizeException if the size of the resulting bytes is not a multiple of the cipher block size.
ShortBufferException if the size of the output buffer is too small.
BadPaddingException if the padding of the data does not match the padding scheme.
IllegalStateException if this cipher instance is not initialized for encryption or decryption.

doFinal

Added in API level 1
byte[] doFinal (byte[] input, 
                int inputOffset, 
                int inputLen)

Finishes a multi-part transformation (encryption or decryption).

Processes the inputLen bytes in input buffer at inputOffset, and any bytes that have been buffered in previous update calls.

Parameters
input byte: the input buffer.
inputOffset int: the offset in the input buffer.
inputLen int: the length of the input
Returns
byte[] the final bytes from the transformation.
Throws
IllegalBlockSizeException if the size of the resulting bytes is not a multiple of the cipher block size.
BadPaddingException if the padding of the data does not match the padding scheme.
IllegalStateException if this cipher instance is not initialized for encryption or decryption.
IllegalArgumentException if inputOffset and inputLen do not specify an valid chunk in the input buffer.

doFinal

Added in API level 1
int doFinal (byte[] input, 
                int inputOffset, 
                int inputLen, 
                byte[] output, 
                int outputOffset)

Finishes a multi-part transformation (encryption or decryption).

Processes the inputLen bytes in input buffer at inputOffset, and any bytes that have been buffered in previous update calls.

Parameters
input byte: the input buffer.
inputOffset int: the offset in the input buffer.
inputLen int: the length of the input.
output byte: the output buffer for the transformed bytes.
outputOffset int: the offset in the output buffer.
Returns
int the number of bytes placed in the output buffer.
Throws
ShortBufferException if the size of the output buffer is too small.
IllegalBlockSizeException if the size of the resulting bytes is not a multiple of the cipher block size.
BadPaddingException if the padding of the data does not match the padding scheme.
IllegalStateException if this cipher instance is not initialized for encryption or decryption.
IllegalArgumentException if inputOffset and inputLen do not specify an valid chunk in the input buffer.

doFinal

Added in API level 1
int doFinal (byte[] input, 
                int inputOffset, 
                int inputLen, 
                byte[] output)

Finishes a multi-part transformation (encryption or decryption).

Processes the inputLen bytes in input buffer at inputOffset, and any bytes that have been buffered in previous update calls.

Parameters
input byte: the input buffer.
inputOffset int: the offset in the input buffer.
inputLen int: the length of the input.
output byte: the output buffer for the transformed bytes.
Returns
int the number of bytes placed in the output buffer.
Throws
ShortBufferException if the size of the output buffer is too small.
IllegalBlockSizeException if the size of the resulting bytes is not a multiple of the cipher block size.
BadPaddingException if the padding of the data does not match the padding scheme.
IllegalStateException if this cipher instance is not initialized for encryption or decryption.
IllegalArgumentException if inputOffset and inputLen do not specify an valid chunk in the input buffer.

doFinal

Added in API level 1
byte[] doFinal (byte[] input)

Finishes a multi-part transformation (encryption or decryption).

Processes the bytes in input buffer, and any bytes that have been buffered in previous update calls.

Parameters
input byte: the input buffer.
Returns
byte[] the final bytes from the transformation.
Throws
IllegalBlockSizeException if the size of the resulting bytes is not a multiple of the cipher block size.
BadPaddingException if the padding of the data does not match the padding scheme.
IllegalStateException if this cipher instance is not initialized for encryption or decryption.

doFinal

Added in API level 1
int doFinal (ByteBuffer input, 
                ByteBuffer output)

Finishes a multi-part transformation (encryption or decryption).

Processes the input.remaining() bytes in input buffer at input.position(), and any bytes that have been buffered in previous update calls. The transformed bytes are placed into output buffer.

Parameters
input ByteBuffer: the input buffer.
output ByteBuffer: the output buffer.
Returns
int the number of bytes placed into the output buffer.
Throws
ShortBufferException if the size of the output buffer is too small.
IllegalBlockSizeException if the size of the resulting bytes is not a multiple of the cipher block size.
BadPaddingException if the padding of the data does not match the padding scheme.
IllegalArgumentException if the input buffer and the output buffer are the same object.
IllegalStateException if this cipher instance is not initialized for encryption or decryption.

doFinal

Added in API level 1
byte[] doFinal ()

Finishes a multi-part transformation (encryption or decryption).

Processes any bytes that may have been buffered in previous update calls.

Returns
byte[] the final bytes from the transformation.
Throws
IllegalBlockSizeException if the size of the resulting bytes is not a multiple of the cipher block size.
BadPaddingException if the padding of the data does not match the padding scheme.
IllegalStateException if this cipher instance is not initialized for encryption or decryption.

getAlgorithm

Added in API level 1
String getAlgorithm ()

Returns the name of the algorithm of this cipher instance.

This is the name of the transformation argument used in the getInstance call creating this object.

Returns
String the name of the algorithm of this cipher instance.

getBlockSize

Added in API level 1
int getBlockSize ()

Returns this ciphers block size (in bytes).

Returns
int this ciphers block size.

getExemptionMechanism

Added in API level 1
ExemptionMechanism getExemptionMechanism ()

Returns the exemption mechanism associated with this cipher.

Returns
ExemptionMechanism currently null

getIV

Added in API level 1
byte[] getIV ()

Returns the initialization vector for this cipher instance.

Returns
byte[] the initialization vector for this cipher instance.

getInstance

Added in API level 1
Cipher getInstance (String transformation, 
                String provider)

Creates a new cipher for the specified transformation provided by the specified provider.

Parameters
transformation String: the name of the transformation to create a cipher for.
provider String: the name of the provider to ask for the transformation.
Returns
Cipher a cipher for the requested transformation.
Throws
NoSuchAlgorithmException if the specified provider can not provide the transformation, or it is null, empty or in an invalid format.
NoSuchProviderException if no provider with the specified name can be found.
NoSuchPaddingException if the requested padding scheme in the transformation is not available.
IllegalArgumentException if the specified provider is null.

getInstance

Added in API level 1
Cipher getInstance (String transformation, 
                Provider provider)

Creates a new cipher for the specified transformation. The provider supplied does not have to be registered.

Parameters
transformation String: the name of the transformation to create a cipher for.
provider Provider: the provider to ask for the transformation.
Returns
Cipher a cipher for the requested transformation.
Throws
NoSuchAlgorithmException if the specified provider can not provide the transformation, or it is null, empty or in an invalid format.
NoSuchPaddingException if the requested padding scheme in the transformation is not available.
IllegalArgumentException if the provider is null.

getInstance

Added in API level 1
Cipher getInstance (String transformation)

Creates a new Cipher for the specified transformation. The installed providers are searched in order for an implementation of the specified transformation. The first found provider providing the transformation is used to create the cipher. If no provider is found an exception is thrown.

Parameters
transformation String: the name of the transformation to create a cipher for.
Returns
Cipher a cipher for the requested transformation.
Throws
NoSuchAlgorithmException if no installed provider can provide the transformation, or it is null, empty or in an invalid format.
NoSuchPaddingException if no installed provider can provide the padding scheme in the transformation.

getMaxAllowedKeyLength

Added in API level 1
int getMaxAllowedKeyLength (String transformation)

Returns the maximum key length for the specified transformation.

Parameters
transformation String: the transformation name.
Returns
int the maximum key length, currently Integer.MAX_VALUE.
Throws
NoSuchAlgorithmException if no provider for the specified transformation can be found.
NullPointerException if transformation is null.

getMaxAllowedParameterSpec

Added in API level 1
AlgorithmParameterSpec getMaxAllowedParameterSpec (String transformation)

Returns the maximum cipher parameter value for the specified transformation. If there is no maximum limit, null is returned.

Parameters
transformation String: the transformation name.
Returns
AlgorithmParameterSpec a parameter spec holding the maximum value or null. Currently null.
Throws
NoSuchAlgorithmException if no provider for the specified transformation can be found.
NullPointerException if transformation is null.

getOutputSize

Added in API level 1
int getOutputSize (int inputLen)

Returns the length in bytes an output buffer needs to be when this cipher is updated with inputLen bytes.

Parameters
inputLen int: the number of bytes of the input.
Returns
int the output buffer length for the input length.
Throws
IllegalStateException if this cipher instance is in an invalid state.

getParameters

Added in API level 1
AlgorithmParameters getParameters ()

Returns the parameters that where used to create this cipher instance.

These may be a the same parameters that were used to create this cipher instance, or may be a combination of default and random parameters, depending on the underlying cipher implementation.

Returns
AlgorithmParameters the parameters that where used to create this cipher instance, or null if this cipher instance does not have any parameters.

getProvider

Added in API level 1
Provider getProvider ()

Returns the provider of this cipher instance.

Returns
Provider the provider of this cipher instance.

init

Added in API level 1
void init (int opmode, 
                Key key)

Initializes this cipher instance with the specified key.

The cipher is initialized for the specified operational mode (one of: encryption, decryption, key wrapping or key unwrapping) depending on opmode.

If this cipher instance needs any algorithm parameters or random values that the specified key can not provide, the underlying implementation of this cipher is supposed to generate the required parameters (using its provider or random values).

When a cipher instance is initialized by a call to any of the init methods, the state of the instance is overridden, meaning that it is equivalent to creating a new instance and calling its init method.

Parameters
opmode int: the operation this cipher instance should be initialized for (one of: ENCRYPT_MODE, DECRYPT_MODE, WRAP_MODE or UNWRAP_MODE).
key Key: the input key for the operation.
Throws
InvalidKeyException if the specified key can not be used to initialize this cipher instance.

init

Added in API level 1
void init (int opmode, 
                Key key, 
                SecureRandom random)

Initializes this cipher instance with the specified key and a source of randomness.

The cipher is initialized for the specified operational mode (one of: encryption, decryption, key wrapping or key unwrapping) depending on opmode.

If this cipher instance needs any algorithm parameters or random values that the specified key can not provide, the underlying implementation of this cipher is supposed to generate the required parameters (using its provider or random values). Random values are generated using random;

When a cipher instance is initialized by a call to any of the init methods, the state of the instance is overridden, means it is equivalent to creating a new instance and calling it init method.

Parameters
opmode int: the operation this cipher instance should be initialized for (one of: ENCRYPT_MODE, DECRYPT_MODE, WRAP_MODE or UNWRAP_MODE).
key Key: the input key for the operation.
random SecureRandom: the source of randomness to use.
Throws
InvalidKeyException if the specified key can not be used to initialize this cipher instance.
InvalidParameterException if the specified opmode is invalid.

init

Added in API level 1
void init (int opmode, 
                Key key, 
                AlgorithmParameterSpec params, 
                SecureRandom random)

Initializes this cipher instance with the specified key, algorithm parameters and a source of randomness.

The cipher is initialized for the specified operational mode (one of: encryption, decryption, key wrapping or key unwrapping) depending on opmode.

If this cipher instance needs any algorithm parameters and params is null, the underlying implementation of this cipher is supposed to generate the required parameters (using its provider or random values). Random values are generated using random;

When a cipher instance is initialized by a call to any of the init methods, the state of the instance is overridden, meaning that it is equivalent to creating a new instance and calling it init method.

Parameters
opmode int: the operation this cipher instance should be initialized for (one of: ENCRYPT_MODE, DECRYPT_MODE, WRAP_MODE or UNWRAP_MODE).
key Key: the input key for the operation.
params AlgorithmParameterSpec: the algorithm parameters.
random SecureRandom: the source of randomness to use.
Throws
InvalidKeyException if the specified key can not be used to initialize this cipher instance.
InvalidAlgorithmParameterException it the specified parameters are inappropriate for this cipher.
InvalidParameterException if the specified opmode is invalid.

init

Added in API level 1
void init (int opmode, 
                Key key, 
                AlgorithmParameters params, 
                SecureRandom random)

Initializes this cipher instance with the specified key, algorithm parameters and a source of randomness.

The cipher will be initialized for the specified operation (one of: encryption, decryption, key wrapping or key unwrapping) depending on opmode.

If this cipher instance needs any algorithm parameters and params is null, the underlying implementation of this cipher is supposed to generate the required parameters (using its provider or random values). Random values are generated using random.

When a cipher instance is initialized by a call to any of the init methods, the state of the instance is overridden, means it is equivalent to creating a new instance and calling it init method.

Parameters
opmode int: the operation this cipher instance should be initialized for (one of: ENCRYPT_MODE, DECRYPT_MODE, WRAP_MODE or UNWRAP_MODE).
key Key: the input key for the operation.
params AlgorithmParameters: the algorithm parameters.
random SecureRandom: the source of randomness to use.
Throws
InvalidKeyException if the specified key can not be used to initialize this cipher instance.
InvalidAlgorithmParameterException if the specified parameters are inappropriate for this cipher.
InvalidParameterException if the specified opmode is invalid.

init

Added in API level 1
void init (int opmode, 
                Key key, 
                AlgorithmParameterSpec params)

Initializes this cipher instance with the specified key and algorithm parameters.

The cipher is initialized for the specified operational mode (one of: encryption, decryption, key wrapping or key unwrapping).

If this cipher instance needs any algorithm parameters and params is null, the underlying implementation of this cipher is supposed to generate the required parameters (using its provider or random values).

When a cipher instance is initialized by a call to any of the init methods, the state of the instance is overridden, means it is equivalent to creating a new instance and calling it init method.

Parameters
opmode int: the operation this cipher instance should be initialized for (one of: ENCRYPT_MODE, DECRYPT_MODE, WRAP_MODE or UNWRAP_MODE).
key Key: the input key for the operation.
params AlgorithmParameterSpec: the algorithm parameters.
Throws
InvalidKeyException if the specified key can not be used to initialize this cipher instance.
InvalidAlgorithmParameterException it the specified parameters are inappropriate for this cipher.

init

Added in API level 1
void init (int opmode, 
                Certificate certificate, 
                SecureRandom random)

Initializes this cipher instance with the public key from the specified certificate and a source of randomness.

The cipher will be initialized for the specified operation (one of: encryption, decryption, key wrapping or key unwrapping) depending on opmode.

It the type of the certificate is X.509 and the certificate has a key usage extension field marked as critical, the specified opmode has the be enabled for this key, otherwise an InvalidKeyException is thrown.

If this cipher instance needs any algorithm parameters that the key in the certificate can not provide, the underlying implementation of this cipher is supposed to generate the required parameters (using its provider or random values). Random values are generated using random.

When a cipher instance is initialized by a call to any of the init methods, the state of the instance is overridden, means it is equivalent to creating a new instance and calling it init method.

Parameters
opmode int: the operation this cipher instance should be initialized for (one of: ENCRYPT_MODE, DECRYPT_MODE, WRAP_MODE or UNWRAP_MODE).
certificate Certificate: the certificate.
random SecureRandom: the source of randomness to be used.
Throws
InvalidKeyException if the public key in the certificate can not be used to initialize this cipher instance.

init

Added in API level 1
void init (int opmode, 
                Certificate certificate)

Initializes this cipher instance with the public key from the specified certificate.

The cipher will be initialized for the specified operation (one of: encryption, decryption, key wrapping or key unwrapping) depending on opmode.

It the type of the certificate is X.509 and the certificate has a key usage extension field marked as critical, the specified opmode has the be enabled for this key, otherwise an InvalidKeyException is thrown.

If this cipher instance needs any algorithm parameters that the key in the certificate can not provide, the underlying implementation of this cipher is supposed to generate the required parameters (using its provider or random values).

When a cipher instance is initialized by a call to any of the init methods, the state of the instance is overridden, means it is equivalent to creating a new instance and calling it init method.

Parameters
opmode int: the operation this cipher instance should be initialized for (one of: ENCRYPT_MODE, DECRYPT_MODE, WRAP_MODE or UNWRAP_MODE).
certificate Certificate: the certificate.
Throws
InvalidKeyException if the public key in the certificate can not be used to initialize this cipher instance.

init

Added in API level 1
void init (int opmode, 
                Key key, 
                AlgorithmParameters params)

Initializes this cipher instance with the specified key and algorithm parameters.

The cipher is initialized for the specified operation (one of: encryption, decryption, key wrapping or key unwrapping) depending on opmode.

If this cipher instance needs any algorithm parameters and params is null, the underlying implementation of this cipher is supposed to generate the required parameters (using its provider or random values).

When a cipher instance is initialized by a call to any of the init methods, the state of the instance is overridden, meaning that it is equivalent to creating a new instance and calling it init method.

Parameters
opmode int: the operation this cipher instance should be initialized for (one of: ENCRYPT_MODE, DECRYPT_MODE, WRAP_MODE or UNWRAP_MODE).
key Key: the input key for the operation.
params AlgorithmParameters: the algorithm parameters.
Throws
InvalidKeyException if the specified key can not be used to initialize this cipher instance.
InvalidAlgorithmParameterException it the specified parameters are inappropriate for this cipher.

unwrap

Added in API level 1
Key unwrap (byte[] wrappedKey, 
                String wrappedKeyAlgorithm, 
                int wrappedKeyType)

Unwraps a key using this cipher instance.

Parameters
wrappedKey byte: the wrapped key to unwrap.
wrappedKeyAlgorithm String: the algorithm for the wrapped key.
wrappedKeyType int: the type of the wrapped key (one of: SECRET_KEY , PRIVATE_KEY or PUBLIC_KEY)
Returns
Key the unwrapped key
Throws
InvalidKeyException if the wrappedKey can not be unwrapped to a key of type wrappedKeyType for the wrappedKeyAlgorithm.
NoSuchAlgorithmException if no provider can be found that can create a key of type wrappedKeyType for the wrappedKeyAlgorithm.
IllegalStateException if this cipher instance is not initialized for unwrapping.

update

Added in API level 1
int update (ByteBuffer input, 
                ByteBuffer output)

Continues a multi-part transformation (encryption or decryption). The input.remaining() bytes starting at input.position() are transformed and stored in the output buffer.

If the output.remaining() is too small to hold the transformed bytes a ShortBufferException is thrown. Use getOutputSize to check for the size of the output buffer.

Parameters
input ByteBuffer: the input buffer to transform.
output ByteBuffer: the output buffer to store the result within.
Returns
int the number of bytes stored in the output buffer.
Throws
ShortBufferException if the size of the output buffer is too small.
IllegalStateException if this cipher instance is not initialized for encryption or decryption.
IllegalArgumentException if the input buffer and the output buffer are the identical object.

update

Added in API level 1
byte[] update (byte[] input)

Continues a multi-part transformation (encryption or decryption). The transformed bytes are returned.

Parameters
input byte: the input bytes to transform.
Returns
byte[] the transformed bytes in a new buffer, or null if the input has zero length.
Throws
IllegalStateException if this cipher instance is not initialized for encryption or decryption.
IllegalArgumentException if the input is null.

update

Added in API level 1
int update (byte[] input, 
                int inputOffset, 
                int inputLen, 
                byte[] output)

Continues a multi-part transformation (encryption or decryption). The transformed bytes are stored in the output buffer.

If the size of the output buffer is too small to hold the result, a ShortBufferException is thrown. Use getOutputSize to check for the size of the output buffer.

Parameters
input byte: the input bytes to transform.
inputOffset int: the offset in the input to start.
inputLen int: the length of the input to transform.
output byte: the output buffer.
Returns
int the number of bytes placed in output.
Throws
ShortBufferException if the size of the output buffer is too small.
IllegalStateException if this cipher instance is not initialized for encryption or decryption.
IllegalArgumentException if the input is null, the output is null, or if inputOffset and inputLen do not specify a valid chunk in the input buffer.

update

Added in API level 1
byte[] update (byte[] input, 
                int inputOffset, 
                int inputLen)

Continues a multi-part transformation (encryption or decryption). The transformed bytes are returned.

Parameters
input byte: the input bytes to transform.
inputOffset int: the offset in the input to start.
inputLen int: the length of the input to transform.
Returns
byte[] the transformed bytes in a new buffer, or null if inputLen is zero.
Throws
IllegalStateException if this cipher instance is not initialized for encryption or decryption.
IllegalArgumentException if input is null, or if inputOffset and inputLen do not specify a valid chunk in the input buffer.

update

Added in API level 1
int update (byte[] input, 
                int inputOffset, 
                int inputLen, 
                byte[] output, 
                int outputOffset)

Continues a multi-part transformation (encryption or decryption). The transformed bytes are stored in the output buffer.

If the size of the output buffer is too small to hold the result, a ShortBufferException is thrown. Use getOutputSize to check for the size of the output buffer.

Parameters
input byte: the input bytes to transform.
inputOffset int: the offset in the input to start.
inputLen int: the length of the input to transform.
output byte: the output buffer.
outputOffset int: the offset in the output buffer.
Returns
int the number of bytes placed in output.
Throws
ShortBufferException if the size of the output buffer is too small.
IllegalStateException if this cipher instance is not initialized for encryption or decryption.
IllegalArgumentException if the input is null, the output is null, or if inputOffset and inputLen do not specify a valid chunk in the input buffer.

updateAAD

Added in API level 19
void updateAAD (ByteBuffer input)

Continues a multi-part transformation (encryption or decryption) with Authenticated Additional Data (AAD). AAD may only be added after the Cipher is initialized and before any data is passed to the instance.

This is only usable with cipher modes that support Authenticated Encryption with Additional Data (AEAD) such as Galois/Counter Mode (GCM).

Parameters
input ByteBuffer: buffer of AAD to be used
Throws
IllegalStateException if this cipher instance is not initialized for encryption or decryption.
UnsupportedOperationException if the cipher does not support AEAD

updateAAD

Added in API level 19
void updateAAD (byte[] input, 
                int inputOffset, 
                int inputLen)

Continues a multi-part transformation (encryption or decryption) with Authenticated Additional Data (AAD). AAD may only be added after the Cipher is initialized and before any data is passed to the instance.

This is only usable with cipher modes that support Authenticated Encryption with Additional Data (AEAD) such as Galois/Counter Mode (GCM).

Parameters
input byte: bytes of AAD to use with the cipher
inputOffset int: offset within bytes of additional data to add to cipher
inputLen int: length of bytes of additional data to add to cipher
Throws
IllegalStateException if this cipher instance is not initialized for encryption or decryption.
IllegalArgumentException if input is null, or if inputOffset and inputLen do not specify a valid chunk in the input buffer.
UnsupportedOperationException if the cipher does not support AEAD

updateAAD

Added in API level 19
void updateAAD (byte[] input)

Continues a multi-part transformation (encryption or decryption) with Authenticated Additional Data (AAD). AAD may only be added after the Cipher is initialized and before any data is passed to the instance.

This is only usable with cipher modes that support Authenticated Encryption with Additional Data (AEAD) such as Galois/Counter Mode (GCM).

Parameters
input byte: bytes of AAD to use with the cipher
Throws
IllegalStateException if this cipher instance is not initialized for encryption or decryption.
IllegalArgumentException if input is null
UnsupportedOperationException if the cipher does not support AEAD

wrap

Added in API level 1
byte[] wrap (Key key)

Wraps a key using this cipher instance.

Parameters
key Key: the key to wrap.
Returns
byte[] the wrapped key.
Throws
IllegalBlockSizeException if the size of the resulting bytes is not a multiple of the cipher block size.
InvalidKeyException if this cipher instance can not wrap this key.
IllegalStateException if this cipher instance is not initialized for wrapping.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.