Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.bouncycastle.crypto.modes;
  
implements Cipher-Block-Chaining (CBC) mode on top of a simple cipher.
 
 public class CBCBlockCipher
     implements BlockCipher
 {
     private byte[]          IV;
     private byte[]          cbcV;
     private byte[]          cbcNextV;
 
     private int             blockSize;
     private BlockCipher     cipher = null;
     private boolean         encrypting;

    
Basic constructor.

Parameters:
cipher the block cipher to be used as the basis of chaining.
 
     public CBCBlockCipher(
         BlockCipher cipher)
     {
         this. = cipher;
         this. = cipher.getBlockSize();
 
         this. = new byte[];
         this. = new byte[];
         this. = new byte[];
     }

    
return the underlying block cipher that we are wrapping.

Returns:
the underlying block cipher that we are wrapping.
 
     public BlockCipher getUnderlyingCipher()
     {
         return ;
     }

    
Initialise the cipher and, possibly, the initialisation vector (IV). If an IV isn't passed as part of the parameter, the IV will be all zeros.

Parameters:
encrypting if true the cipher is initialised for encryption, if false for decryption.
params the key and other data required by the cipher.
Throws:
java.lang.IllegalArgumentException if the params argument is inappropriate.
 
     public void init(
         boolean             encrypting,
         CipherParameters    params)
         throws IllegalArgumentException
     {
         boolean oldEncrypting = this.;
 
         this. = encrypting;
 
         if (params instanceof ParametersWithIV)
         {
             ParametersWithIV ivParam = (ParametersWithIV)params;
             byte[] iv = ivParam.getIV();
 
             if (iv.length != )
             {
                 throw new IllegalArgumentException("initialisation vector must be the same length as block size");
             }
 
             System.arraycopy(iv, 0, , 0, iv.length);
 
             reset();
 
             // if null it's an IV changed only.
             if (ivParam.getParameters() != null)
             {
                 .init(encryptingivParam.getParameters());
             }
             else if (oldEncrypting != encrypting)
             {
                 throw new IllegalArgumentException("cannot change encrypting state without providing key.");
             }
         }
         else
         {
             reset();
 
             // if it's null, key is to be reused.
             if (params != null)
             {
                 .init(encryptingparams);
            }
            else if (oldEncrypting != encrypting)
            {
                throw new IllegalArgumentException("cannot change encrypting state without providing key.");
            }
        }
    }

    
return the algorithm name and mode.

Returns:
the name of the underlying algorithm followed by "/CBC".
    public String getAlgorithmName()
    {
        return .getAlgorithmName() + "/CBC";
    }

    
return the block size of the underlying cipher.

Returns:
the block size of the underlying cipher.
    public int getBlockSize()
    {
        return .getBlockSize();
    }

    
Process one block of input from the array in and write it to the out array.

Parameters:
in the array containing the input data.
inOff offset into the in array the data starts at.
out the array the output data will be copied into.
outOff the offset into the out array the output will start at.
Returns:
the number of bytes processed and produced.
Throws:
org.bouncycastle.crypto.DataLengthException if there isn't enough data in in, or space in out.
java.lang.IllegalStateException if the cipher isn't initialised.
    public int processBlock(
        byte[]      in,
        int         inOff,
        byte[]      out,
        int         outOff)
    {
        return () ? encryptBlock(ininOffoutoutOff) : decryptBlock(ininOffoutoutOff);
    }

    
reset the chaining vector back to the IV and reset the underlying cipher.
    public void reset()
    {
        System.arraycopy(, 0, , 0, .);
        Arrays.fill(, (byte)0);
        .reset();
    }

    
Do the appropriate chaining step for CBC mode encryption.

Parameters:
in the array containing the data to be encrypted.
inOff offset into the in array the data starts at.
out the array the encrypted data will be copied into.
outOff the offset into the out array the output will start at.
Returns:
the number of bytes processed and produced.
Throws:
org.bouncycastle.crypto.DataLengthException if there isn't enough data in in, or space in out.
java.lang.IllegalStateException if the cipher isn't initialised.
    private int encryptBlock(
        byte[]      in,
        int         inOff,
        byte[]      out,
        int         outOff)
    {
        if ((inOff + ) > in.length)
        {
            throw new DataLengthException("input buffer too short");
        }
        /*
         * XOR the cbcV and the input,
         * then encrypt the cbcV
         */
        for (int i = 0; i < i++)
        {
            [i] ^= in[inOff + i];
        }
        int length = .processBlock(, 0, outoutOff);
        /*
         * copy ciphertext to cbcV
         */
        System.arraycopy(outoutOff, 0, .);
        return length;
    }

    
Do the appropriate chaining step for CBC mode decryption.

Parameters:
in the array containing the data to be decrypted.
inOff offset into the in array the data starts at.
out the array the decrypted data will be copied into.
outOff the offset into the out array the output will start at.
Returns:
the number of bytes processed and produced.
Throws:
org.bouncycastle.crypto.DataLengthException if there isn't enough data in in, or space in out.
java.lang.IllegalStateException if the cipher isn't initialised.
    private int decryptBlock(
        byte[]      in,
        int         inOff,
        byte[]      out,
        int         outOff)
    {
        if ((inOff + ) > in.length)
        {
            throw new DataLengthException("input buffer too short");
        }
        System.arraycopy(ininOff, 0, );
        int length = .processBlock(ininOffoutoutOff);
        /*
         * XOR the cbcV and the output
         */
        for (int i = 0; i < i++)
        {
            out[outOff + i] ^= [i];
        }
        /*
         * swap the back up buffer into next position
         */
        byte[]  tmp;
        tmp = ;
         = ;
         = tmp;
        return length;
    }
New to GrepCode? Check out our FAQ X