Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.bouncycastle.crypto.modes;
  
A Cipher Text Stealing (CTS) mode cipher. CTS allows block ciphers to be used to produce cipher text which is the same length as the plain text.
 
 public class CTSBlockCipher
     extends BufferedBlockCipher
 {
     private int     blockSize;

    
Create a buffered block cipher that uses Cipher Text Stealing

Parameters:
cipher the underlying block cipher this buffering object wraps.
 
     public CTSBlockCipher(
         BlockCipher     cipher)
     {
         if (cipher instanceof StreamBlockCipher)
         {
             throw new IllegalArgumentException("CTSBlockCipher can only accept ECB, or CBC ciphers");
         }
 
         this. = cipher;
 
          = cipher.getBlockSize();
 
          = new byte[ * 2];
          = 0;
     }

    
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 total - .;
         }
 
         return total - leftOver;
     }

    
return the 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)
     {
         return len + ;
     }

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

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)
         throws DataLengthExceptionIllegalStateException
     {
         int         resultLen = 0;
 
         if ( == .)
         {
             resultLen = .processBlock(, 0, outoutOff);
             System.arraycopy(, 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 DataLengthException("output buffer too short");
            }
        }
        int resultLen = 0;
        int gapLen = . - ;
        if (len > gapLen)
        {
            System.arraycopy(ininOffgapLen);
            resultLen += .processBlock(, 0, outoutOff);
            System.arraycopy(blockSize, 0, blockSize);
             = blockSize;
            len -= gapLen;
            inOff += gapLen;
            while (len > blockSize)
            {
                System.arraycopy(ininOffblockSize);
                resultLen += .processBlock(, 0, outoutOff + resultLen);
                System.arraycopy(blockSize, 0, blockSize);
                len -= blockSize;
                inOff += blockSize;
            }
        }
        System.arraycopy(ininOfflen);
         += len;
        return resultLen;
    }

    
Process the last block in the buffer.

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.
java.lang.IllegalStateException if the underlying cipher is not initialised.
org.bouncycastle.crypto.InvalidCipherTextException if cipher text decrypts wrongly (in case the exception will never get thrown).
    public int doFinal(
        byte[]  out,
        int     outOff)
    {
        if ( + outOff > out.length)
        {
            throw new DataLengthException("output buffer to small in doFinal");
        }
        int     blockSize = .getBlockSize();
        int     len =  - blockSize;
        byte[]  block = new byte[blockSize];
        if ()
        {
            if ( < blockSize)
            {
                throw new DataLengthException("need at least one block of input for CTS");
            }
            .processBlock(, 0, block, 0);
            if ( > blockSize)
            {
                for (int i = i != .i++)
                {
                    [i] = block[i - blockSize];
                }
                for (int i = blockSizei != i++)
                {
                    [i] ^= block[i - blockSize];
                }
                if ( instanceof CBCBlockCipher)
                {
                    BlockCipher c = ((CBCBlockCipher)).getUnderlyingCipher();
                    c.processBlock(blockSizeoutoutOff);
                }
                else
                {
                    .processBlock(blockSizeoutoutOff);
                }
                System.arraycopy(block, 0, outoutOff + blockSizelen);
            }
            else
            {
                System.arraycopy(block, 0, outoutOffblockSize);
            }
        }
        else
        {
            if ( < blockSize)
            {
                throw new DataLengthException("need at least one block of input for CTS");
            }
            byte[]  lastBlock = new byte[blockSize];
            if ( > blockSize)
            {
                if ( instanceof CBCBlockCipher)
                {
                    BlockCipher c = ((CBCBlockCipher)).getUnderlyingCipher();
                    c.processBlock(, 0, block, 0);
                }
                else
                {
                    .processBlock(, 0, block, 0);
                }
                for (int i = blockSizei != i++)
                {
                    lastBlock[i - blockSize] = (byte)(block[i - blockSize] ^ [i]);
                }
                System.arraycopy(blockSizeblock, 0, len);
                .processBlock(block, 0, outoutOff);
                System.arraycopy(lastBlock, 0, outoutOff + blockSizelen);
            }
            else
            {
                .processBlock(, 0, block, 0);
                System.arraycopy(block, 0, outoutOffblockSize);
            }
        }
        int offset = ;
        reset();
        return offset;
    }
New to GrepCode? Check out our FAQ X