de.flexiprovider.core.pbe
Class PBES2

java.lang.Object
  |
  +--javax.crypto.CipherSpi
        |
        +--de.flexiprovider.api.Cipher
              |
              +--de.flexiprovider.core.pbe.PBES2

public class PBES2
extends Cipher

This class is the main class for the passphrase based encryption scheme 2 as defined in PKCS #5 version 2.0 standard.

Author:
Thomas Wahrenbruch, Martin Döring

Field Summary
static java.lang.String OID
          The OID of PBES2.
 
Fields inherited from class de.flexiprovider.api.Cipher
DECRYPT_MODE, ENCRYPT_MODE, opMode
 
Constructor Summary
PBES2()
           
 
Method Summary
 byte[] doFinal(byte[] input, int inOff, int inLen)
          Finish a multiple-part encryption or decryption operation (depending on how this cipher was initialized).
 int doFinal(byte[] input, int inOff, int inLen, byte[] output, int outOff)
          Finish a multiple-part encryption or decryption operation (depending on how this cipher was initialized).
 int getBlockSize()
          Returns the block size (in bytes).
 byte[] getIV()
          Returns the initialization vector (IV) in a new buffer.
 int getKeySize(Key key)
          Return the key size of the given key object.
 java.lang.String getName()
          Return the name of this cipher.
 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).
 AlgorithmParameterSpec getParameters()
          Returns the parameters used with this cipher.
 void initDecrypt(Key key, AlgorithmParameterSpec params)
          Initializes this cipher with a key, a set of algorithm parameters, and a source of randomness.
 void initEncrypt(Key key, AlgorithmParameterSpec params, SecureRandom random)
          Initializes this cipher with a key, a set of algorithm parameters, and a source of randomness.
protected  void setMode(java.lang.String modeName)
          Set the mode for this cipher.
protected  void setPadding(java.lang.String paddingName)
          Set the padding scheme for this cipher.
 byte[] update(byte[] input, int inOff, int inLen)
          Continue a multiple-part encryption or decryption operation (depending on how this cipher was initialized), processing another data part.
 int update(byte[] input, int inOff, int inLen, byte[] output, int outOff)
          Continue a multiple-part encryption or decryption operation (depending on how this cipher was initialized), processing another data part.
 
Methods inherited from class de.flexiprovider.api.Cipher
doFinal, doFinal, engineDoFinal, engineDoFinal, engineGetBlockSize, engineGetIV, engineGetKeySize, engineGetOutputSize, engineGetParameters, engineInit, engineInit, engineInit, engineSetMode, engineSetPadding, engineUpdate, engineUpdate, update
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

OID

public static final java.lang.String OID
The OID of PBES2.
Constructor Detail

PBES2

public PBES2()
Method Detail

getName

public java.lang.String getName()
Return the name of this cipher.
Overrides:
getName in class Cipher
Returns:
"PBES2"

getKeySize

public int getKeySize(Key key)
               throws InvalidKeyException
Return the key size of the given key object. Checks whether the key object is an instance of PBEKey.
Overrides:
getKeySize in class Cipher
Parameters:
key - the key object
Returns:
the key size of the given key object.
Throws:
InvalidKeyException - if key is invalid.

getBlockSize

public int getBlockSize()
Returns the block size (in bytes).
Overrides:
getBlockSize in class Cipher
Returns:
the block size (in bytes), or 0 if the underlying algorithm is not a block cipher

getIV

public byte[] getIV()
Returns the initialization vector (IV) in a new buffer.

This is useful in the context of password-based encryption or decryption, where the IV is derived from a user-provided passphrase.

Overrides:
getIV in class Cipher
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.

getOutputSize

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

Overrides:
getOutputSize in class Cipher
Parameters:
inputLen - the input length (in bytes)
Returns:
the required output buffer size (in bytes)

getParameters

public AlgorithmParameterSpec getParameters()
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).

Overrides:
getParameters in class Cipher
Returns:
the parameters used with this cipher, or null if this cipher does not use any parameters.

