Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.bouncycastle.crypto.engines;
  
HC-256 is a software-efficient stream cipher created by Hongjun Wu. It generates keystream from a 256-bit secret key and a 256-bit initialization vector.

http://www.ecrypt.eu.org/stream/p3ciphers/hc/hc256_p3.pdf

Its brother, HC-128, is a third phase candidate in the eStream contest. The algorithm is patent-free. No attacks are known as of today (April 2007). See http://www.ecrypt.eu.org/stream/hcp3.html

 
 public class HC256Engine
     implements StreamCipher
 {
     private int[] p = new int[1024];
     private int[] q = new int[1024];
     private int cnt = 0;
 
     private int step()
     {
         int j =  & 0x3FF;
         int ret;
         if ( < 1024)
         {
             int x = [(j - 3 & 0x3FF)];
             int y = [(j - 1023 & 0x3FF)];
             [j] += [(j - 10 & 0x3FF)]
                 + (rotateRight(x, 10) ^ rotateRight(y, 23))
                 + [((x ^ y) & 0x3FF)];
 
             x = [(j - 12 & 0x3FF)];
             ret = ([x & 0xFF] + [((x >> 8) & 0xFF) + 256]
                 + [((x >> 16) & 0xFF) + 512] + [((x >> 24) & 0xFF) + 768])
                 ^ [j];
         }
         else
         {
             int x = [(j - 3 & 0x3FF)];
             int y = [(j - 1023 & 0x3FF)];
             [j] += [(j - 10 & 0x3FF)]
                 + (rotateRight(x, 10) ^ rotateRight(y, 23))
                 + [((x ^ y) & 0x3FF)];
 
             x = [(j - 12 & 0x3FF)];
             ret = ([x & 0xFF] + [((x >> 8) & 0xFF) + 256]
                 + [((x >> 16) & 0xFF) + 512] + [((x >> 24) & 0xFF) + 768])
                 ^ [j];
         }
          =  + 1 & 0x7FF;
         return ret;
     }
 
     private byte[] keyiv;
     private boolean initialised;
 
     private void init()
     {
         if (. != 32 && . != 16)
         {
             throw new IllegalArgumentException(
                 "The key must be 128/256 bits long");
         }
 
         if (. < 16)
         {
             throw new IllegalArgumentException(
                 "The IV must be at least 128 bits long");
         }
 
         if (. != 32)
         {
             byte[] k = new byte[32];
 
             System.arraycopy(, 0, k, 0, .);
             System.arraycopy(, 0, k, 16, .);
 
              = k;
         }
 
         if (. < 32)
         {
             byte[] newIV = new byte[32];
 
             System.arraycopy(, 0, newIV, 0, .);
             System.arraycopy(, 0, newIV.newIV.length - .);
 
              = newIV;
        }
         = 0;
         = 0;
        int[] w = new int[2560];
        for (int i = 0; i < 32; i++)
        {
            w[i >> 2] |= ([i] & 0xff) << (8 * (i & 0x3));
        }
        for (int i = 0; i < 32; i++)
        {
            w[(i >> 2) + 8] |= ([i] & 0xff) << (8 * (i & 0x3));
        }
        for (int i = 16; i < 2560; i++)
        {
            int x = w[i - 2];
            int y = w[i - 15];
            w[i] = (rotateRight(x, 17) ^ rotateRight(x, 19) ^ (x >>> 10))
                + w[i - 7]
                + (rotateRight(y, 7) ^ rotateRight(y, 18) ^ (y >>> 3))
                + w[i - 16] + i;
        }
        System.arraycopy(w, 512, , 0, 1024);
        System.arraycopy(w, 1536, , 0, 1024);
        for (int i = 0; i < 4096; i++)
        {
            step();
        }
         = 0;
    }
    public String getAlgorithmName()
    {
        return "HC-256";
    }

    
Initialise a HC-256 cipher.

Parameters:
forEncryption whether or not we are for encryption. Irrelevant, as encryption and decryption are the same.
params the parameters required to set up the cipher.
Throws:
java.lang.IllegalArgumentException if the params argument is inappropriate (ie. the key is not 256 bit long).
    public void init(boolean forEncryptionCipherParameters params)
        throws IllegalArgumentException
    {
        CipherParameters keyParam = params;
        if (params instanceof ParametersWithIV)
        {
             = ((ParametersWithIV)params).getIV();
            keyParam = ((ParametersWithIV)params).getParameters();
        }
        else
        {
             = new byte[0];
        }
        if (keyParam instanceof KeyParameter)
        {
             = ((KeyParameter)keyParam).getKey();
            init();
        }
        else
        {
            throw new IllegalArgumentException(
                "Invalid parameter passed to HC256 init - "
                    + params.getClass().getName());
        }
         = true;
    }
    private byte[] buf = new byte[4];
    private int idx = 0;
    private byte getByte()
    {
        if ( == 0)
        {
            int step = step();
            [0] = (byte)(step & 0xFF);
            step >>= 8;
            [1] = (byte)(step & 0xFF);
            step >>= 8;
            [2] = (byte)(step & 0xFF);
            step >>= 8;
            [3] = (byte)(step & 0xFF);
        }
        byte ret = [];
         =  + 1 & 0x3;
        return ret;
    }
    public int processBytes(byte[] inint inOffint lenbyte[] out,
                             int outOffthrows DataLengthException
    {
        if (!)
        {
            throw new IllegalStateException(getAlgorithmName()
                + " not initialised");
        }
        if ((inOff + len) > in.length)
        {
            throw new DataLengthException("input buffer too short");
        }
        if ((outOff + len) > out.length)
        {
            throw new OutputLengthException("output buffer too short");
        }
        for (int i = 0; i < leni++)
        {
            out[outOff + i] = (byte)(in[inOff + i] ^ getByte());
        }
        return len;
    }
    public void reset()
    {
        init();
    }
    public byte returnByte(byte in)
    {
        return (byte)(in ^ getByte());
    }
    private static int rotateRight(
        int     x,
        int     bits)
    {
        return (x >>> bits) | (x << -bits);
    }
New to GrepCode? Check out our FAQ X