-
Notifications
You must be signed in to change notification settings - Fork 1
/
RC4 -Stream-Cipher.cpp
158 lines (115 loc) · 2.93 KB
/
RC4 -Stream-Cipher.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <iostream>
#include <string>
#include<vector>
using namespace std;
vector<int> permute(vector<int>, vector<int>);
string encrypt(vector<int>s , vector<int> t, string p);
string decrypt(vector<int>s, vector<int> t, string p);
int main() {
string plaintext = "cryptology";
string plaintext2 = "RC4";
vector<int> S(256); //Fill with for loop
vector<int> T(256); // Fill with key
int key[] = { 1,2,3,6 };
int key2[] = { 5,7,8,9 };
// Initilize the S and T vectors
int tmp = 0;
for (int i = 0; i < 256;i++) {
S[i] = i;
T[i] = key[( i % (sizeof(key)/sizeof(*key)) )];
}
S = permute(S, T);
for (int i = 0; i < 256 ;i++) {
cout << S[i] << " ";
if ((i + 1) % 16 == 0)
cout << endl;
}
cout << endl;
string p = encrypt(S, T, plaintext);
cout << "Message: " << plaintext << endl;
cout << "Encrypted Message: " << " " << p << endl;
cout << "Decrypted Message: " << decrypt(S, T, p) << endl << endl;
//Init S and T
tmp = 0;
for (int i = 0; i < 256;i++) {
S[i] = i;
T[i] = key2[(i % (sizeof(key) / sizeof(*key)))];
}
S = permute(S, T);
for (int i = 0; i < 256;i++) {
cout << S[i] << " ";
if ((i + 1) % 16 == 0)
cout << endl;
}
cout << endl;
p = encrypt(S, T, plaintext2);
cout << "Message: " << plaintext2 << endl;
cout << "Encrypted Message: " << p << endl;
cout << "Decrypted Message: "<<decrypt(S, T, p) << endl << endl;
system("pause");
return 0;
}
string decrypt(vector<int>s, vector<int> t, string p) {
int i = 0;
int j = 0;
int tmp = 0;
int k = 0;
//temp variables
int b;
int c;
int * plain = new int[p.length()];
string plain_T;
for (int r = 0; r < p.length(); r++) {
i = (i + 1) % 256;
j = (j + s[i]) % 256;
// swap section
b = s[i];
s[i] = s[j];
s[j] = b;
tmp = (s[i] + s[j]) % 256;
k = s[tmp];
c = ((int)p[r] ^ k); // cast the p string as and int then xor with k
plain[r] = c;
plain_T += (char)plain[r]; // Cast plaintext int array as a char
}
return plain_T;
}
string encrypt(vector<int>s, vector<int> t, string p) {
int i = 0;
int j = 0;
int tmp = 0;
int k = 0;
//Temp variables
int b;
int c;
int * cipher = new int [p.length()];
string cipher_T;
cout << "Keys Generated for plaintext: ";
for (int r = 0; r < p.length(); r++) {
i = (i + 1) % 256;
j = (j + s[i]) % 256;
// swap section
b = s[i];
s[i] = s[j];
s[j] = b;
tmp = (s[i] + s[j]) % 256;
k = s[tmp];
cout << k << " ";
c = ((int)p[r] ^ k); //Cast p char as an int then xor with k
cipher[r] = c;
cipher_T += (char)cipher[r]; //cast int as char then append to string
}
cout << endl;
return cipher_T;
}
vector<int> permute(vector<int> s, vector<int> t) {
int j = 0;
int tmp;
for (int i = 0; i< 256; i++) {
j = (j + s[i] + t[i]) % 256;
tmp = s[i];
s[i] = s[j];
s[j] = tmp;
}
return s;
}