iaik.security.cipher
Class DES

java.lang.Object
  |
  +--javax.crypto.CipherSpi
        |
        +--iaik.security.cipher.BufferedCipher
              |
              +--iaik.security.cipher.DES
Direct Known Subclasses:
PbeWithMD5AndDES_CBC

public class DES
extends iaik.security.cipher.BufferedCipher

Extends the BufferedCipher class for adding a buffering mechanism to the underlying DES cipher.

This DES implementation can be used with custom S-Boxes and/or P-Boxes, see the DESParameterSpec class. From these boxes custom tables are automatically generated to allow for a fast DES ciphering. Memory usage is 3.5 kbyte per instance that does not use the standard tables. Calculating these tables also takes a bit of time (each time init() is called), but it is pretty fast and usually neglegible.

The Data Encryption Standard (DES) today is the most popular symmetric block algorithm for data encryption. It operates on data blocks of 64 bit size using a 64 (in fact 56) bit key. The DES algorithm performs 16 rounds permutating and combining data and key bits according to a given scheme.

Initially, the given 64 bit data block is devided into two 32-bit parts, called left and right half. 48 of the 56 key bits are XOR combined with the right data half, which previously is expanded to 48 bits. Each of the resulting 48 combined data-key bits specifies one of 48 address lines into 8 S-boxes (64x4 ROMs a 6 address lines and 4 data output lines). From every S-Box one 4-Bit word is read. The resulting eight 4-bit words are concatenated giving a 32-bit word, which - after a permutation - is combined with the old left data half to produce the new right data half for the next round. The old right data half is shifted to the left half, serving as new left half for the next round. After 16 rounds, left and right data half are combined and permutated to finally produce the encrypted 64 bit block.

Decryption uses the same proceeding, except for applying the keys in reverse order.


This class only creates a BufferedCipher object for the DES cipher.

Applications shall use

Cipher.getInstance("DES", "IAIK");
 
for creating a DES object. They may optionally specifiy operation mode (ECB (default), CBC, PCBC, OFB, CFB) and padding scheme (NoPadding (default), or PKCS5Padding as described in the PKCS #5: Password-Based Encryption Standard).

When requesting this DES implementation without any mode specification (Cipher.getInstance("DES")), the DES algorithm is used in pure ECB (Electronic Code Book) mode encrypting plaintext blocks into ciphertext blocks independently from each other. The ECB mode is prone to codebook attacks and block replay. A codebook attack may be successfully when being able to read plain- and corresponding ciphertext blocks for a certain quantity of messages making it possible to generate a codebook for decrypting blocks of further messages without knowing the key. For being effective against codebook analyses and block replay (often messages contain common sub-parts making it possible to unnoticed replace these blocks) one can use the CBC mode makes the encryption of one block of plain data conditional on all previously encrypted data blocks.

Since ECB encrypts each single block independently, it enables random access to encrypted data blocks which may be preferable for database encrypting. Often ECB is used for key-encrypting.

Version:
File Revision 19
See Also:
DESParameterSpec, Cipher

Constructor Summary
DES()
          Creates a DES object by calling the BufferedCipher constructor for the DES cipher.
 
Method Summary
 byte[] engineDoFinal(byte[] in, int inOff, int inLen)
          Returns the result of the last step of a multi-step en/decryption operation or the result of a single-step en/decryption operation by processing the given input data and any remaining buffered data.
 int engineDoFinal(byte[] in, int inOff, int inLen, byte[] out, int outOff)
          Performs the last step of a multi-step en/decryption operation or a single-step en/decryption operation by processing the given input data and any remaining buffered data.
 int engineGetBlockSize()
          Returns the block size corresponding to this cipher.
 byte[] engineGetIV()
          Returns a byte array containing the initialization vector (IV).
protected  int engineGetKeySize(Key key)
          New method in JCE 1.2.1
 int engineGetOutputSize(int inLen)
          Returns the output buffer size necessary for capturing the data resulting from the next update or doFinal operation including any data currently being buffered.
 AlgorithmParameters engineGetParameters()
          Returns the parameters used with this cipher.
 void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random)
          Initializes this cipher object with proper key and algorithm parameter values, and some random seed.
 void engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random)
          Initializes this cipher object with proper key and algorithm parameter values, and some random seed.
 void engineInit(int opmode, Key key, SecureRandom random)
          Initializes this cipher object with a proper key and some random seed.
 void engineSetMode(String mode)
          Sets the mode of this cipher.
 void engineSetPadding(String paddingName)
          Sets the padding scheme of this cipher.
