Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.bouncycastle.crypto.modes;
  
implements a Cipher-FeedBack (CFB) mode on top of a simple cipher.
 
 public class CFBBlockCipher
     extends StreamBlockCipher
 {
     private byte[]          IV;
     private byte[]          cfbV;
     private byte[]          cfbOutV;
     private byte[]          inBuf;
 
     private int             blockSize;
     private BlockCipher     cipher = null;
     private boolean         encrypting;
     private int             byteCount;

    
Basic constructor.

Parameters:
cipher the block cipher to be used as the basis of the feedback mode.
bitBlockSize the block size in bits (note: a multiple of 8)
 
     public CFBBlockCipher(
         BlockCipher cipher,
         int         bitBlockSize)
     {
         super(cipher);
 
         this. = cipher;
         this. = bitBlockSize / 8;
 
         this. = new byte[cipher.getBlockSize()];
         this. = new byte[cipher.getBlockSize()];
         this. = new byte[cipher.getBlockSize()];
         this. = new byte[];
     }

    
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. An IV which is too short is handled in FIPS compliant fashion.

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
     {
         this. = encrypting;
         
         if (params instanceof ParametersWithIV)
         {
             ParametersWithIV ivParam = (ParametersWithIV)params;
             byte[]      iv = ivParam.getIV();
 
             if (iv.length < .)
             {
                 // prepend the supplied IV with zeros (per FIPS PUB 81)
                 System.arraycopy(iv, 0, . - iv.lengthiv.length);
                 for (int i = 0; i < . - iv.lengthi++)
                 {
                     [i] = 0;
                 }
             }
             else
             {
                 System.arraycopy(iv, 0, , 0, .);
             }
 
             reset();
 
             // if null it's an IV changed only.
             if (ivParam.getParameters() != null)
             {
                 .init(trueivParam.getParameters());
             }
         }
         else
         {
             reset();
 
             // if it's null, key is to be reused.
             if (params != null)
             {
                .init(trueparams);
            }
        }
    }

    
return the algorithm name and mode.

Returns:
the name of the underlying algorithm followed by "/CFB" and the block size in bits.
    public String getAlgorithmName()
    {
        return .getAlgorithmName() + "/CFB" + ( * 8);
    }
    protected byte calculateByte(byte in)
          throws DataLengthExceptionIllegalStateException
    {
        return () ? encryptByte(in) : decryptByte(in);
    }
    private byte encryptByte(byte in)
    {
        if ( == 0)
        {
            .processBlock(, 0, , 0);
        }
        byte rv = (byte)([] ^ in);
        [++] = rv;
        if ( == )
        {
             = 0;
            System.arraycopy(, 0, . - );
            System.arraycopy(, 0, . - );
        }
        return rv;
    }
    private byte decryptByte(byte in)
    {
        if ( == 0)
        {
            .processBlock(, 0, , 0);
        }
        [] = in;
        byte rv = (byte)([++] ^ in);
        if ( == )
        {
             = 0;
            System.arraycopy(, 0, . - );
            System.arraycopy(, 0, . - );
        }
        return rv;
    }

    
return the block size we are operating at.

Returns:
the block size we are operating at (in bytes).
    public int getBlockSize()
    {
        return ;
    }

    
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)
    {
        processBytes(ininOffoutoutOff);
        return ;
    }

    
Do the appropriate processing for CFB 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.
    public int encryptBlock(
        byte[]      in,
        int         inOff,
        byte[]      out,
        int         outOff)
    {
        processBytes(ininOffoutoutOff);
        return ;
    }

    
Do the appropriate processing for CFB 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 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.
    public int decryptBlock(
        byte[]      in,
        int         inOff,
        byte[]      out,
        int         outOff)
    {
        processBytes(ininOffoutoutOff);
        return ;
    }

    
Return the current state of the initialisation vector.

Returns:
current IV
    public byte[] getCurrentIV()
    {
        return Arrays.clone();
    }

    
reset the chaining vector back to the IV and reset the underlying cipher.
    public void reset()
    {
        System.arraycopy(, 0, , 0, .);
        Arrays.fill(, (byte)0);
         = 0;
        .reset();
    }
New to GrepCode? Check out our FAQ X