APIdia Launch Partner
Top Description Inners Fields Constructors Methods
com.sun.crypto.provider

pack-priv Class AESCipher

extends CipherSpi
Class Inheritance
Known Direct Subclasses
com.sun.crypto.provider.AESCipher.General, com.sun.crypto.provider.AESCipher.OidImpl
Imports
javax.crypto.BadPaddingException, .CipherSpi, .IllegalBlockSizeException, .NoSuchPaddingException, .ShortBufferException, java.security.AlgorithmParameters, .GeneralSecurityException, .InvalidAlgorithmParameterException, .InvalidKeyException, .Key, .NoSuchAlgorithmException, .ProviderException, .SecureRandom, java.security.spec.AlgorithmParameterSpec, java.util.Arrays

This class implements the AES algorithm in its various modes (ECB, CFB, OFB, CBC, PCBC) and padding schemes (PKCS5Padding, NoPadding, ISO10126Padding).
Author
Valerie Peng
See Also
AES_Crypt, CipherBlockChaining, ElectronicCodeBook, CipherFeedback, OutputFeedback

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
public static class
public static class
public static class
public static class
public static class
public static class
public static class
public static class
public static class
public static class
public static class
public static class
pack-priv static class

Field Summary

Modifier and TypeField and Description
private final CipherCore
private final int

Constructor Summary

AccessConstructor and Description
protected
AESCipher(int keySize)

Creates an instance of AES cipher with default ECB mode and PKCS5Padding.

Method Summary

Modifier and TypeMethod and Description
pack-priv static void
checkKeySize(Key key, int fixedKeySize)

protected byte[]

Returns:

the new buffer with the result
engineDoFinal
(byte[]
the input buffer
input
,
int
the offset in input where the input starts
inputOffset
,
int
the input length
inputLen
)

Implements abstract javax.crypto.CipherSpi.engineDoFinal.

Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.

protected int

Returns:

the number of bytes stored in output
engineDoFinal
(byte[]
the input buffer
input
,
int
the offset in input where the input starts
inputOffset
,
int
the input length
inputLen
,
byte[]
the buffer for the result
output
,
int
the offset in output where the result is stored
outputOffset
)

Implements abstract javax.crypto.CipherSpi.engineDoFinal.

Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.

protected int

Returns:

the block size (in bytes), or 0 if the underlying algorithm is not a block cipher
engineGetBlockSize
()

Implements abstract javax.crypto.CipherSpi.engineGetBlockSize.

Returns the block size (in bytes).

protected byte[]

Returns:

the initialization vector in a new buffer, or null if the underlying algorithm does not use an IV, or if the IV has not yet been set.
engineGetIV
()

Implements abstract javax.crypto.CipherSpi.engineGetIV.

Returns the initialization vector (IV) in a new buffer.

protected int

Returns:

the key size of the given key object.
engineGetKeySize
(Key
the key object.
key
)

Overrides javax.crypto.CipherSpi.engineGetKeySize.

Returns the key size of the given key object.

protected int

Returns:

the required output buffer size (in bytes)
engineGetOutputSize
(int
the input length (in bytes)
inputLen
)

Implements abstract javax.crypto.CipherSpi.engineGetOutputSize.

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, given the input length inputLen (in bytes).

protected AlgorithmParameters

Returns:

the parameters used with this cipher, or null if this cipher does not use any parameters.
engineGetParameters
()

Implements abstract javax.crypto.CipherSpi.engineGetParameters.

Returns the parameters used with this cipher.

protected void
engineInit(int
the operation mode of this cipher (this is one of the following: ENCRYPT_MODE, DECRYPT_MODE, WRAP_MODE or UNWRAP_MODE)
opmode
,
Key
the secret key
key
,
SecureRandom
the source of randomness
random
)

Implements abstract javax.crypto.CipherSpi.engineInit.

Initializes this cipher with a key and a source of randomness.

