-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayfair_main.cpp
494 lines (449 loc) · 15.6 KB
/
playfair_main.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
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cctype>
#include <string>
#include <fstream>
#include <cstdlib>
#include <cstdio>
using namespace std;
const unsigned int ROWS = 5;
const unsigned int COLS = 5;
char tab[ROWS][COLS] = {{'\0'}};
//struktura przechowująca pozycje znaku
struct point
{
int x;
int y;
};
void WelcomeMenu (void);
void Decision (string&, string&);
void RemovingSpace (string&, string&);
void ToUppercase (string&, string&);
void Replacing (string&, string&);
void AddingZ (string&, string&);
void RemovingRepeatingSigns (string&);
void MakingKeyTab (string&);
void PrintTab(void);
point* FindCoordinates(char);
string Encoding(string);
void EncodeByPlayfair(string&, string&);
string Decoding(string);
void GetKeyWord (string&);
void GetPlaintext (string&);
void SaveInFile (string&, string&, string&, string&);
void EndMenu(string&, string&, string&, string&);
void WholeProgram(string&, string&);
void Loop(string&, string&);
int main()
{
string keyWord = {0}, plaintext = {0};
cout << endl << "WELCOME" << endl <<endl << "This a program encoding and decoding in the Playfair cipher" << endl << endl;
WholeProgram(keyWord, plaintext);
Loop(keyWord, plaintext);
return 0;
}
void WholeProgram(string& keyWord, string& plaintext)
{
WelcomeMenu();
Decision (keyWord, plaintext);
RemovingSpace (keyWord, plaintext);
ToUppercase (keyWord, plaintext);
Replacing (keyWord, plaintext);
AddingZ (keyWord, plaintext);
RemovingRepeatingSigns (keyWord);
MakingKeyTab (keyWord);
PrintTab();
string encodedText = Encoding (plaintext);
cout << "ENCODED TEXT: " << encodedText << endl;
string decodedText = Decoding (encodedText);
cout << "DECODED TEXT: " << decodedText << endl;
EndMenu(encodedText, decodedText, keyWord, plaintext);
}
void Loop(string& keyWord, string& plaintext)
{
cout << endl << "Choose what would you like to do next, type:" << endl;
cout << "C to code again" << endl;
cout << "Q to quit the program" << endl << endl << "-> ";
char choice = {0};
for (int i = {0}; i < 2; i++)
{
cin >> choice;
if (choice == 'C')
{
WholeProgram(keyWord, plaintext);
cout << endl << endl << "Choose what would you like to do next, type:" << endl;
cout << "C to code again" << endl;
cout << "Q to quit the program" << endl << endl << "-> ";
}
else if (choice == 'Q')
{
exit(0);
}
else
{
cout << "Wrong sign has been entered" << endl << endl << "You have one more try or else the program closes";
cout << endl << "->";
}
}
}
void GetKeyWord (string& keyWord)
{
cout << endl << "Input your key word (no special characters) : " << endl << "-> ";
getline (cin >> ws, keyWord);
}
void GetPlaintext (string& plaintext)
{
cout << "Input your message (no special characters) :" << endl << "-> ";
getline (cin >> ws, plaintext);
}
void WelcomeMenu (void)
{
cout << "Choose what would you like to do and type:" << endl << endl;
cout << "M to input the message manually" << endl;
cout << "F to input the message from the file" << endl;
cout << "Q to quit the program" << endl << endl << "-> ";
}
void ReadingFile (string& plaintext)
{
string fileName = {0};
for (int i = {4}; i > 0 ;i--)
{
if (i == 1)
{
cout << endl << endl << "QUITTING THE PROGRAM..." << endl;
exit(0);
}
cout << endl << "Input the name of your file" << endl << "-> ";
cin >> fileName;
ifstream ifhandle(fileName.c_str());
if (ifhandle)
{
while (getline (ifhandle, plaintext))
{
cout << endl << "Your message: " << plaintext;
}
ifhandle.close();
break;
}
else if (!ifhandle)
{
cout << endl << "CANNOT OPEN FILE " << fileName ;
cout << endl << "You have " << i - 2 << " more tries";
}
}
}
void Decision (string& keyWord, string& plaintext)
{
char decision = {0};
for (int i = {0}; i < 2; i++)
{
cin >> decision;
cout << endl;
if (decision == 'M' )
{
GetPlaintext (plaintext);
GetKeyWord (keyWord);
break;
}
else if (decision == 'F')
{
ReadingFile (plaintext);
GetKeyWord (keyWord);
break;
}
else if (decision == 'Q')
{
cout << endl << "QUITTING...";
exit(0);
}
else
{
if ( i == 1)
{
cout << "QUITTING..." << endl;
exit (0);
}
cout << "Wrong sign has been entered" << endl << endl << "You have one more try";
cout << endl << "->";
}
}
}
void EndMenu (string& encodedText, string& decodedText, string& keyWord, string& plaintext)
{
cout << endl << "To save your code type: " << endl << endl << "S to save" << endl << "Q to quit right now" << endl << endl << "->";
char decision = {0};
string fileName = {0};
for (int i = {0}; i < 2; i++)
{
cin >> decision;
if (decision == 'S' )
{
SaveInFile (encodedText, decodedText, keyWord, plaintext);
break;
}
else if (decision == 'Q')
{
cout << endl << "QUITTING...";
exit(0);
}
else
{
if ( i == 1)
{
cout << "QUITTING..." << endl;
exit (0);
}
cout << endl << "Wrong sign has been entered" << endl << endl << "You have one more try";
cout << endl << "->";
}
}
}
void SaveInFile (string& encodedText, string& decodedText, string& keyWord, string& plaintext)
{
ofstream ofhandle("output.txt");
ofhandle << "KEYWORD: " << keyWord << endl << "PLAINTEXT: " << plaintext << endl;
ofhandle << "ENCODED TEXT: " << encodedText << endl << "DECODED TEXT: " << decodedText;
cout << "Your code has been saved in a file named output.txt" << endl;
}
void EncodeByPlayfair(string& keyWord, string& plaintext)
{
void ToUppercase (string& keyWord, string& plaintext);
void RemovingSpace (string& keyWord, string& plaintext);
void Replacing (string& keyWord, string& plaintext);
void AddingZ (string& keyWord, string& plaintext);
void RemovingRepeatingSigns (string& keyWord);
void MakingKeyTab (string& keyWord);
void PrintTab(void);
string Encoding(string plaintext);
}
void ToUppercase (string& keyWord, string& plaintext)
{
for (unsigned int i = 0; i < keyWord.size(); i++)
{
if (keyWord.at(i) >= 'a' && keyWord.at(i) <= 'z')
{
keyWord.at(i) = keyWord.at(i) - 32;
}
}
for (unsigned int i = 0; i < plaintext.size(); i++)
{
if (plaintext.at(i) >= 'a' && plaintext.at(i) <= 'z')
{
plaintext.at(i) = plaintext.at(i) - 32;
}
}
}
void RemovingSpace (string& keyWord, string& plaintext)
{
keyWord.erase(remove(keyWord.begin(), keyWord.end(), ' '), keyWord.end());
plaintext.erase(remove(plaintext.begin(), plaintext.end(), ' '), plaintext.end());
}
//zamieniam j na i, bo nie starczy miejsca dla 26 liter w tablicy 5x5
void Replacing (string& keyWord, string& plaintext)
{
replace(keyWord.begin(), keyWord.end(), 'J', 'I');
replace(plaintext.begin(), plaintext.end(), 'J', 'I');
}
// dodaje z do tekstu jawnego o nieparzystej ilości znaków
void AddingZ (string& keyWord, string& plaintext)
{
if (plaintext.length() % 2 != 0)
{
plaintext += 'Z';
}
}
//w kluczu usuwam powtarzające się znaki
void RemovingRepeatingSigns (string& keyWord)
{
for( unsigned int i = 0; i <= keyWord.size() - 1; i++ )
{
for( unsigned int a = i + 1; a <= keyWord.size() - 1; a++ )
{
if( keyWord.at(i) == keyWord.at(a) )
{
keyWord.erase( a, 1 );
a = a - 1;
}
}
}
}
//wpisywanie klucza do tablicy
void MakingKeyTab (string& keyWord)
{
string control = "";
string alfabet = "ABCDEFGHIKLMNOPQRSTUVWXYZ";
int x = 0, y = 0;
size_t position;
for (char sign : keyWord)
{
position = control.find(sign);
if (position == string::npos)
{
if (y == 5)
{
cout << "ERROR! - INVALID SIGNS IN TAB" << endl;
return;
}
//dodajemy znak do kontrolnego stringa i tablicy
control += sign;
tab[x][y] = sign;
x++;
if (x == 5)
{
x = 0;
y++;
}
}
}
for (char sign : alfabet)
{
position = control.find(sign);
if (position == string::npos)
{
if (y == 5)
{
cout << endl << "ERROR! - INVALID SIGNS IN TAB" << endl;
exit(0);
}
control += sign;
tab[x][y] = sign;
x++;
if (x == 5)
{
x = 0;
y++;
}
}
}
}
//wyświetlanie wypelnionej tablicy w konsoli
void PrintTab(void)
{
cout << endl;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
cout << tab[j][i] << " | ";
}
cout << endl;
}
cout << endl;
}
//przypisywanie pozycji w tabeli do koordynatów
point* FindCoordinates(char sign)
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
if (tab[j][i] == sign)
{
point* coordinates = new point;
coordinates->x = j;
coordinates->y = i;
return coordinates;
}
}
}
return nullptr;
}
string Encoding(string plaintext)
{
point* coordinates_of_first;
point* coordinates_of_second;
string encrypted_text = "";
for (unsigned int x = 0; x < plaintext.length(); x += 2)
{
coordinates_of_first = FindCoordinates(plaintext.at(x));
coordinates_of_second = FindCoordinates(plaintext.at(x + 1));
//jesli nie ma wskaznika wychodzimy z programu
if (coordinates_of_first == nullptr || coordinates_of_second == nullptr)
{
cout << "ERROR - END OF CODING" << endl;
return "";
}
//jesli dwa znaki sa w tej samej kolumnie
if (coordinates_of_first->x == coordinates_of_second->x)
{
encrypted_text += tab[coordinates_of_first->x][(coordinates_of_first->y + 1) % 5];
encrypted_text += tab[coordinates_of_second->x][(coordinates_of_second->y + 1) % 5];
}
//jesli dwa znaki sa w tym samym wierszu
else if (coordinates_of_first->y == coordinates_of_second->y)
{
encrypted_text += tab[(coordinates_of_first->x + 1) % 5][coordinates_of_first->y];
encrypted_text += tab[(coordinates_of_second->x + 1) % 5][coordinates_of_second->y];
}
else
{
encrypted_text += tab[coordinates_of_second->x][coordinates_of_first->y];
encrypted_text += tab[coordinates_of_first->x][coordinates_of_second->y];
}
}
return encrypted_text;
}
string Decoding(string encoded_string)
{
point* coordinates_of_first;
point* coordinates_of_second;
string plaintext = "";
for (unsigned int x = 0; x < encoded_string.length(); x += 2)
{
coordinates_of_first = FindCoordinates(encoded_string.at(x));
coordinates_of_second = FindCoordinates(encoded_string.at(x + 1));
//jesli nie ma wskaznika wychodzimy z programu
if (coordinates_of_first == nullptr || coordinates_of_second == nullptr)
{
cout << "ERROR - END OF CODING" << endl;
return "";
}
//jesli dwa znaki sa w tej samej kolumnie
if (coordinates_of_first->x == coordinates_of_second->x)
{
if (coordinates_of_first->y - 1 < 0)
{
plaintext += tab[coordinates_of_first->x][4];
}
else
{
plaintext += tab[coordinates_of_first->x][coordinates_of_first->y - 1];
}
if (coordinates_of_second->y - 1 < 0)
{
plaintext += tab[coordinates_of_second->x][4];
}
else
{
plaintext += tab[coordinates_of_second->x][coordinates_of_second->y - 1];
}
}
//jesli dwa znaki sa w tym samym wierszu
else if (coordinates_of_first->y == coordinates_of_second->y)
{
if (coordinates_of_first->x - 1 < 0)
{
plaintext += tab[4][coordinates_of_first->y];
}
else
{
plaintext += tab[coordinates_of_first->x - 1][coordinates_of_first->y];
}
if (coordinates_of_second->x - 1 < 0)
{
plaintext += tab[4][coordinates_of_second->y];
}
else
{
plaintext += tab[coordinates_of_second->x - 1][coordinates_of_second->y];
}
}
else
{
plaintext += tab[coordinates_of_second->x][coordinates_of_first->y];
plaintext += tab[coordinates_of_first->x][coordinates_of_second->y];
}
}
return plaintext ;
}