Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.bouncycastle.crypto.paddings;
  
A wrapper class that allows block ciphers to be used to process data in a piecemeal fashion with padding. The PaddedBufferedBlockCipher 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 default padding mechanism used is the one outlined in PKCS5/PKCS7.
 
     extends BufferedBlockCipher
 {
     BlockCipherPadding  padding;

    
Create a buffered block cipher with the desired padding.

Parameters:
cipher the underlying block cipher this buffering object wraps.
padding the padding type.
 
     public PaddedBufferedBlockCipher(
         BlockCipher         cipher,
         BlockCipherPadding  padding)
     {
         this. = cipher;
         this. = padding;
 
          = new byte[cipher.getBlockSize()];
          = 0;
     }

    
Create a buffered block cipher PKCS7 padding

Parameters:
cipher the underlying block cipher this buffering object wraps.
 
     public PaddedBufferedBlockCipher(
         BlockCipher     cipher)
     {
         this(ciphernew PKCS7Padding());
     }

    
initialise the cipher.

Parameters:
forEncryption 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             forEncryption,
         CipherParameters    params)
         throws IllegalArgumentException
     {
         this. = forEncryption;
 
         reset();
 
         if (params instanceof ParametersWithRandom)
         {
             ParametersWithRandom    p = (ParametersWithRandom)params;
 
             .init(p.getRandom());
 
             .init(forEncryptionp.getParameters());
         }
         else
         {
             .init(null);
 
             .init(forEncryptionparams);
         }
     }

    
return the minimum size of the output buffer required for an update plus a doFinal with an input of len bytes.

Parameters:
len the length of the input.
Returns:
the space required to accommodate a call to update and doFinal with len bytes of input.
 
     public int getOutputSize(
         int len)
     {
         int total       = len + ;
         int leftOver    = total % .;
 
         if (leftOver == 0)
        {
            if ()
            {
                return total + .;
            }
            return total;
        }
        return total - leftOver + .;
    }

    
return the size of the output buffer required for an update an input of len bytes.

Parameters:
len the length of the input.
Returns:
the space required to accommodate a call to update with len bytes of input.
    public int getUpdateOutputSize(
        int len)
    {
        int total       = len + ;
        int leftOver    = total % .;
        if (leftOver == 0)
        {
            return Math.max(0, total - .);
        }
        return total - leftOver;
    }

    
process a single byte, producing an output block if neccessary.

Parameters:
in the input byte.
out the space for any output that might be produced.
outOff the offset from which the output will be copied.
Returns:
the number of output bytes copied to out.
Throws:
org.bouncycastle.crypto.DataLengthException if there isn't enough space in out.
java.lang.IllegalStateException if the cipher isn't initialised.
    public int processByte(
        byte        in,
        byte[]      out,
        int         outOff)
    {
        int         resultLen = 0;
        if ( == .)
        {
            resultLen = .processBlock(, 0, outoutOff);
             = 0;
        }
        [++] = in;
        return resultLen;
    }

    
process an array of bytes, producing output if necessary.

Parameters:
in the input byte array.
inOff the offset at which the input data starts.
len the number of bytes to be copied out of the input array.
out the space for any output that might be produced.
outOff the offset from which the output will be copied.
Returns:
the number of output bytes copied to out.
Throws:
org.bouncycastle.crypto.DataLengthException if there isn't enough space in out.
java.lang.IllegalStateException if the cipher isn't initialised.
    public int processBytes(
        byte[]      in,
        int         inOff,
        int         len,
        byte[]      out,
        int         outOff)
    {
        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 OutputLengthException("output buffer too short");
            }
        }
        int resultLen = 0;
        int gapLen = . - ;
        if (len > gapLen)
        {
            System.arraycopy(ininOffgapLen);
            resultLen += .processBlock(, 0, outoutOff);
             = 0;
            len -= gapLen;
            inOff += gapLen;
            while (len > .)
            {
                resultLen += .processBlock(ininOffoutoutOff + resultLen);
                len -= blockSize;
                inOff += blockSize;
            }
        }
        System.arraycopy(ininOfflen);
         += len;
        return resultLen;
    }

    
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.

Parameters:
out the array the block currently being held is copied into.
outOff the offset at which the copying starts.
Returns:
the number of output bytes copied to out.
Throws:
org.bouncycastle.crypto.DataLengthException if there is insufficient space in out for the output or we are decrypting and the input is not block size aligned.
java.lang.IllegalStateException if the underlying cipher is not initialised.
org.bouncycastle.crypto.InvalidCipherTextException if padding is expected and not found.
    public int doFinal(
        byte[]  out,
        int     outOff)
    {
        int blockSize = .getBlockSize();
        int resultLen = 0;
        if ()
        {
            if ( == blockSize)
            {
                if ((outOff + 2 * blockSize) > out.length)
                {
                    reset();
                    throw new OutputLengthException("output buffer too short");
                }
                resultLen = .processBlock(, 0, outoutOff);
                 = 0;
            }
            .addPadding();
            resultLen += .processBlock(, 0, outoutOff + resultLen);
            reset();
        }
        else
        {
            if ( == blockSize)
            {
                resultLen = .processBlock(, 0, , 0);
                 = 0;
            }
            else
            {
                reset();
                throw new DataLengthException("last block incomplete in decryption");
            }
            try
            {
                resultLen -= .padCount();
                System.arraycopy(, 0, outoutOffresultLen);
            }
            finally
            {
                reset();
            }
        }
        return resultLen;
    }
New to GrepCode? Check out our FAQ X