com.motorola.iden.crypto
Class Cipher

java.lang.Object
  |
  +--com.motorola.iden.crypto.Cipher

public class Cipher
extends java.lang.Object

This class provides the functionality of a cryptographic cipher for encryption and decryption.

In order to create a Cipher object, the application calls the Cipher's getInstance method, and passes the name of the requested transformation to it. Optionally, the name of a provider may be specified.

A transformation is a string that describes the operation (or set of operations) to be performed on the given input, to produce some output. A transformation always includes the name of a cryptographic algorithm (e.g., DES), and may be followed by a feedback mode and padding scheme.

A transformation is of the form:

(in the latter case, provider-specific default values for the mode and padding scheme are used). For example, the following is a valid transformation:

     Cipher c = Cipher.getInstance("DES/CBC/PKCS5Padding");
 

MOTOROLA and the Stylized M Logo are registered trademarks of Motorola, Inc. Reg. U.S. Pat. & Tm. Off.
© Copyright 2002 - 2004 Motorola, Inc. All Rights Reserved.

See Also:
[CipherSpi]

Field Summary
static int CBC
          Constant used to initialize cipher to CBC mode.
static int CFB
          Constant used to initialize cipher to CFB64 mode.
static int CFB128
          Constant used to initialize cipher to CFB128 mode.
static int DECRYPT_MODE
          Constant used to initialize cipher to decryption mode.
static int ECB
          Constant used to initialize cipher to ECB mode.
static int ENCRYPT_MODE
          Constant used to initialize cipher to encryption mode.
static int OFB
          Constant used to initialize cipher to OFB64 mode.
static int OFB128
          Constant used to initialize cipher to OFB128 mode.
static int PKCS1V15
          Constant used to initialize padding to PKCS#1v15;
static int PKCS5
          Constant used to initialize padding to PKCS#5;
static int PRIVATE
          Constant used to initialize public key operation.
static int PUBLIC
          Constant used to initialize public key operation.
 
Constructor Summary
protected Cipher(CipherSpi cipherSpi)
          Creates a Cipher object.
 
Method Summary
 byte[] doFinal()
          Finishes a multiple-part encryption or decryption operation, depending on how this cipher was initialized.
 byte[] doFinal(byte[] input)
          Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.
 byte[] doFinal(byte[] input, int offset, int len)
          Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.
 java.lang.String getAlgorithm()
          Returns the algorithm name of this Cipher object.
 int getBlockSize()
          Returns the block size (in bytes).
static Cipher getInstance(java.lang.String transformation)
          Generates a Cipher object that implements the specified transformation.
 byte[] getIV()
          Returns the initialization vector (IV) in a new buffer.
 void init(int opmode, Key key)
          Initializes this cipher with a key.
 void init(int opmode, Key key, AlgorithmParameterSpec params)
          Initializes this cipher with a key and a set of algorithm parameters.
 byte[] update(byte[] input)
          Continues a multiple-part encryption or decryption operation (depending on how this cipher was initialized), processing another data part.
 byte[] update(byte[] input, int offset, int len)
          Continues a multiple-part encryption or decryption operation (depending on how this cipher was initialized), processing another data part.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ENCRYPT_MODE

public static final int ENCRYPT_MODE
Constant used to initialize cipher to encryption mode.

DECRYPT_MODE

public static final int DECRYPT_MODE
Constant used to initialize cipher to decryption mode.

ECB

public static final int ECB
Constant used to initialize cipher to ECB mode.

CBC

public static final int CBC
Constant used to initialize cipher to CBC mode.

CFB

public static final int CFB
Constant used to initialize cipher to CFB64 mode.

OFB

public static final int OFB
Constant used to initialize cipher to OFB64 mode.

CFB128

public static final int CFB128
Constant used to initialize cipher to CFB128 mode.

OFB128

public static final int OFB128
Constant used to initialize cipher to OFB128 mode.

PKCS5

public static final int PKCS5
Constant used to initialize padding to PKCS#5;

PKCS1V15

public static final int PKCS1V15
Constant used to initialize padding to PKCS#1v15;

PUBLIC

public static final int PUBLIC
Constant used to initialize public key operation.

PRIVATE

public static final int PRIVATE
Constant used to initialize public key operation.
Constructor Detail

Cipher

protected Cipher(CipherSpi cipherSpi)
Creates a Cipher object.
Parameters:
cipherSpi - the instance of an algorithm which implements CipherSpi.
Method Detail

getInstance

public static final Cipher getInstance(java.lang.String transformation)
                                throws NoSuchAlgorithmException,
                                       NoSuchPaddingException
Generates a Cipher object that implements the specified transformation.

If the default provider package supplies an implementation of the requested transformation, an instance of Cipher containing that implementation is returned.

Parameters:
transformation - the name of the transformation, e.g., DES/CBC/PKCS5Padding, or DES.

The following table list all supported cipher algorithms, modes, and paddings.

ALGORITM MODE PADDING
DES ECB;CBC;CFB;OFB PKCS5Padding
DESede ECB;CBC;CFB;OFB PKCS5Padding
AES ECB;CBC;CFB;OFB PKCS5Padding
RC5 ECB;CBC;CFB;OFB PKCS5Padding
ARC4 ----- -----
RSA ECB PKCS1v15Padding

