-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRC4.cs
More file actions
32 lines (27 loc) · 751 Bytes
/
Copy pathRC4.cs
File metadata and controls
32 lines (27 loc) · 751 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
class RC4
{
public static byte[] rc4(byte[] input, byte[] key)
{
byte[] encrypted = new byte[input.Length];
int x, y, j = 0;
int[] box = new int[256];
for (int i = 0; i < 256; i++) box[i] = i;
for (int i = 0; i < 256; i++)
{
j = (key[i % key.Length] + box[i] + j) % 256;
x = box[i];
box[i] = box[j];
box[j] = x;
}
for (int i = 0; i < input.Length; i++)
{
y = (i + 1) % 256;
j = (box[y] + j) % 256;
x = box[y];
box[y] = box[j];
box[j] = x;
encrypted[i] = (byte)(input[i] ^ box[(box[y] + box[j]) % 256]);
}
return encrypted;
}
}