protected  Key engineUnwrap(byte[] wrappedKey, String wrappedKeyAlgorithm, int wrappedKeyType)
          Engine method for key unwrapping.
 byte[] engineUpdate(byte[] in, int inOff, int inLen)
          This method is used to encrypt or decrypt chunks of data of any length.
 int engineUpdate(byte[] in, int inOff, int inLen, byte[] out, int outOff)
          This method is used to encrypt or decrypt chunks of data of any length.
protected  byte[] engineWrap(Key key)
          Engine method for key wrapping.
 int getModeBlockSize()
          Returns the block size corresponding to the actual cipher mode.
 String toString()
          Returns a string representation of this Cipher.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DES

public DES()
Creates a DES object by calling the BufferedCipher constructor for the DES cipher. This constructor only internally is used for initializing a DES Cipher. Applications should not call this constructor to get a DES Cipher; they should call one of the Cipher.getInstance factory methods instead.

See Also:
Cipher.getInstance(java.lang.String)
Method Detail

engineInit

public void engineInit(int opmode,
                       Key key,
                       AlgorithmParameterSpec params,
                       SecureRandom random)
                throws InvalidKeyException,
                       InvalidAlgorithmParameterException
Initializes this cipher object with proper key and algorithm parameter values, and some random seed.

Before a cipher object is ready for data processing, it has to be initialized according to the desired cryptographic operation, which is specified by the opmode parameter (either ENCRYPT_MODE or DECCRYPT_MODE), e.g.:

 cipher_obj.init(Cipher.ENCRYPT_MODE, key, alg_params, random_seed);
 

The Cipher init will call the proper CipherSpi engineInit method.

Overrides:
engineInit in class CipherSpi
Parameters:
opmode - the operation mode for which this cipher is used (ENCRYPT_MODE or DECRYPT_MODE)
key - the key
params - the algorithm parameters
random - the random seed
Throws:
InvalidKeyException - if the given key cannot be used for initializing this cipher
InvalidAlgorithmParameterException - if the given algorithm parameters donīt match to this cipher
See Also:
Cipher.init(int, java.security.Key), CipherSpi.engineInit(int, java.security.Key, java.security.SecureRandom)

engineInit

public void engineInit(int opmode,
                       Key key,
                       SecureRandom random)
                throws InvalidKeyException
Initializes this cipher object with a proper key and some random seed.

Before a cipher object is ready for data processing, it has to be initialized according to the desired cryptographic operation, which is specified by the opmode parameter (either ENCRYPT_MODE or DECCRYPT_MODE), e.g.:

 cipher_obj.init(Cipher.ENCRYPT_MODE, key, random_seed);
 

The Cipher init will call the proper CipherSpi engineInit method.

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

Overrides:
engineInit in class CipherSpi
Parameters:
opmode - the operation mode for which this cipher is used (ENCRYPT_MODE or DECRYPT_MODE)
key - the key
random - the random seed
Throws:
InvalidKeyException - if the given key cannot be used for initializing this cipher
See Also:
Cipher.init(int, java.security.Key), CipherSpi.engineInit(int, java.security.Key, java.security.SecureRandom)

engineInit

public void engineInit(int opmode,
                       Key key,
                       AlgorithmParameters params,
                       SecureRandom random)
                throws InvalidKeyException,
                       InvalidAlgorithmParameterException
Initializes this cipher object with proper key and algorithm parameter values, and some random seed.

Before a cipher object is ready for data processing, it has to be initialized according to the desired cryptographic operation, which is specified by the opmode parameter (either ENCRYPT_MODE or DECCRYPT_MODE), e.g.:

 cipher_obj.init(Cipher.ENCRYPT_MODE, key, alg_params, random_seed);
 

The Cipher init will call the proper CipherSpi engineInit method.

Overrides:
engineInit in class CipherSpi
Parameters:
opmode - the operation mode for which this cipher is used (ENCRYPT_MODE or DECRYPT_MODE)
key - the key
params - the algorithm parameters
random - the random seed
Throws:
InvalidKeyException - if the given key cannot be used for initializing this cipher
InvalidAlgorithmParameterException - if the given algorithm parameters donīt match to this cipher
See Also:
Cipher.init(int, java.security.Key), CipherSpi.engineInit(int, java.security.Key, java.security.SecureRandom)

