FileDocCategorySizeDatePackage
DES_CBC.javaAPI DocphoneME MR2 API (J2ME)5333Wed May 02 18:00:00 BST 2007com.sun.midp.crypto

DES_CBC

public class DES_CBC extends DES_ECB
DES CBC Cipher algorithm.

Fields Summary
private byte[]
chainingBlock
Internal data.
private byte[]
storeBuffer
Internal data.
private byte[]
savedChainingBlock
Saved state variable.
Constructors Summary
public DES_CBC(boolean useTripleDes)
Constructor.

param
useTripleDes true if the class is being used for triple DES

        super(useTripleDes);
        chainingBlock = new byte[BLOCK_SIZE];
        storeBuffer = new byte[BLOCK_SIZE];
    
Methods Summary
public intdoFinal(byte[] inBuff, int inOffset, int inLength, byte[] outBuff, int outOffset)
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.

param
inBuff the input buffer
param
inOffset the offset in input where the input starts
param
inLength the input length
param
outBuff the buffer for the result
param
outOffset the offset in output where the result is stored
return
the number of bytes stored in output
exception
IllegalStateException if this cipher is in a wrong state (e.g., has not been initialized)
exception
javax.crypto.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
exception
javax.crypto.ShortBufferException if the given output buffer is too small to hold the result
exception
javax.crypto.BadPaddingException if this cipher is int decryption mode, and (un)padding has been requested, but the decrypted data is not bounded by the appropriate padding bytes

        int result = super.doFinal(inBuff, inOffset, inLength,
                                   outBuff, outOffset);
        System.arraycopy(IV, 0, chainingBlock, 0, BLOCK_SIZE);
        return result;
    
public voidinit(int mode, Key key, CryptoParameter params)
Initializes this cipher with a key and a set of algorithm parameters.

param
mode the operation mode of this cipher
param
key the encryption key
param
params the algorithm parameters
exception
java.security.InvalidKeyException if the given key is inappropriate for initializing this cipher
exception
java.security.InvalidAlgorithmParameterException if the given algorithm parameters are inappropriate for this cipher


        doInit(mode, keyAlgorithm, key, true, params);
        System.arraycopy(IV, 0, chainingBlock, 0, BLOCK_SIZE);
    
protected voidprocessBlock(byte[] out, int offset)
Depending on the mode, either encrypts or decrypts data block.

param
out will contain the result of encryption or decryption operation
param
offset is the offset in out


        if (mode == Cipher.ENCRYPT_MODE) {
            Util.xorArrays(holdData, chainingBlock);
            super.processBlock(out, offset);
            System.arraycopy(out, offset, chainingBlock, 0, BLOCK_SIZE);
        } else {
            System.arraycopy(holdData, 0, storeBuffer, 0, BLOCK_SIZE);
            super.processBlock(out, offset);
            for (int i = 0; i < BLOCK_SIZE; i++) {
                out[offset + i] ^= chainingBlock[i];
            }
            System.arraycopy(storeBuffer, 0, chainingBlock, 0, BLOCK_SIZE);
        }
    
protected voidrestoreState()
Restores cipher state.

        super.restoreState();
        chainingBlock = savedChainingBlock;
    
protected voidsaveState()
Saves cipher state.

        super.saveState();
        savedChainingBlock = Util.cloneArray(chainingBlock);