CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/data/headers/windows/rc4.h
Views: 1904
1
//
2
// License:
3
// https://github.com/rapid7/metasploit-framework/blob/master/LICENSE
4
//
5
6
// This code was originally obtained and modified from the following source
7
// by Bobin Verton:
8
// https://gist.github.com/rverton/a44fc8ca67ab9ec32089
9
10
#define N 256 // 2^8
11
12
void swap(unsigned char *a, unsigned char *b) {
13
int tmp = *a;
14
*a = *b;
15
*b = tmp;
16
}
17
18
int KSA(char *key, unsigned char *S) {
19
int len = strlen(key);
20
int j = 0;
21
22
for (int i = 0; i < N; i++) {
23
S[i] = i;
24
}
25
26
for (int i = 0; i < N; i++) {
27
j = (j + S[i] + key[i % len]) % N;
28
swap(&S[i], &S[j]);
29
}
30
31
return 0;
32
}
33
34
int PRGA(unsigned char *S, char *plaintext, unsigned char *ciphertext, int plainTextSize) {
35
int i = 0;
36
int j = 0;
37
38
for (size_t n = 0, len = plainTextSize; n < len; n++) {
39
i = (i + 1) % N;
40
j = (j + S[i]) % N;
41
swap(&S[i], &S[j]);
42
int rnd = S[(S[i] + S[j]) % N];
43
ciphertext[n] = rnd ^ plaintext[n];
44
}
45
46
return 0;
47
}
48
49
int RC4(char *key, char *plaintext, unsigned char *ciphertext, int plainTextSize) {
50
unsigned char S[N];
51
KSA(key, S);
52
PRGA(S, plaintext, ciphertext, plainTextSize);
53
return 0;
54
}
55