initEncrypt

public void initEncrypt(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 encryption.

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, and raise an InvalidAlgorithmParameterException if it is being initialized for decryption. The generated parameters can be retrieved using engineGetParameters or engineGetIV (if the parameter is an IV).

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

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

Overrides:
initEncrypt in class Cipher
Parameters:
key - the encryption key
params - the algorithm parameters
random - the source of randomness
Throws:
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 is being initialized fro decryption and requires algorithm parameters and params is null

initDecrypt

public void initDecrypt(Key key,
                        AlgorithmParameterSpec params)
                 throws InvalidKeyException,
                        InvalidAlgorithmParameterException
Initializes this cipher with a key, a set of algorithm parameters, and a source of randomness.

The cipher is initialized for decryption.

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, and raise an InvalidAlgorithmParameterException if it is being initialized for decryption. The generated parameters can be retrieved using engineGetParameters or engineGetIV (if the parameter is an IV).

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

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

Overrides:
initDecrypt in class Cipher
Parameters:
key - the encryption key
params - the algorithm parameters
Throws:
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 is being initialized fro decryption and requires algorithm parameters and params is null

setMode

protected void setMode(java.lang.String modeName)
                throws NoSuchModeException
Set the mode for this cipher. This method is not supported and always throws an exception.
Overrides:
setMode in class Cipher
Parameters:
modeName - the name of the cipher mode
Throws:
NoSuchModeException - always.

setPadding

protected void setPadding(java.lang.String paddingName)
                   throws NoSuchPaddingException
Set the padding scheme for this cipher. This method is not supported and always throws an exception.
Overrides:
setPadding in class Cipher
Parameters:
paddingName - the name of the padding scheme
Throws:
NoSuchPaddingException - always.

update

public byte[] update(byte[] input,
                     int inOff,
                     int inLen)
Continue a multiple-part encryption or decryption operation (depending on how this cipher was initialized), processing another data part.
Overrides:
update in class Cipher
Parameters:
input - the input buffer
inOff - the offset where the input starts
inLen - the input length
Returns:
a new buffer with the result (maybe an empty byte array)

update

public int update(byte[] input,
                  int inOff,
                  int inLen,
                  byte[] output,
                  int outOff)
           throws ShortBufferException
Continue a multiple-part encryption or decryption operation (depending on how this cipher was initialized), processing another data part.
Overrides:
update in class Cipher
Parameters:
input - the input buffer
inOff - the offset where the input starts
inLen - the input length
output - the output buffer
outOff - the offset where the result is stored
Returns:
the length of the output
Throws:
ShortBufferException - if the output buffer is too small to hold the result.

doFinal

public byte[] doFinal(byte[] input,
                      int inOff,
                      int inLen)
               throws IllegalBlockSizeException,
                      BadPaddingException
Finish a multiple-part encryption or decryption operation (depending on how this cipher was initialized).
Overrides:
doFinal in class Cipher
Parameters:
input - the input buffer
inOff - the offset where the input starts
inLen - the input length
Returns:
a new buffer with the result
Throws:
IllegalBlockSizeException - if the total input length is not a multiple of the block size (for encryption when no padding is used or for decryption).
BadPaddingException - if unpadding fails.

doFinal

public int doFinal(byte[] input,
                   int inOff,
                   int inLen,
                   byte[] output,
                   int outOff)
            throws ShortBufferException,
                   IllegalBlockSizeException,
                   BadPaddingException
Finish a multiple-part encryption or decryption operation (depending on how this cipher was initialized).
Overrides:
doFinal in class Cipher
Parameters:
input - the input buffer
inOff - the offset where the input starts
inLen - the input length
output - the buffer for the result
outOff - the offset where the result is stored
Returns:
the output length
Throws:
ShortBufferException - if the output buffer is too small to hold the result.
IllegalBlockSizeException - if the total input length is not a multiple of the block size (for encryption when no padding is used or for decryption).
BadPaddingException - if unpadding fails.