FileDocCategorySizeDatePackage
RC4Engine.javaAPI DocAzureus 3.0.3.43871Thu Jan 11 07:46:52 GMT 2007org.bouncycastle.crypto.engines

RC4Engine

public class RC4Engine extends Object implements org.bouncycastle.crypto.StreamCipher

Fields Summary
private static final int
STATE_LENGTH
private byte[]
engineState
private int
x
private int
y
private byte[]
workingKey
Constructors Summary
Methods Summary
public java.lang.StringgetAlgorithmName()

        return "RC4";
    
public voidinit(boolean forEncryption, org.bouncycastle.crypto.CipherParameters params)
initialise a RC4 cipher.

param
forEncryption whether or not we are for encryption.
param
params the parameters required to set up the cipher.
exception
IllegalArgumentException if the params argument is inappropriate.


                                        
      
                      
             
    
    
        if (params instanceof KeyParameter)
        {
            /* 
             * RC4 encryption and decryption is completely
             * symmetrical, so the 'forEncryption' is 
             * irrelevant.
             */
            workingKey = ((KeyParameter)params).getKey();
            setKey(workingKey);

            return;
        }

        throw new IllegalArgumentException("invalid parameter passed to RC4 init - " + params.getClass().getName());
    
public voidprocessBytes(byte[] in, int inOff, int len, byte[] out, int outOff)

        for (int i = 0; i < len ; i++)
        {
            x = (x + 1) & 0xff;
            y = (engineState[x] + y) & 0xff;

            // swap
            byte tmp = engineState[x];
            engineState[x] = engineState[y];
            engineState[y] = tmp;

            // xor
            out[i+outOff] = (byte)(in[i + inOff]
                    ^ engineState[(engineState[x] + engineState[y]) & 0xff]);
        }
    
public voidreset()

        setKey(workingKey);
    
public bytereturnByte(byte in)

        x = (x + 1) & 0xff;
        y = (engineState[x] + y) & 0xff;

        // swap
        byte tmp = engineState[x];
        engineState[x] = engineState[y];
        engineState[y] = tmp;

        // xor
        return (byte)(in ^ engineState[(engineState[x] + engineState[y]) & 0xff]);
    
private voidsetKey(byte[] keyBytes)

        workingKey = keyBytes;

        // System.out.println("the key length is ; "+ workingKey.length);

        x = 0;
        y = 0;

        if (engineState == null)
        {
            engineState = new byte[STATE_LENGTH];
        }

        // reset the state of the engine
        for (int i=0; i < STATE_LENGTH; i++)
        {
            engineState[i] = (byte)i;
        }
        
        int i1 = 0;
        int i2 = 0;

        for (int i=0; i < STATE_LENGTH; i++)
        {
            i2 = ((keyBytes[i1] & 0xff) + engineState[i] + i2) & 0xff;
            // do the byte-swap inline
            byte tmp = engineState[i];
            engineState[i] = engineState[i2];
            engineState[i2] = tmp;
            i1 = (i1+1) % keyBytes.length; 
        }