protected void
engineInit(int
the operation mode of this cipher (this is one of the following: ENCRYPT_MODE, DECRYPT_MODE, WRAP_MODE or UNWRAP_MODE)
opmode
,
Key
the encryption key
key
,
AlgorithmParameterSpec
the algorithm parameters
params
,
SecureRandom
the source of randomness
random
)

Implements abstract javax.crypto.CipherSpi.engineInit.

Initializes this cipher with a key, a set of algorithm parameters, and a source of randomness.

protected void
engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random)

Implements abstract javax.crypto.CipherSpi.engineInit.

Initializes this CipherSpi object with a key, a set of algorithm parameters, and a source of randomness.

protected void
engineSetMode(String
the cipher mode
mode
)

Implements abstract javax.crypto.CipherSpi.engineSetMode.

Sets the mode of this cipher.

protected void
engineSetPadding(String
the padding mechanism
paddingScheme
)

Implements abstract javax.crypto.CipherSpi.engineSetPadding.

Sets the padding mechanism of this cipher.

protected Key

Returns:

the unwrapped key.
engineUnwrap
(byte[]
the key to be unwrapped.
wrappedKey
,
String
the algorithm the wrapped key is for.
wrappedKeyAlgorithm
,
int
the type of the wrapped key. This is one of Cipher.SECRET_KEY, Cipher.PRIVATE_KEY, or Cipher.PUBLIC_KEY.
wrappedKeyType
)

Overrides javax.crypto.CipherSpi.engineUnwrap.

Unwrap a previously wrapped key.

protected byte[]

Returns:

the new buffer with the result
engineUpdate
(byte[]
the input buffer
input
,
int
the offset in input where the input starts
inputOffset
,
int
the input length
inputLen
)

Implements abstract javax.crypto.CipherSpi.engineUpdate.

Continues a multiple-part encryption or decryption operation (depending on how this cipher was initialized), processing another data part.

protected int

Returns:

the number of bytes stored in output
engineUpdate
(byte[]
the input buffer
input
,
int
the offset in input where the input starts
inputOffset
,
int
the input length
inputLen
,
byte[]
the buffer for the result
output
,
int
the offset in output where the result is stored
outputOffset
)

Implements abstract javax.crypto.CipherSpi.engineUpdate.

Continues a multiple-part encryption or decryption operation (depending on how this cipher was initialized), processing another data part.

protected byte[]

Returns:

the wrapped key.
engineWrap
(Key
the key to be wrapped.
key
)

Overrides javax.crypto.CipherSpi.engineWrap.

Wrap a key.

Inherited from javax.crypto.CipherSpi:
engineDoFinalengineUpdateengineUpdateAADengineUpdateAAD

Field Detail

coreback to summary
private final CipherCore core
fixedKeySizeback to summary
private final int fixedKeySize

Constructor Detail

AESCipherback to summary
protected AESCipher(int keySize)

Creates an instance of AES cipher with default ECB mode and PKCS5Padding.

Method Detail

checkKeySizeback to summary
pack-priv static void checkKeySize(Key key, int fixedKeySize) throws InvalidKeyException
engineDoFinalback to summary
protected byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen) throws IllegalBlockSizeException, BadPaddingException

Implements abstract javax.crypto.CipherSpi.engineDoFinal.

Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation. The data is encrypted or decrypted, depending on how this cipher was initialized.

The first inputLen bytes in the input buffer, starting at inputOffset, and any input bytes that may have been buffered during a previous update operation, are processed, with padding (if requested) being applied. The result is stored in a new buffer.

The cipher is reset to its initial state (uninitialized) after this call.

Parameters
input:byte[]

the input buffer

inputOffset:int

the offset in input where the input starts

inputLen:int

the input length

Returns:byte[]

the new buffer with the result

Annotations
@Override
Exceptions
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
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 bytes
engineDoFinalback to summary
protected int engineDoFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws IllegalBlockSizeException, ShortBufferException, BadPaddingException

