KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > crypto > engines > RC4Engine


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.crypto.engines;
21
22 public class RC4Engine
23 {
24     private final static int STATE_LENGTH = 256;
25
26     /*
27      * variables to hold the state of the RC4 engine
28      * during encryption and decryption
29      */

30
31     private byte[] engineState = null;
32     private int x = 0;
33     private int y = 0;
34     private byte[] workingKey = null;
35
36     /**
37      * initialise a RC4 cipher.
38      *
39      * @param forEncryption whether or not we are for encryption.
40      * @param params the parameters required to set up the cipher.
41      * @exception IllegalArgumentException if the params argument is
42      * inappropriate.
43      */

44     public void init(
45         boolean forEncryption,
46         byte[] key
47     )
48     {
49             /*
50              * RC4 encryption and decryption is completely
51              * symmetrical, so the 'forEncryption' is
52              * irrelevant.
53              */

54             workingKey = key;
55             setKey(workingKey);
56             return;
57     }
58
59     public String JavaDoc getAlgorithmName()
60     {
61         return "RC4";
62     }
63
64     public byte returnByte(byte in)
65     {
66         x = (x + 1) & 0xff;
67         y = (engineState[x] + y) & 0xff;
68
69         // swap
70
byte tmp = engineState[x];
71         engineState[x] = engineState[y];
72         engineState[y] = tmp;
73
74         // xor
75
return (byte)(in ^ engineState[(engineState[x] + engineState[y]) & 0xff]);
76     }
77
78     public void processBytes(
79         byte[] in,
80         int inOff,
81         int len,
82         byte[] out,
83         int outOff
84     )
85     {
86         if ((inOff + len) > in.length)
87         {
88             throw new RuntimeException JavaDoc("input buffer too short");
89         }
90
91         if ((outOff + len) > out.length)
92         {
93             throw new RuntimeException JavaDoc("output buffer too short");
94         }
95
96         for (int i = 0; i < len ; i++)
97         {
98             x = (x + 1) & 0xff;
99             y = (engineState[x] + y) & 0xff;
100
101             // swap
102
byte tmp = engineState[x];
103             engineState[x] = engineState[y];
104             engineState[y] = tmp;
105
106             // xor
107
out[i+outOff] = (byte)(in[i + inOff]
108                     ^ engineState[(engineState[x] + engineState[y]) & 0xff]);
109         }
110     }
111
112     public void reset()
113     {
114         setKey(workingKey);
115     }
116
117     // Private implementation
118

119     private void setKey(byte[] keyBytes)
120     {
121         workingKey = keyBytes;
122
123         // System.out.println("the key length is ; "+ workingKey.length);
124

125         x = 0;
126         y = 0;
127
128         if (engineState == null)
129         {
130             engineState = new byte[STATE_LENGTH];
131         }
132
133         // reset the state of the engine
134
for (int i=0; i < STATE_LENGTH; i++)
135         {
136             engineState[i] = (byte)i;
137         }
138
139         int i1 = 0;
140         int i2 = 0;
141
142         for (int i=0; i < STATE_LENGTH; i++)
143         {
144             i2 = ((keyBytes[i1] & 0xff) + engineState[i] + i2) & 0xff;
145             // do the byte-swap inline
146
byte tmp = engineState[i];
147             engineState[i] = engineState[i2];
148             engineState[i2] = tmp;
149             i1 = (i1+1) % keyBytes.length;
150         }
151     }
152 }
153
Popular Tags