1 package org.bouncycastle.crypto.engines;
\r
3 import org.bouncycastle.crypto.StreamCipher;
\r
4 import org.bouncycastle.crypto.CipherParameters;
\r
5 import org.bouncycastle.crypto.DataLengthException;
\r
6 import org.bouncycastle.crypto.params.KeyParameter;
\r
8 public class RC4Engine implements StreamCipher
\r
10 private final static int STATE_LENGTH = 256;
\r
13 * variables to hold the state of the RC4 engine
\r
14 * during encryption and decryption
\r
17 private byte[] engineState = null;
\r
20 private byte[] workingKey = null;
\r
23 * initialise a RC4 cipher.
\r
25 * @param forEncryption whether or not we are for encryption.
\r
26 * @param params the parameters required to set up the cipher.
\r
27 * @exception IllegalArgumentException if the params argument is
\r
31 boolean forEncryption,
\r
32 CipherParameters params
\r
35 if (params instanceof KeyParameter)
\r
38 * RC4 encryption and decryption is completely
\r
39 * symmetrical, so the 'forEncryption' is
\r
42 workingKey = ((KeyParameter)params).getKey();
\r
48 throw new IllegalArgumentException("invalid parameter passed to RC4 init - " + params.getClass().getName());
\r
51 public String getAlgorithmName()
\r
56 public byte returnByte(byte in)
\r
59 y = (engineState[x] + y) & 0xff;
\r
62 byte tmp = engineState[x];
\r
63 engineState[x] = engineState[y];
\r
64 engineState[y] = tmp;
\r
67 return (byte)(in ^ engineState[(engineState[x] + engineState[y]) & 0xff]);
\r
70 public void processBytes(
\r
78 if ((inOff + len) > in.length)
\r
80 throw new DataLengthException("input buffer too short");
\r
83 if ((outOff + len) > out.length)
\r
85 throw new DataLengthException("output buffer too short");
\r
88 for (int i = 0; i < len ; i++)
\r
91 y = (engineState[x] + y) & 0xff;
\r
94 byte tmp = engineState[x];
\r
95 engineState[x] = engineState[y];
\r
96 engineState[y] = tmp;
\r
99 out[i+outOff] = (byte)(in[i + inOff]
\r
100 ^ engineState[(engineState[x] + engineState[y]) & 0xff]);
\r
104 public void reset()
\r
106 setKey(workingKey);
\r
109 // Private implementation
\r
111 private void setKey(byte[] keyBytes)
\r
113 workingKey = keyBytes;
\r
115 // System.out.println("the key length is ; "+ workingKey.length);
\r
120 if (engineState == null)
\r
122 engineState = new byte[STATE_LENGTH];
\r
125 // reset the state of the engine
\r
126 for (int i=0; i < STATE_LENGTH; i++)
\r
128 engineState[i] = (byte)i;
\r
134 for (int i=0; i < STATE_LENGTH; i++)
\r
136 i2 = ((keyBytes[i1] & 0xff) + engineState[i] + i2) & 0xff;
\r
137 // do the byte-swap inline
\r
138 byte tmp = engineState[i];
\r
139 engineState[i] = engineState[i2];
\r
140 engineState[i2] = tmp;
\r
141 i1 = (i1+1) % keyBytes.length;
\r