Implements abstract javax.crypto.CipherSpi.engineDoFinal.

Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation. The data is encrypted or decrypted, depending on how this cipher was initialized.

The first inputLen bytes in the input buffer, starting at inputOffset, and any input bytes that may have been buffered during a previous update operation, are processed, with padding (if requested) being applied. The result is stored in the output buffer, starting at outputOffset.

The cipher is reset to its initial state (uninitialized) after this call.

Parameters
input:byte[]

the input buffer

inputOffset:int

the offset in input where the input starts

inputLen:int

the input length

output:byte[]

the buffer for the result

outputOffset:int

the offset in output where the result is stored

Returns:int

the number of bytes stored in output

Annotations
@Override
Exceptions
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
ShortBufferException:
if the given output buffer is too small to hold the result
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 bytes
engineGetBlockSizeback to summary
protected int engineGetBlockSize()

Implements abstract javax.crypto.CipherSpi.engineGetBlockSize.

Returns the block size (in bytes).

Returns:int

the block size (in bytes), or 0 if the underlying algorithm is not a block cipher

Annotations
@Override
engineGetIVback to summary
protected byte[] engineGetIV()

Implements abstract javax.crypto.CipherSpi.engineGetIV.

Returns the initialization vector (IV) in a new buffer.

This is useful in the case where a random IV has been created (see init), or in the context of password-based encryption or decryption, where the IV is derived from a user-provided password.

Returns:byte[]

the initialization vector in a new buffer, or null if the underlying algorithm does not use an IV, or if the IV has not yet been set.

Annotations
@Override
engineGetKeySizeback to summary
protected int engineGetKeySize(Key key) throws InvalidKeyException

Overrides javax.crypto.CipherSpi.engineGetKeySize.

Returns the key size of the given key object.

Parameters
key:Key

the key object.

Returns:int

the key size of the given key object.

Annotations
@Override
Exceptions
InvalidKeyException:
if key is invalid.
engineGetOutputSizeback to summary
protected int engineGetOutputSize(int inputLen)

Implements abstract javax.crypto.CipherSpi.engineGetOutputSize.

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, given the input length inputLen (in bytes).

This call takes into account any unprocessed (buffered) data from a previous update call, and padding.

The actual output length of the next update or doFinal call may be smaller than the length returned by this method.

Parameters
inputLen:int

the input length (in bytes)

Returns:int

the required output buffer size (in bytes)

Annotations
@Override
engineGetParametersback to summary
protected AlgorithmParameters engineGetParameters()

Implements abstract javax.crypto.CipherSpi.engineGetParameters.

Returns the parameters used with this cipher.

The returned parameters may be the same that were used to initialize this cipher, or may contain the default set of parameters or a set of randomly generated parameters used by the underlying cipher implementation (provided that the underlying cipher implementation uses a default set of parameters or creates new parameters if it needs parameters but was not initialized with any).

Returns:AlgorithmParameters

the parameters used with this cipher, or null if this cipher does not use any parameters.

Annotations
@Override
engineInitback to summary
protected void engineInit(int opmode, Key key, SecureRandom random) throws InvalidKeyException

Implements abstract javax.crypto.CipherSpi.engineInit.

Initializes this cipher with a key and a source of randomness.

The cipher is initialized for one of the following four operations: encryption, decryption, key wrapping or key unwrapping, depending on the value of opmode.

If this cipher requires an initialization vector (IV), it will get it from random. This behaviour should only be used in encryption or key wrapping mode, however. When initializing a cipher that requires an IV for decryption or key unwrapping, the IV (same IV that was used for encryption or key wrapping) must be provided explicitly as a parameter, in order to get the correct result.

This method also cleans existing buffer and other related state information.

Parameters
opmode:int

the operation mode of this cipher (this is one of the following: ENCRYPT_MODE, DECRYPT_MODE, WRAP_MODE or UNWRAP_MODE)

key:Key

the secret key

random:SecureRandom

the source of randomness

