forked from Pusty/Obfuscat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRC4.java
71 lines (54 loc) · 1.48 KB
/
RC4.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* RC4 Implementation - Merged Functions Example
*/
public class RC4 {
private static void swap(byte[] array, int a, int b) {
byte tmp = array[a];
array[a] = array[b];
array[b] = tmp;
}
private static void KSA(byte[] key, byte[] S) {
int len_key = 8;
int j = 0;
for(int i = 0; i < 256; i++)
S[i] = (byte) i;
for(int i = 0; i < 256; i++) {
j = (j + (S[i]&0xFF) + (key[i % len_key]&0xFF)) % 256;
swap(S, i, j);
}
}
private static void PRGA(byte[] S, byte[] plaintext, int len) {
int i = 0;
int j = 0;
for(int n = 0; n < len; n++) {
i = (i + 1) % 256;
j = (j + (S[i]&0xFF)) % 256;
swap(S, i, j);
int rnd = S[((S[i]&0xFF) + (S[j]&0xFF)) % 256];
plaintext[n] = (byte) (rnd ^ plaintext[n]);
}
}
// key len = 8
private static void rc4(byte[] key, byte[] plaintext, int len) {
byte[] buffer = new byte[256];
KSA(key, buffer);
PRGA(buffer, plaintext, len);
return;
}
public static int entry(byte[] data, int len) {
byte[] key = new byte[8];
key[0] = 0x01;
key[1] = 0x02;
key[2] = 0x03;
key[3] = 0x04;
key[4] = 0x05;
key[5] = 0x06;
key[6] = 0x07;
key[7] = 0x08;
byte[] tmp = new byte[len];
for(int i=0;i<len;i++)
tmp[i] = data[i];
rc4(key, tmp, len);
return tmp[0]&0xFF;
}
}