FileDocCategorySizeDatePackage
SICBlockCipher.javaAPI DocAndroid 1.5 API2867Wed May 06 22:41:06 BST 2009org.bouncycastle.crypto.modes

SICBlockCipher

public class SICBlockCipher extends Object implements org.bouncycastle.crypto.BlockCipher
Implements the Segmented Integer Counter (SIC) mode on top of a simple block cipher. This mode is also known as CTR mode.

Fields Summary
private final org.bouncycastle.crypto.BlockCipher
cipher
private final int
blockSize
private byte[]
IV
private byte[]
counter
private byte[]
counterOut
Constructors Summary
public SICBlockCipher(org.bouncycastle.crypto.BlockCipher c)
Basic constructor.

param
c the block cipher to be used.

        this.cipher = c;
        this.blockSize = cipher.getBlockSize();
        this.IV = new byte[blockSize];
        this.counter = new byte[blockSize];
        this.counterOut = new byte[blockSize];
    
Methods Summary
public java.lang.StringgetAlgorithmName()

        return cipher.getAlgorithmName() + "/SIC";
    
public intgetBlockSize()

        return cipher.getBlockSize();
    
public org.bouncycastle.crypto.BlockCiphergetUnderlyingCipher()
return the underlying block cipher that we are wrapping.

return
the underlying block cipher that we are wrapping.

        return cipher;
    
public voidinit(boolean forEncryption, org.bouncycastle.crypto.CipherParameters params)

        if (params instanceof ParametersWithIV)
        {
          ParametersWithIV ivParam = (ParametersWithIV)params;
          byte[]           iv      = ivParam.getIV();
          System.arraycopy(iv, 0, IV, 0, IV.length);

          reset();
          cipher.init(true, ivParam.getParameters());
        }
    
public intprocessBlock(byte[] in, int inOff, byte[] out, int outOff)

        cipher.processBlock(counter, 0, counterOut, 0);

        //
        // XOR the counterOut with the plaintext producing the cipher text
        //
        for (int i = 0; i < counterOut.length; i++)
        {
          out[outOff + i] = (byte)(counterOut[i] ^ in[inOff + i]);
        }

        int    carry = 1;
        
        for (int i = counter.length - 1; i >= 0; i--)
        {
            int    x = (counter[i] & 0xff) + carry;
            
            if (x > 0xff)
            {
                carry = 1;
            }
            else
            {
                carry = 0;
            }
            
            counter[i] = (byte)x;
        }

        return counter.length;
    
public voidreset()

        System.arraycopy(IV, 0, counter, 0, counter.length);
        cipher.reset();