engineGetParameters

public AlgorithmParameters engineGetParameters()
Description copied from class: CipherSpi
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:
engineGetParameters in class CipherSpi
Following copied from class: javax.crypto.CipherSpi
Returns:
the parameters used with this cipher, or null if this cipher does not use any parameters

engineSetPadding

public void engineSetPadding(String paddingName)
                      throws NoSuchPaddingException
Sets the padding scheme of this cipher.

If an application creates a cipher object by calling Cipher.getInstance(...) without specifying a particular padding scheme, engineSetMode is supplied with the default "NoPadding" scheme.

The following padding schemes are supported:

Overrides:
engineSetPadding in class CipherSpi
Parameters:
paddingName - the name of the padding scheme
Throws:
NoSuchPaddingException - if this padding scheme is not supported
See Also:
CipherSpi.engineSetPadding(java.lang.String)

engineSetMode

public void engineSetMode(String mode)
                   throws NoSuchAlgorithmException
Sets the mode of this cipher.

If an application creates a cipher object by calling Cipher.getInstance(...) without specifying a particular cipher mode, engineSetMode is supplied with the default "ECB" mode. The following modes are supported:

Overrides:
engineSetMode in class CipherSpi
Parameters:
mode - the cipher mode
Throws:
NoSuchAlgorithmException - if this cipher mode is not supported
See Also:
CipherSpi.engineSetMode(java.lang.String)

engineUpdate

public byte[] engineUpdate(byte[] in,
                           int inOff,
                           int inLen)
This method is used to encrypt or decrypt chunks of data of any length. To get get the last data in the buffer use a doFinal() method. This method will automatically allocate an output buffer of the right size.
Overrides:
engineUpdate in class CipherSpi
Parameters:
in - the input data.
inOff - the offset indicating where the subarray starts in the in array.
inLen - the length of the subarray.
isFinal - true, if this is the last call to update
Returns:
array containing the en/decrypted data
See Also:
BufferedCipher.engineDoFinal(byte[], int, int, byte[], int), Cipher.doFinal(), Cipher.update(byte[]), CipherSpi.engineUpdate(byte[], int, int)

engineUpdate

public int engineUpdate(byte[] in,
                        int inOff,
                        int inLen,
                        byte[] out,
                        int outOff)
                 throws ShortBufferException
This method is used to encrypt or decrypt chunks of data of any length. To get get the last data in the buffer use a doFinal() method.
Overrides:
engineUpdate in class CipherSpi
Parameters:
in - the input data.
inOff - the offset indicating where the subarray starts in the in array.
inLen - the length of the subarray.
out - the output buffer.
outOff - the offset indicating where to start writing the result into the output buffer.
Returns:
number of bytes written to out array.
Throws:
ShoetBufferException - if the buffer size is to short
See Also:
BufferedCipher.engineDoFinal(byte[], int, int, byte[], int), Cipher.doFinal(), Cipher.update(byte[]), CipherSpi.engineUpdate(byte[], int, int)

engineGetOutputSize

public int engineGetOutputSize(int inLen)
Returns the output buffer size necessary for capturing the data resulting from the next update or doFinal operation including any data currently being buffered.
Overrides:
engineGetOutputSize in class CipherSpi
Parameters:
inLen - the number of bytes to process
Returns:
the size of the output buffer (in bytes)
See Also:
Cipher.getOutputSize(int), CipherSpi.engineGetOutputSize(int)

engineGetIV

public byte[] engineGetIV()
Returns a byte array containing the initialization vector (IV). If the algorithm corresponding to this cipher object does not use an initialization vector, null is returned.
Overrides:
engineGetIV in class CipherSpi
Returns:
the initialization vector in a byte array if this cipher object operates with an IV and the IV already has been set, null otherwise.
See Also:
Cipher.getIV(), CipherSpi.engineGetIV()

getModeBlockSize

public int getModeBlockSize()
Returns the block size corresponding to the actual cipher mode. This may differ from the actual block size of the cipher in the OFB, CFB and CTR mode. This is the input size that an application must supply to an update call to get an output.
Returns:
The block size (in bytes) of the current mode. This is always at least one.

engineGetBlockSize

