-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathARC4.h
More file actions
44 lines (33 loc) · 682 Bytes
/
ARC4.h
File metadata and controls
44 lines (33 loc) · 682 Bytes
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
#include <string>
#ifndef ARC4_H_
#define ARC4_H_
class RC4
{
public:
static std::string rc4_perform(const std::string& input, const std::string& key) {
std::string out;
out.resize(input.size());
int x{}, y{}, j{};
unsigned char box[256];
for (int i = 0; i < 256; i++)
box[i] = (char)i;
for (int i = 0; i < 256; i++)
{
j = (key[i % key.size()] + box[i] + j) % 256;
x = box[i];
box[i] = box[j];
box[j] = x;
}
for (int i{}; i < input.size(); i++)
{
y = (i + 1) % 256;
j = (box[y] + j) % 256;
x = box[y];
box[y] = box[j];
box[j] = x;
out[i] = char(input[i] ^ box[(box[y] + box[j]) % 256]);
}
return out;
}
};
#endif