Annotations
@Override
Exceptions
InvalidKeyException:
if the given key is inappropriate for initializing this cipher
engineInitback to summary
protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException

Implements abstract javax.crypto.CipherSpi.engineInit.

Initializes this cipher with a key, a set of algorithm parameters, and a source of randomness.

The cipher is initialized for one of the following four operations: encryption, decryption, key wrapping or key unwrapping, depending on the value of opmode.

If this cipher (including its underlying feedback or padding scheme) requires any random bytes, it will get them from random.

Parameters
opmode:int

the operation mode of this cipher (this is one of the following: ENCRYPT_MODE, DECRYPT_MODE, WRAP_MODE or UNWRAP_MODE)

key:Key

the encryption key

params:AlgorithmParameterSpec

the algorithm parameters

random:SecureRandom

the source of randomness

Annotations
@Override
Exceptions
InvalidKeyException:
if the given key is inappropriate for initializing this cipher
InvalidAlgorithmParameterException:
if the given algorithm parameters are inappropriate for this cipher
engineInitback to summary
protected void engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException

Implements abstract javax.crypto.CipherSpi.engineInit.

Doc from javax.crypto.CipherSpi.engineInit.

Initializes this CipherSpi object with a key, a set of algorithm parameters, and a source of randomness.

The CipherSpi object is initialized for one of the following four operations: encryption, decryption, key wrapping or key unwrapping, depending on the value of opmode.

If this cipher requires any algorithm parameters and params is null, the underlying cipher implementation is supposed to generate the required parameters itself (using provider-specific default or random values) if it is being initialized for encryption or key wrapping, and raise an InvalidAlgorithmParameterException if it is being initialized for decryption or key unwrapping. The generated parameters can be retrieved using engineGetParameters or engineGetIV (if the parameter is an IV).

If this cipher requires algorithm parameters that cannot be derived from the input parameters, and there are no reasonable provider-specific default values, initialization will necessarily fail.

If this cipher (including its feedback or padding scheme) requires any random bytes (e.g., for parameter generation), it will get them from random.

Note that when a CipherSpi object is initialized, it loses all previously-acquired state. In other words, initializing a CipherSpi object is equivalent to creating a new instance of that CipherSpi object and initializing it.

Annotations
@Override
Exceptions
InvalidKeyException:
if the given key is inappropriate for initializing this cipher
InvalidAlgorithmParameterException:
if the given algorithm parameters are inappropriate for this cipher, or if this cipher requires algorithm parameters and params is null
engineSetModeback to summary
protected void engineSetMode(String mode) throws NoSuchAlgorithmException

Implements abstract javax.crypto.CipherSpi.engineSetMode.

Sets the mode of this cipher.

Parameters
mode:String

the cipher mode

Annotations
@Override
Exceptions
NoSuchAlgorithmException:
if the requested cipher mode does not exist
engineSetPaddingback to summary
protected void engineSetPadding(String paddingScheme) throws NoSuchPaddingException

Implements abstract javax.crypto.CipherSpi.engineSetPadding.

Sets the padding mechanism of this cipher.

Parameters
paddingScheme:String

the padding mechanism

Annotations
@Override
Exceptions
NoSuchPaddingException:
if the requested padding mechanism does not exist
engineUnwrapback to summary
protected Key engineUnwrap(byte[] wrappedKey, String wrappedKeyAlgorithm, int wrappedKeyType) throws InvalidKeyException, NoSuchAlgorithmException

Overrides javax.crypto.CipherSpi.engineUnwrap.

Unwrap a previously wrapped key.

Parameters
wrappedKey:byte[]

the key to be unwrapped.

wrappedKeyAlgorithm:String

the algorithm the wrapped key is for.

wrappedKeyType:int

the type of the wrapped key. This is one of Cipher.SECRET_KEY, Cipher.PRIVATE_KEY, or Cipher.PUBLIC_KEY.

Returns:Key

the unwrapped key.