public int engineGetBlockSize()
Returns the block size corresponding to this cipher. The block size is returned in bytes. If the implemented algorithm is not a block cipher, 0 is returned.
Overrides:
engineGetBlockSize in class CipherSpi
Returns:
the block size (in bytes) if this cipher is a block cipher, 0 otherwise.
See Also:
Cipher.getBlockSize(), CipherSpi.engineGetBlockSize()

engineDoFinal

public int engineDoFinal(byte[] in,
                         int inOff,
                         int inLen,
                         byte[] out,
                         int outOff)
                  throws ShortBufferException,
                         IllegalBlockSizeException,
                         BadPaddingException
Performs the last step of a multi-step en/decryption operation or a single-step en/decryption operation by processing the given input data and any remaining buffered data.

The data to be processed is given in an input byte array. Beginning at inputOffset, only the first inputLen bytes are en/decrypted, including any buffered bytes of a previous update operation. If necessary, padding is performed. The result is stored in the given output byte array, beginning at outputOffset. The number of bytes stored in this byte array are returned.

Overrides:
engineDoFinal in class CipherSpi
Parameters:
in - the byte array holding the data to be processed
inOff - the offset indicating the start position within the input byte array
inLen - the number of bytes to be processed
out - the byte array for holding the result
outOff - the offset indicating the start position within the output byte array to which the en/decrypted data is written
Returns:
the number of bytes stored in the output byte array
Throws:
ShortBufferException - if the given output buffer is too small for holding the result
IllegalBlockSizeException - if the total length of the processed data is not a multiple of the block size for a (no padding performing) block cipher
BadPaddingException - if the decrypted data is not bounded by the proper padding bytes after data decryption including (un)padding
See Also:
Cipher.doFinal(), CipherSpi.engineDoFinal(byte[], int, int)

engineDoFinal

public byte[] engineDoFinal(byte[] in,
                            int inOff,
                            int inLen)
                     throws IllegalBlockSizeException,
                            BadPaddingException
Returns the result of the last step of a multi-step en/decryption operation or the result of a single-step en/decryption operation by processing the given input data and any remaining buffered data.

The data to be processed is given in an input byte array. Beginning at inputOffset, only the first inputLen bytes are en/decrypted, including any buffered bytes of a previous update operation. If necessary, padding is performed. The result is returned as a output byte array.

Overrides:
engineDoFinal in class CipherSpi
Parameters:
in - the byte array holding the data to be processed
inOff - the offset indicating the start position within the input byte array
inLen - the number of bytes to be processed
Returns:
the byte array containing the en/decrypted data
Throws:
IllegalBlockSizeException - if the total length of the processed data is not a multiple of the block size for a (no padding performing) block cipher
BadPaddingException - if the decrypted data is not bounded by the proper padding bytes after data decryption including (un)padding
See Also:
Cipher.doFinal(), CipherSpi.engineDoFinal(byte[], int, int)

toString

public String toString()
Returns a string representation of this Cipher.
Overrides:
toString in class Object
Returns:
a string representation

engineGetKeySize

protected int engineGetKeySize(Key key)
                        throws InvalidKeyException
Description copied from class: CipherSpi
New method in JCE 1.2.1
Overrides:
engineGetKeySize in class CipherSpi

engineWrap

protected byte[] engineWrap(Key key)
                     throws IllegalBlockSizeException,
                            InvalidKeyException
Description copied from class: CipherSpi
Engine method for key wrapping. Since JCE 1.2.1
Overrides:
engineWrap in class CipherSpi

engineUnwrap

protected Key engineUnwrap(byte[] wrappedKey,
                           String wrappedKeyAlgorithm,
                           int wrappedKeyType)
                    throws InvalidKeyException,
                           NoSuchAlgorithmException
Description copied from class: CipherSpi
Engine method for key unwrapping. Since JCE 1.2.1
Overrides:
engineUnwrap in class CipherSpi

This Javadoc may contain text parts from Internet Standard specifications (RFC 2459, 3280, 3039, 2560, 1521, 821, 822, 2253, 1319, 1321, ,2630, 2631, 2268, 3058, 2984, 2104, 2144, 2040, 2311, 2279, see copyright note) and RSA Data Security Public-Key Cryptography Standards (PKCS#1,3,5,7,8,9,10,12, see copyright note).

IAIK-JCE 3.1 with IAIK-JCE CC Core 3.1, (c) 1997-2004 IAIK