Returns:
a cipher that implements the requested transformation
Throws:
NoSuchAlgorithmException - if the specified transformation is not available in the default provider package.
NoSuchPaddingException - if transformation contains a padding scheme that is not available.

getAlgorithm

public final java.lang.String getAlgorithm()
Returns the algorithm name of this Cipher object.

This is the same name that was specified in one of the getInstance calls that created this Cipher object..

Returns:
the algorithm name of this Cipher object.

getBlockSize

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

getIV

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

This is useful in the case where a random IV was created, or in the context of password-based encryption or decryption, where the IV is derived from a user-supplied password.

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.

init

public final void init(int opmode,
                       Key key)
                throws InvalidKeyException,
                       InvalidAlgorithmParameterException
Initializes this cipher with a key.

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

If this cipher requires any algorithm parameters that cannot be derived from the given key, the underlying cipher implementation is supposed to generate the required parameters itself.

Parameters:
opmode - the operation mode of this cipher (this is one of the following: ENCRYPT_MODE, DECRYPT_MODE)
key - the key
Throws:
InvalidKeyException - if the given key is inappropriate for initializing this cipher, or if this cipher is being initialized for decryption and requires algorithm parameters that cannot be determined from the given key, or if the given key has a keysize that exceeds the maximum allowable keysize (as determined from the configured jurisdiction policy files).
InvalidAlgorithmParameterException - if algorithmparameters are needed to init this cipher.

init

public final void init(int opmode,
                       Key key,
                       AlgorithmParameterSpec params)
                throws InvalidKeyException,
                       InvalidAlgorithmParameterException
Initializes this cipher with a key and a set of algorithm parameters.

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

Parameters:
opmode - the operation mode of this cipher (this is one of the following: ENCRYPT_MODE, DECRYPT_MODE)
key - the encryption key
params - the algorithm parameters
Throws:
InvalidKeyException - if the given key is inappropriate for initializing this cipher, or its keysize exceeds the maximum allowable keysize (as determined from the configured jurisdiction policy files).
InvalidAlgorithmParameterException - if the given algorithm parameters are inappropriate for this cipher, or this cipher is being initialized for decryption and requires algorithm parameters and params is null, or the given algorithm parameters imply a cryptographic strength that would exceed the legal limits (as determined from the configured jurisdiction policy files).

update

public final byte[] update(byte[] input)
                    throws java.lang.IllegalStateException
Continues a multiple-part encryption or decryption operation (depending on how this cipher was initialized), processing another data part.

The bytes in the input buffer are processed, and the result is stored in a new buffer.

If input has a length of zero, this method returns null.

Parameters:
input - the input buffer
Returns:
the new buffer with the result, or null if the underlying cipher is a block cipher and the input data is too short to result in a new block.
Throws:
java.lang.IllegalStateException - if this cipher is in a wrong state (e.g., has not been initialized)

update

public final byte[] update(byte[] input,
                           int offset,
                           int len)
                    throws java.lang.IllegalStateException
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 inclusive, are processed, and the result is stored in a new buffer.

If inputLen is zero, this method returns null.

Parameters:
input - the input buffer
offset - the offset in input where the input starts
len - the input length
Returns:
the new buffer with the result, or null if the underlying cipher is a block cipher and the input data is too short to result in a new block.
Throws:
java.lang.IllegalStateException - if this cipher is in a wrong state (e.g., has not been initialized)

doFinal

public final byte[] doFinal()
                     throws java.lang.IllegalStateException,
                            IllegalBlockSizeException,
                            BadPaddingException
Finishes a multiple-part encryption or decryption operation, depending on how this cipher was initialized.

Input data that may have been buffered during a previous update operation is processed, with padding (if requested) being applied. The result is stored in a new buffer.

A call to this method resets this cipher object to the state it was in when previously initialized via a call to init. That is, the object is reset and available to encrypt or decrypt (depending on the operation mode that was specified in the call to init) more data.

Returns:
the new buffer with the result
Throws:
java.lang.IllegalStateException - if this cipher is in a wrong state (e.g., has not been initialized)
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

doFinal

public final byte[] doFinal(byte[] input)
                     throws java.lang.IllegalStateException,
                            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 bytes in the input buffer, 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.

A call to this method resets this cipher object to the state it was in when previously initialized via a call to init. That is, the object is reset and available to encrypt or decrypt (depending on the operation mode that was specified in the call to init) more data.

Parameters:
input - the input buffer
Returns:
the new buffer with the result
Throws:
java.lang.IllegalStateException - if this cipher is in a wrong state (e.g., has not been initialized)
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

doFinal

public final byte[] doFinal(byte[] input,
                            int offset,
                            int len)
                     throws java.lang.IllegalStateException,
                            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 inclusive, 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.

A call to this method resets this cipher object to the state it was in when previously initialized via a call to init. That is, the object is reset and available to encrypt or decrypt (depending on the operation mode that was specified in the call to init) more data.

Parameters:
input - the input buffer
offset - the offset in input where the input starts
len - the input length
Returns:
the new buffer with the result
Throws:
java.lang.IllegalStateException - if this cipher is in a wrong state (e.g., has not been initialized)
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