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

pack-priv final Class CipherCore

extends Object
Class Inheritance
Imports
java.util.Arrays, .Locale, java.security.*, java.security.spec.*, javax.crypto.*, .BadPaddingException, javax.crypto.spec.*

This class represents the symmetric algorithms in its various modes (ECB, CFB, OFB, CBC, PCBC, CTR, and CTS) and padding schemes (PKCS5Padding, NoPadding, ISO10126Padding).
Authors
Gigi Ankeny, Jan Luehe
See Also
ElectronicCodeBook, CipherFeedback, OutputFeedback, CipherBlockChaining, PCBC, CounterMode, CipherTextStealing

Field Summary

Modifier and TypeField and Description
private final int
private final byte[]
private int
private static final int
private static final int
private FeedbackCipher
private int
private static final int
private static final int
private boolean
private int
private static final int
private int
private static final int
private Padding
private static final int
private int

Constructor Summary

AccessConstructor and Description
pack-priv
CipherCore(SymmetricCipher impl, int blkSize)

Creates an instance of CipherCore with default ECB mode and PKCS5Padding.

Method Summary

Modifier and TypeMethod and Description
private int
checkOutputCapacity(byte[] output, int outputOffset, int estOutSize)

pack-priv byte[]

Returns:

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

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

pack-priv int

Returns:

the number of bytes stored in output
doFinal
(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
)

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

private void
private int
fillOutputBuffer(byte[] finalBuf, int finalOffset, byte[] output, int outOfs, int finalBufLen, byte[] input)

private int
finalNoPadding(byte[] in, int inOfs, byte[] out, int outOfs, int len)

pack-priv 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.
getIV
()

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

pack-priv static byte[]
getKeyBytes(Key key)

Return the key bytes of the specified key.

private static int
getNumOfUnit(String mode, int offset, int blockSize)

pack-priv int

Returns:

the required output buffer size (in bytes)
getOutputSize
(int
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 update or doFinal operation, given the input length inputLen (in bytes).

private int
getOutputSizeByOperation(int inputLen, boolean isDoFinal)

pack-priv AlgorithmParameters

Returns:

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

Returns the parameters used with this cipher.

pack-priv void
init(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
)

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

pack-priv void
init(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
)

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

pack-priv void
init(int opmode, Key key, AlgorithmParameters params, SecureRandom random)

private byte[]
prepareInputBuffer(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset)

pack-priv void
setMode(String
the cipher mode
mode
)

Sets the mode of this cipher.

pack-priv void
setPadding(String
the padding mechanism
paddingScheme
)

Sets the padding mechanism of this cipher.

private int
unpad(int outLen, int off, byte[] outWithPadding)

pack-priv Key

Returns:

the unwrapped key.
unwrap
(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
)

Unwrap a previously wrapped key.

pack-priv byte[]

Returns:

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

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

pack-priv int

Returns:

the number of bytes stored in output
update
(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
)

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

pack-priv byte[]

Returns:

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

Wrap a key.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

blockSizeback to summary
private final int blockSize
bufferback to summary
private final byte[] buffer
bufferedback to summary
private int buffered
CBC_MODEback to summary
private static final int CBC_MODE
CFB_MODEback to summary
private static final int CFB_MODE
cipherback to summary
private FeedbackCipher cipher
cipherModeback to summary
private int cipherMode
CTR_MODEback to summary
private static final int CTR_MODE
CTS_MODEback to summary
private static final int CTS_MODE
decryptingback to summary
private boolean decrypting
diffBlocksizeback to summary
private int diffBlocksize
ECB_MODEback to summary
private static final int ECB_MODE
minBytesback to summary
private int minBytes
OFB_MODEback to summary
private static final int OFB_MODE
paddingback to summary
private Padding padding
PCBC_MODEback to summary
private static final int PCBC_MODE
unitBytesback to summary
private int unitBytes

Constructor Detail

CipherCoreback to summary
pack-priv CipherCore(SymmetricCipher impl, int blkSize)

Creates an instance of CipherCore with default ECB mode and PKCS5Padding.

Method Detail

checkOutputCapacityback to summary
private int checkOutputCapacity(byte[] output, int outputOffset, int estOutSize) throws ShortBufferException
doFinalback to summary
pack-priv byte[] doFinal(byte[] input, int inputOffset, int inputLen) throws IllegalBlockSizeException, BadPaddingException

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

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
doFinalback to summary
pack-priv int doFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws IllegalBlockSizeException, ShortBufferException, BadPaddingException

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

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
endDoFinalback to summary
private void endDoFinal()
fillOutputBufferback to summary
private int fillOutputBuffer(byte[] finalBuf, int finalOffset, byte[] output, int outOfs, int finalBufLen, byte[] input) throws BadPaddingException, IllegalBlockSizeException
finalNoPaddingback to summary
private int finalNoPadding(byte[] in, int inOfs, byte[] out, int outOfs, int len) throws IllegalBlockSizeException
getIVback to summary
pack-priv byte[] getIV()

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.

getKeyBytesback to summary
pack-priv static byte[] getKeyBytes(Key key) throws InvalidKeyException

Return the key bytes of the specified key. Throw an InvalidKeyException if the key is not usable.

getNumOfUnitback to summary
private static int getNumOfUnit(String mode, int offset, int blockSize) throws NoSuchAlgorithmException
getOutputSizeback to summary
pack-priv 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 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)

getOutputSizeByOperationback to summary
private int getOutputSizeByOperation(int inputLen, boolean isDoFinal)
getParametersback to summary
pack-priv AlgorithmParameters getParameters(String algName)

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.

initback to summary
pack-priv void init(int opmode, Key key, SecureRandom random) throws InvalidKeyException

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

Exceptions
InvalidKeyException:
if the given key is inappropriate for initializing this cipher
initback to summary
pack-priv void init(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException

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

Exceptions
InvalidKeyException:
if the given key is inappropriate for initializing this cipher
InvalidAlgorithmParameterException:
if the given algorithm parameters are inappropriate for this cipher
initback to summary
pack-priv void init(int opmode, Key key, AlgorithmParameters params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException
prepareInputBufferback to summary
private byte[] prepareInputBuffer(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws IllegalBlockSizeException, ShortBufferException
setModeback to summary
pack-priv void setMode(String mode) throws NoSuchAlgorithmException

Sets the mode of this cipher.

Parameters
mode:String

the cipher mode

Exceptions
NoSuchAlgorithmException:
if the requested cipher mode does not exist for this cipher
setPaddingback to summary
pack-priv void setPadding(String paddingScheme) throws NoSuchPaddingException

Sets the padding mechanism of this cipher.

Parameters
paddingScheme:String

the padding mechanism

Exceptions
NoSuchPaddingException:
if the requested padding mechanism does not exist
unpadback to summary
private int unpad(int outLen, int off, byte[] outWithPadding) throws BadPaddingException
unwrapback to summary
pack-priv Key unwrap(byte[] wrappedKey, String wrappedKeyAlgorithm, int wrappedKeyType) throws InvalidKeyException, NoSuchAlgorithmException

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.

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.
updateback to summary
pack-priv 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.

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

Exceptions
IllegalStateException:
if this cipher is in a wrong state (e.g., has not been initialized)
updateback to summary
pack-priv int update(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws ShortBufferException

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

Exceptions
ShortBufferException:
if the given output buffer is too small to hold the result
wrapback to summary
pack-priv byte[] wrap(Key key) throws IllegalBlockSizeException, InvalidKeyException

Wrap a key.

Parameters
key:Key

the key to be wrapped.

Returns:byte[]

the wrapped key.

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).