-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
441 lines (384 loc) · 9.74 KB
/
Copy pathmain.cpp
File metadata and controls
441 lines (384 loc) · 9.74 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int SolveX(int &x1, int x2) {
int output = x1 % x2;
x1 = x2;
return output;
}
int SolveY(int x1, int x2) {
return x1 / x2;
}
int SolveB(int &b2, int &b3, int y) {
int output = b2 - (b3 * y);
b2 = b3;
return output;
}
int SolveEuclide(int x, int a) {
int b = 1, b2 = 0, y = 0;
while (a > 1){
y = SolveY(x, a);
b = SolveB(b2, b, y);
a = SolveX(x, a);
}
if (b < 0)
b = (b + 26) % 26;
return b;
}
int AffineDecoding_Encoding(int x, int k1, int k2, bool type) {
int output = 0;
if (type) {
output = SolveEuclide(26, k1) * (x - k2) % 26;
if (output >= 0)
return output;
return 26 + output;
}
else
return (k1 * x + k2) % 26;
}
string SolveAffine() {
int k1, k2;
bool decoding = true;
string word = "";
cout << "Input word: ";
getline(cin, word);
cout << "\nType of Affine (0 for decoding, 1 for encoding), k1, k2: ";
cin >> decoding >> k1 >> k2;
string output = "";
for (int i = 0; i < word.size(); i++) {
if (word[i] != ' ')
output += char(AffineDecoding_Encoding(word[i] - 65, k1, k2, decoding) + 65);
else
output += ' ';
}
return output;
}
int mod(int a, int m) {
return (a % m + m) % m;
}
string CeasarDecoding_Encoding(string x, int k, bool type) {
string output = "";
for (int i = 0; i < x.size(); i++) {
if(type)
output += mod((int(x[i]) - 65 - k), 26) + 65;
else
output += mod((int(x[i]) - 65 + k), 26) + 65;
}
return output;
}
string SolveCeasar() {
int k = 0;
bool decoding = true;
string word = "";
cout << "Input word: ";
cin.ignore();
getline(cin, word);
cout << "\nType of Ceasar (0 for decoding, 1 for encoding) and k: ";
cin >> decoding >> k;
return CeasarDecoding_Encoding(word, k, decoding);
}
string VigenereDecoding_Encoding(string x, string k, bool type) {
string output = "";
int countCurrentK = 0;
for (int i = 0; i < x.size(); i++) {
if(type)
output += mod((int(x[i]) - int(k[countCurrentK++]) - 65 * 2), 26) + 65;
else
output += mod((int(x[i]) + int(k[countCurrentK++]) - 65 * 2), 26) + 65;
if (countCurrentK >= k.size())
countCurrentK = 0;
}
return output;
}
string SolveVigenere() {
string k = "", word = "";
bool decoding = true;
cout << "Input word: ";
cin.ignore();
getline(cin, word);
cout << "\nType of Vigenere (0 for decoding, 1 for encoding) and k: ";
cin >> decoding;
cin.ignore();
getline(cin, k);
return VigenereDecoding_Encoding(word, k, decoding);
}
int DetK(int arr[2][2]) {
return arr[0][0] * arr[1][1] - arr[0][1] * arr[1][0];
}
int DetKDelta(int arr[2][2]) {
return SolveEuclide(26, DetK(arr));
}
void updateK(int (&arr)[2][2], int delta) {
int temp = arr[0][0];
arr[0][0] = arr[1][1];
arr[1][1] = temp;
arr[0][1] *= -1;
arr[1][0] *= -1;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++) {
arr[i][j] = mod(arr[i][j] * delta, 26);
}
}
string HillDecoding_Encoding(string x, int(&arr)[2][2], bool type) {
string output = "";
if(!type)
updateK(arr, DetKDelta(arr));
for (int i = 0; i < x.size(); i += 2) {
output += mod(((int(x[i]) - 65) * arr[0][0] + (int(x[i + 1]) - 65) * arr[1][0]), 26) + 65;
output += mod(((int(x[i]) - 65) * arr[0][1] + (int(x[i + 1]) - 65) * arr[1][1]), 26) + 65;
}
return output;
}
string SolveHill() {
int k[2][2];
string word = "";
bool decoding = true;
cout << "Input word: ";
cin.ignore();
getline(cin, word);
cout << "\nType of Hill (0 for decoding, 1 for encoding) and k1, k2, k3, k4: ";
cin >> decoding;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
cin >> k[i][j];
return HillDecoding_Encoding(word, k, decoding);
}
void Initialize_PlayFairTable(string k, int(&arr)[50][50]) {
string key = "";
vector<bool> seen(26, false);
for (char ch : k) {
if (!isalpha((unsigned char)ch)) continue;
ch = tolower((unsigned char)ch);
if (ch == 'j') ch = 'i';
int idx = ch - 'a';
if (!seen[idx]) {
if (ch == 'i') {
if (!seen['i' - 'a']) seen['i' - 'a'] = true;
}
else {
seen[idx] = true;
}
key.push_back(ch);
}
}
for (char c = 'a'; c <= 'z'; ++c) {
if (c == 'j') continue;
if (!seen[c - 'a']) {
key.push_back(c);
seen[c - 'a'] = true;
}
}
for (int i = 0; i < 50; ++i) for (int j = 0; j < 50; ++j) arr[i][j] = -1;
int r = 0, c = 0;
for (char ch : key) {
arr[r][c] = ch - 'a';
c++;
if (c == 5) { c = 0; r++; }
}
}
static void findPosition(int val, int(&arr)[50][50], int& r, int& c) {
for (int i = 0; i < 5; ++i) for (int j = 0; j < 5; ++j) {
if (arr[i][j] == val) { r = i; c = j; return; }
}
r = c = -1;
}
string preprocessPlainForEncoding(string s) {
string t;
for (char ch : s) {
if (!isalpha((unsigned char)ch)) continue;
ch = tolower((unsigned char)ch);
if (ch == 'j') ch = 'i';
t.push_back(ch);
}
string out;
for (size_t i = 0; i < t.size(); ) {
char a = t[i];
char b = (i + 1 < t.size()) ? t[i + 1] : '\0';
if (b == '\0') {
out.push_back(a);
out.push_back('x');
i += 1;
}
else if (a == b) {
out.push_back(a);
out.push_back('x');
i += 1;
}
else {
out.push_back(a);
out.push_back(b);
i += 2;
}
}
return out;
}
string PlayfairEncoding(string x, int(&arr)[50][50]) {
string cleaned = preprocessPlainForEncoding(x);
string output;
for (size_t i = 0; i < cleaned.size(); i += 2) {
char a = cleaned[i], b = cleaned[i + 1];
int va = a - 'a', vb = b - 'a';
int ra, ca, rb, cb;
findPosition(va, arr, ra, ca);
findPosition(vb, arr, rb, cb);
if (ra == rb) {
int na = arr[ra][(ca + 1) % 5];
int nb = arr[rb][(cb + 1) % 5];
output.push_back(char(na + 'a'));
output.push_back(char(nb + 'a'));
}
else if (ca == cb) {
int na = arr[(ra + 1) % 5][ca];
int nb = arr[(rb + 1) % 5][cb];
output.push_back(char(na + 'a'));
output.push_back(char(nb + 'a'));
}
else {
int na = arr[ra][cb];
int nb = arr[rb][ca];
output.push_back(char(na + 'a'));
output.push_back(char(nb + 'a'));
}
}
return output;
}
string PlayfairDecoding(string x, int(&arr)[50][50]) {
string t;
for (char ch : x) {
if (!isalpha((unsigned char)ch)) continue;
ch = tolower((unsigned char)ch);
if (ch == 'j') ch = 'i';
t.push_back(ch);
}
if (t.size() % 2 != 0) t.push_back('x');
string output;
for (size_t i = 0; i < t.size(); i += 2) {
char a = t[i], b = t[i + 1];
int va = a - 'a', vb = b - 'a';
int ra, ca, rb, cb;
findPosition(va, arr, ra, ca);
findPosition(vb, arr, rb, cb);
if (ra == rb) {
int na = arr[ra][(ca + 5 - 1) % 5];
int nb = arr[rb][(cb + 5 - 1) % 5];
output.push_back(char(na + 'a'));
output.push_back(char(nb + 'a'));
}
else if (ca == cb) {
int na = arr[(ra + 5 - 1) % 5][ca];
int nb = arr[(rb + 5 - 1) % 5][cb];
output.push_back(char(na + 'a'));
output.push_back(char(nb + 'a'));
}
else {
int na = arr[ra][cb];
int nb = arr[rb][ca];
output.push_back(char(na + 'a'));
output.push_back(char(nb + 'a'));
}
}
string cleaned;
for (size_t i = 0; i < output.size(); ++i) {
if (i + 2 < output.size() && output[i] == output[i + 2] && output[i + 1] == 'x') {
cleaned.push_back(output[i]);
}
else {
cleaned.push_back(output[i]);
}
}
if (!cleaned.empty() && cleaned.back() == 'x') {
cleaned.pop_back();
}
return cleaned;
}
string SolvePlayFair() {
string k = "";
bool decoding = true;
string word = "";
cout << "Input word: ";
cin.ignore(numeric_limits<streamsize>::max(), '\n');
getline(cin, word);
cout << "\nType of Playfair (1 for decoding, 0 for encoding) and k: ";
int modeInt = 1;
cin >> modeInt;
decoding = (modeInt != 0);
cin.ignore(numeric_limits<streamsize>::max(), '\n');
getline(cin, k);
int arr[50][50];
Initialize_PlayFairTable(k, arr);
if (decoding) {
word = PlayfairDecoding(word, arr);
}
else {
word = PlayfairEncoding(word, arr);
}
return word;
}
const string listTemlate = "\
=========== Menu tool ==========\n\
1. Ceasar tool\n\
2. Vigenere tool\n\
3. Playfair tool\n\
4. Affair tool\n\
5. Ecluid tool\n\
6. Hill tool\n\
7. Diffie-Hellman\n\
0. Exit \n\
================================\n\
";
void ToolsMenu() {
char selection = -1;
while (selection != '0') {
cout << listTemlate << endl;
cout << "Input your choice [0-7]: "; cin >> selection;
switch (selection)
{
case '1':
{
cout << SolveCeasar() << endl;
break;
}
case '2':
{
cout << SolveVigenere() << endl;
break;
}
case '3':
{
cout << SolvePlayFair() << endl;
break;
}
case '4':
{
cout << SolveAffine() << endl;
break;
}
case '5':
{
int x = 0, a = 0;
cout << "Input x, a: "; cin >> x >> a;
cout << SolveEuclide(x,a)<< endl;
break;
}
case '6':
{
cout << SolveHill() << endl;
break;
}
case '7':
{
cout << "Not available" << endl;
break;
}
default:
if(selection != '0')
cout << "Input is invalid!" << endl;
}
}
}
int main() {
ToolsMenu();
system("pause");
return 1;
}