FileDocCategorySizeDatePackage
PaddedBlockCipher.javaAPI DocAzureus 3.0.3.47280Tue Jun 08 05:12:58 BST 2004org.bouncycastle.crypto.modes

PaddedBlockCipher

public class PaddedBlockCipher extends org.bouncycastle.crypto.BufferedBlockCipher
A wrapper class that allows block ciphers to be used to process data in a piecemeal fashion with PKCS5/PKCS7 padding. The PaddedBlockCipher outputs a block only when the buffer is full and more data is being added, or on a doFinal (unless the current block in the buffer is a pad block). The padding mechanism used is the one outlined in PKCS5/PKCS7.
deprecated
use org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher instead.

Fields Summary
Constructors Summary
public PaddedBlockCipher(org.bouncycastle.crypto.BlockCipher cipher)
Create a buffered block cipher with, or without, padding.

param
cipher the underlying block cipher this buffering object wraps.

        this.cipher = cipher;

        buf = new byte[cipher.getBlockSize()];
        bufOff = 0;
	
Methods Summary
public intdoFinal(byte[] out, int outOff)
Process the last block in the buffer. If the buffer is currently full and padding needs to be added a call to doFinal will produce 2 * getBlockSize() bytes.

param
out the array the block currently being held is copied into.
param
outOff the offset at which the copying starts.
exception
DataLengthException if there is insufficient space in out for the output or we are decrypting and the input is not block size aligned.
exception
IllegalStateException if the underlying cipher is not initialised.
exception
InvalidCipherTextException if padding is expected and not found.

        int blockSize = cipher.getBlockSize();
        int resultLen = 0;

        if (forEncryption)
        {
            if (bufOff == blockSize)
            {
                if ((outOff + 2 * blockSize) > out.length)
                {
                    throw new DataLengthException("output buffer too short");
                }

                resultLen = cipher.processBlock(buf, 0, out, outOff);
                bufOff = 0;
            }

            //
            // add PKCS7 padding
            //
            byte code = (byte)(blockSize - bufOff);

            while (bufOff < blockSize)
            {
                buf[bufOff] = code;
                bufOff++;
            }

            resultLen += cipher.processBlock(buf, 0, out, outOff + resultLen);
        }
        else
        {
            if (bufOff == blockSize)
            {
                resultLen = cipher.processBlock(buf, 0, buf, 0);
                bufOff = 0;
            }
            else
            {
                throw new DataLengthException("last block incomplete in decryption");
            }

            //
            // remove PKCS7 padding
            //
            int count = buf[blockSize - 1] & 0xff;

            if ((count < 0) || (count > blockSize))
            {
                throw new InvalidCipherTextException("pad block corrupted");
            }

            resultLen -= count;

            System.arraycopy(buf, 0, out, outOff, resultLen);
        }

        reset();

		return resultLen;
	
public intgetOutputSize(int len)
return the size of the output buffer required for an update plus a doFinal with an input of len bytes.

param
len the length of the input.
return
the space required to accommodate a call to update and doFinal with len bytes of input.

        int total       = len + bufOff;
        int leftOver    = total % buf.length;

        if (leftOver == 0)
        {
            if (forEncryption)
            {
                return total + buf.length;
            }

            return total;
        }

        return total - leftOver + buf.length;
    
public intgetUpdateOutputSize(int len)
return the size of the output buffer required for an update an input of len bytes.

param
len the length of the input.
return
the space required to accommodate a call to update with len bytes of input.

        int total       = len + bufOff;
        int leftOver    = total % buf.length;

        if (leftOver == 0)
        {
            return total - buf.length;
        }

        return total - leftOver;
    
public intprocessByte(byte in, byte[] out, int outOff)
process a single byte, producing an output block if neccessary.

param
in the input byte.
param
out the space for any output that might be produced.
param
outOff the offset from which the output will be copied.
exception
DataLengthException if there isn't enough space in out.
exception
IllegalStateException if the cipher isn't initialised.

        int         resultLen = 0;

        if (bufOff == buf.length)
        {
            resultLen = cipher.processBlock(buf, 0, out, outOff);
            bufOff = 0;
        }

        buf[bufOff++] = in;

        return resultLen;
    
public intprocessBytes(byte[] in, int inOff, int len, byte[] out, int outOff)
process an array of bytes, producing output if necessary.

param
in the input byte array.
param
inOff the offset at which the input data starts.
param
len the number of bytes to be copied out of the input array.
param
out the space for any output that might be produced.
param
outOff the offset from which the output will be copied.
exception
DataLengthException if there isn't enough space in out.
exception
IllegalStateException if the cipher isn't initialised.

        if (len < 0)
        {
            throw new IllegalArgumentException("Can't have a negative input length!");
        }

        int blockSize   = getBlockSize();
        int length      = getUpdateOutputSize(len);
        
        if (length > 0)
        {
            if ((outOff + length) > out.length)
            {
                throw new DataLengthException("output buffer too short");
            }
        }

		int resultLen = 0;
        int gapLen = buf.length - bufOff;

        if (len > gapLen)
        {
            System.arraycopy(in, inOff, buf, bufOff, gapLen);

            resultLen += cipher.processBlock(buf, 0, out, outOff);

            bufOff = 0;
            len -= gapLen;
            inOff += gapLen;

            while (len > buf.length)
            {
                resultLen += cipher.processBlock(in, inOff, out, outOff + resultLen);

                len -= blockSize;
                inOff += blockSize;
            }
        }

        System.arraycopy(in, inOff, buf, bufOff, len);

        bufOff += len;

        return resultLen;