Annotations
@Override
Exceptions
InvalidKeyException:
if wrappedKey does not represent a wrapped key of type wrappedKeyType for the wrappedKeyAlgorithm.
NoSuchAlgorithmException:
if no installed providers can create keys of type wrappedKeyType for the wrappedKeyAlgorithm.
engineUpdateback to summary
protected byte[] engineUpdate(byte[] input, int inputOffset, int inputLen)

Implements abstract javax.crypto.CipherSpi.engineUpdate.

Continues a multiple-part encryption or decryption operation (depending on how this cipher was initialized), processing another data part.

The first inputLen bytes in the input buffer, starting at inputOffset, are processed, and the result is stored in a new buffer.

Parameters
input:byte[]

the input buffer

inputOffset:int

the offset in input where the input starts

inputLen:int

the input length

Returns:byte[]

the new buffer with the result

Annotations
@Override
Exceptions
IllegalStateException:
if this cipher is in a wrong state (e.g., has not been initialized)
engineUpdateback to summary
protected int engineUpdate(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws ShortBufferException

Implements abstract javax.crypto.CipherSpi.engineUpdate.

Continues a multiple-part encryption or decryption operation (depending on how this cipher was initialized), processing another data part.

The first inputLen bytes in the input buffer, starting at inputOffset, are processed, and the result is stored in the output buffer, starting at outputOffset.

Parameters
input:byte[]

the input buffer

inputOffset:int

the offset in input where the input starts

inputLen:int

the input length

output:byte[]

the buffer for the result

outputOffset:int

the offset in output where the result is stored

Returns:int

the number of bytes stored in output

Annotations
@Override
Exceptions
ShortBufferException:
if the given output buffer is too small to hold the result
engineWrapback to summary
protected byte[] engineWrap(Key key) throws IllegalBlockSizeException, InvalidKeyException

Overrides javax.crypto.CipherSpi.engineWrap.

Wrap a key.

Parameters
key:Key

the key to be wrapped.

Returns:byte[]

the wrapped key.

Annotations
@Override
Exceptions
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.
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).
com.sun.crypto.provider back to summary

public final Class AESCipher.AES128_CBC_NoPadding

extends OidImpl
Class Inheritance

Constructor Summary

AccessConstructor and Description
public

Constructor Detail

AES128_CBC_NoPaddingback to summary
public AES128_CBC_NoPadding()
com.sun.crypto.provider back to summary

public final Class AESCipher.AES128_CFB_NoPadding

extends OidImpl
Class Inheritance

Constructor Summary

AccessConstructor and Description
public

Constructor Detail

AES128_CFB_NoPaddingback to summary
public AES128_CFB_NoPadding()
com.sun.crypto.provider back to summary

public final Class AESCipher.AES128_ECB_NoPadding

extends OidImpl
Class Inheritance

Constructor Summary

AccessConstructor and Description
public

Constructor Detail

AES128_ECB_NoPaddingback to summary
public AES128_ECB_NoPadding()
com.sun.crypto.provider back to summary

public final Class AESCipher.AES128_OFB_NoPadding

extends OidImpl
Class Inheritance

Constructor Summary

AccessConstructor and Description
public

Constructor Detail

AES128_OFB_NoPaddingback to summary
public AES128_OFB_NoPadding()
com.sun.crypto.provider back to summary

public final Class AESCipher.AES192_CBC_NoPadding

extends OidImpl
Class Inheritance

Constructor Summary

AccessConstructor and Description
public

Constructor Detail

AES192_CBC_NoPaddingback to summary
public AES192_CBC_NoPadding()
com.sun.crypto.provider back to summary

public final Class AESCipher.AES192_CFB_NoPadding

extends OidImpl
Class Inheritance

Constructor Summary

AccessConstructor and Description
public

Constructor Detail

AES192_CFB_NoPaddingback to summary
public AES192_CFB_NoPadding()
com.sun.crypto.provider back to summary

public final Class AESCipher.AES192_ECB_NoPadding

extends OidImpl
Class Inheritance

