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

public final Class PBEWithMD5AndTripleDESCipher

extends CipherSpi
Class Inheritance
Imports
java.security.*, java.security.spec.*, javax.crypto.*

This class implements a proprietary password-based encryption algorithm. It is based on password-based encryption as defined by the PKCS #5 standard, except that is uses triple DES instead of DES. Here's how this algorithm works: 1. Create random salt and split it in two halves. If the two halves are identical, invert one of them. 2. Concatenate password with each of the halves. 3. Digest each concatenation with c iterations, where c is the iterationCount. Concatenate the output from each digest round with the password, and use the result as the input to the next digest operation. The digest algorithm is MD5. 4. After c iterations, use the 2 resulting digests as follows: The 16 bytes of the first digest and the 1st 8 bytes of the 2nd digest form the triple DES key, and the last 8 bytes of the 2nd digest form the IV.
Author
Jan Luehe
See Also
javax.crypto.Cipher

Field Summary

Modifier and TypeField and Description
private final PBES1Core

Constructor Summary

AccessConstructor and Description
public
PBEWithMD5AndTripleDESCipher()

Creates an instance of this cipher, and initializes its mode (CBC) and padding (PKCS5).

Method Summary

Modifier and TypeMethod and Description
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)
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 encryption 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 either ENCRYPT_MODE or DECRYPT_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 PBES1Core core

Constructor Detail

PBEWithMD5AndTripleDESCipherback to summary
public PBEWithMD5AndTripleDESCipher() throws NoSuchAlgorithmException, NoSuchPaddingException

Creates an instance of this cipher, and initializes its mode (CBC) and padding (PKCS5).

Exceptions
NoSuchAlgorithmException:
if the required cipher mode (CBC) is unavailable
NoSuchPaddingException:
if the required padding mechanism (PKCS5Padding) is unavailable

Method Detail

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

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 decrypting and padding is chosen, but the last input data does not have proper padding bytes.
engineDoFinalback to summary
protected int engineDoFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws ShortBufferException, 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 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
ShortBufferException:
if the given output buffer is too small to hold the result
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 decrypting and padding is chosen, but the last input data does not have proper 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)

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

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.

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)

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.

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 (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

random:SecureRandom

the source of randomness

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 encryption or decryption, 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 either ENCRYPT_MODE or DECRYPT_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
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.

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. This algorithm can only be run in CBC mode.

Parameters
mode:String

the cipher mode

Exceptions
NoSuchAlgorithmException:
if the requested cipher mode is invalid
engineSetPaddingback to summary
protected void engineSetPadding(String paddingScheme) throws NoSuchPaddingException

Implements abstract javax.crypto.CipherSpi.engineSetPadding.

Sets the padding mechanism of this cipher. This algorithm only uses PKCS #5 padding.

Parameters
paddingScheme:String

the padding mechanism

Exceptions
NoSuchPaddingException:
if the requested padding mechanism is invalid
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.

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

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

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.

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