1 | /******************************************************************************
|
---|
2 | *
|
---|
3 | * Copyright (c) 1998,99 by Mindbright Technology AB, Stockholm, Sweden.
|
---|
4 | * www.mindbright.se, [email protected]
|
---|
5 | *
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation; either version 2 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This program is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | * GNU General Public License for more details.
|
---|
15 | *
|
---|
16 | *****************************************************************************
|
---|
17 | * $Author: mats $
|
---|
18 | * $Date: 1999/07/19 17:13:49 $
|
---|
19 | * $Name: rel1-2-1 $
|
---|
20 | *****************************************************************************/
|
---|
21 | /*
|
---|
22 | * !!! Author's comment: I don't know if there are any copyright
|
---|
23 | * issues here but this code is so trivial so I guess there are not
|
---|
24 | * (apart from the name RC4 which I believe is a trademark...).
|
---|
25 | */
|
---|
26 | package mindbright.security;
|
---|
27 |
|
---|
28 | public final class RC4 extends Cipher {
|
---|
29 | int x;
|
---|
30 | int y;
|
---|
31 | byte[] state = new byte[256];
|
---|
32 |
|
---|
33 | final int arcfour_byte() {
|
---|
34 | int x;
|
---|
35 | int y;
|
---|
36 | int sx, sy;
|
---|
37 |
|
---|
38 | x = (this.x + 1) & 0xff;
|
---|
39 | sx = (int)state[x];
|
---|
40 | y = (sx + this.y) & 0xff;
|
---|
41 | sy = (int)state[y];
|
---|
42 | this.x = x;
|
---|
43 | this.y = y;
|
---|
44 | state[y] = (byte)(sx & 0xff);
|
---|
45 | state[x] = (byte)(sy & 0xff);
|
---|
46 | return (int)state[((sx + sy) & 0xff)];
|
---|
47 | }
|
---|
48 |
|
---|
49 | public synchronized void encrypt(byte[] src, int srcOff, byte[] dest, int destOff, int len) {
|
---|
50 | int end = srcOff + len;
|
---|
51 | for(int si = srcOff, di = destOff; si < end; si++, di++)
|
---|
52 | dest[di] = (byte)(((int)src[si] ^ arcfour_byte()) & 0xff);
|
---|
53 | }
|
---|
54 |
|
---|
55 | public void decrypt(byte[] src, int srcOff, byte[] dest, int destOff, int len) {
|
---|
56 | encrypt(src, srcOff, dest, destOff, len);
|
---|
57 | }
|
---|
58 |
|
---|
59 | public void setKey(byte[] key) {
|
---|
60 | int t, u;
|
---|
61 | int keyindex;
|
---|
62 | int stateindex;
|
---|
63 | int counter;
|
---|
64 |
|
---|
65 | for(counter = 0; counter < 256; counter++)
|
---|
66 | state[counter] = (byte)counter;
|
---|
67 | keyindex = 0;
|
---|
68 | stateindex = 0;
|
---|
69 | for(counter = 0; counter < 256; counter++) {
|
---|
70 | t = (int)state[counter];
|
---|
71 | stateindex = (stateindex + key[keyindex] + t) & 0xff;
|
---|
72 | u = (int)state[stateindex];
|
---|
73 | state[stateindex] = (byte)(t & 0xff);
|
---|
74 | state[counter] = (byte)(u & 0xff);
|
---|
75 | if(++keyindex >= key.length)
|
---|
76 | keyindex = 0;
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | }
|
---|