Constructor Summary

AccessConstructor and Description
public

Constructor Detail

AES192_ECB_NoPaddingback to summary
public AES192_ECB_NoPadding()
com.sun.crypto.provider back to summary

public final Class AESCipher.AES192_OFB_NoPadding

extends OidImpl
Class Inheritance

Constructor Summary

AccessConstructor and Description
public

Constructor Detail

AES192_OFB_NoPaddingback to summary
public AES192_OFB_NoPadding()
com.sun.crypto.provider back to summary

public final Class AESCipher.AES256_CBC_NoPadding

extends OidImpl
Class Inheritance

Constructor Summary

AccessConstructor and Description
public

Constructor Detail

AES256_CBC_NoPaddingback to summary
public AES256_CBC_NoPadding()
com.sun.crypto.provider back to summary

public final Class AESCipher.AES256_CFB_NoPadding

extends OidImpl
Class Inheritance

Constructor Summary

AccessConstructor and Description
public

Constructor Detail

AES256_CFB_NoPaddingback to summary
public AES256_CFB_NoPadding()
com.sun.crypto.provider back to summary

public final Class AESCipher.AES256_ECB_NoPadding

extends OidImpl
Class Inheritance

Constructor Summary

AccessConstructor and Description
public

Constructor Detail

AES256_ECB_NoPaddingback to summary
public AES256_ECB_NoPadding()
com.sun.crypto.provider back to summary

public final Class AESCipher.AES256_OFB_NoPadding

extends OidImpl
Class Inheritance

Constructor Summary

AccessConstructor and Description
public

Constructor Detail

AES256_OFB_NoPaddingback to summary
public AES256_OFB_NoPadding()
com.sun.crypto.provider back to summary

public final Class AESCipher.General

extends AESCipher
Class Inheritance

Constructor Summary

AccessConstructor and Description
public

Method Summary

Inherited from com.sun.crypto.provider.AESCipher:
checkKeySizeengineDoFinalengineDoFinalengineGetBlockSizeengineGetIVengineGetKeySizeengineGetOutputSizeengineGetParametersengineInitengineInitengineInitengineSetModeengineSetPaddingengineUnwrapengineUpdateengineUpdateengineWrap

Constructor Detail

Generalback to summary
public General()
com.sun.crypto.provider back to summary

pack-priv Class AESCipher.OidImpl

extends AESCipher
Class Inheritance
Known Direct Subclasses
com.sun.crypto.provider.AESCipher.AES128_ECB_NoPadding, com.sun.crypto.provider.AESCipher.AES192_ECB_NoPadding, com.sun.crypto.provider.AESCipher.AES256_ECB_NoPadding, com.sun.crypto.provider.AESCipher.AES128_CBC_NoPadding, com.sun.crypto.provider.AESCipher.AES192_CBC_NoPadding, com.sun.crypto.provider.AESCipher.AES256_CBC_NoPadding, com.sun.crypto.provider.AESCipher.AES128_OFB_NoPadding, com.sun.crypto.provider.AESCipher.AES192_OFB_NoPadding, com.sun.crypto.provider.AESCipher.AES256_OFB_NoPadding, com.sun.crypto.provider.AESCipher.AES128_CFB_NoPadding, com.sun.crypto.provider.AESCipher.AES192_CFB_NoPadding, com.sun.crypto.provider.AESCipher.AES256_CFB_NoPadding

Constructor Summary

AccessConstructor and Description
protected
OidImpl(int keySize, String mode, String padding)

Method Summary

Inherited from com.sun.crypto.provider.AESCipher:
checkKeySizeengineDoFinalengineDoFinalengineGetBlockSizeengineGetIVengineGetKeySizeengineGetOutputSizeengineGetParametersengineInitengineInitengineInitengineSetModeengineSetPaddingengineUnwrapengineUpdateengineUpdateengineWrap

Constructor Detail

OidImplback to summary
protected OidImpl(int keySize, String mode, String padding)