-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharcfour.c
65 lines (50 loc) · 1.38 KB
/
arcfour.c
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
#include "arcfour.h"
export Arcfour *rc4init(int8 *key, int16 size) {
int16 x;
int8 tmp1, tmp2;
Arcfour *p;
if (!(p = malloc(sizeof(struct s_Arcfour)))) {
assert_perror(errno);
}
// Initialize the state array
for (x = 0; x < 256; x++) {
p->s[x] = x;
}
p->i = p->j = p->k = 0;
// Key scheduling algorithm (KSA)
for (p->i = 0, p->j = 0; p->i < 256; p->i++) {
tmp1 = p->i % size;
p->j = (p->j + p->s[p->i] + key[tmp1]) % 256;
// Swap p->s[p->i] and p->s[p->j]
tmp2 = p->s[p->i];
p->s[p->i] = p->s[p->j];
p->s[p->j] = tmp2;
}
p->i = p->j = 0;
rc4whitewash(x, p);
return p;
}
int8 rc4byte(Arcfour *p) {
int8 tmp1, tmp2;
p->i = (p->i + 1) % 256;
p->j = (p->j + p->s[p->i]) % 256;
// Swap p->s[p->i] and p->s[p->j]
tmp1 = p->s[p->i];
p->s[p->i] = p->s[p->j];
p->s[p->j] = tmp1;
tmp1 = (p->s[p->i] + p->s[p->j]) % 256;
p->k = p->s[tmp1];
return p->k;
}
export int8 *rc4encrypt(Arcfour *p, int8 *cleartext, int16 size) {
int8 *ciphertext;
int16 x;
ciphertext = (int8 *)malloc(size + 1);
if (!ciphertext) {
assert_perror(errno);
}
for (x = 0; x < size; x++) {
ciphertext[x] = cleartext[x] ^ rc4byte(p);
}
return ciphertext;
}