-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
732 lines (564 loc) · 18.8 KB
/
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
/**
* https://whimsical.com/cryptography-67Gfj8QufPBmgJDHY6fzoC ( LINK FOR FLOW CHART )
* git hub repo :: https://github.com/vipul-2003/ACM-PROJECT
* @file main.cpp
* @author VIPUL KUAMR SINGH ([email protected])
* @brief
* The ascii table is used https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html
* THIS IS THE IMPLEMENTATION OF THE DIFFERENT TEXT CRYPTOGRAPHY (in c++ language )
*
* 1.CEASER CIPHER
* 2.KEYWORD CIPHER
* 3.VERNAM CIPHER
* 4.VIGENERE CIPHER
*
* @version 1.0
* @date 2021-09-05
*
* @copyright Copyright (c) 2021
*
*/
/*
All header file included in this required to run the programs
*/
#include <iostream>
#include <stdio.h>
#include <ctype.h>
#include <vector>
#include <string>
using namespace std;
class CRYPTO
{
private:
string PlainText; // use to encrypt the data
string key; // use to encrypt the data
vector<char> CipherText; // use to encrypt the data
string CipherText1; // use to decrypt the data
vector<char> PlainText1; // use to decrypt the data
public:
/* --------------------------------------------------------------------------------------*/
void GetDataToEncrypt(void) // this function is used to input from user to encrypt in it
{
cout << "ENTER THE MESSAGE TO ENCRYPT " << endl;
cin >> PlainText;
cout << "ENTER THE KEY " << endl;
cin >> key;
}
void EncryptedDataToDisplay() // this function is used to display the encrypted text
{
cout << "THE ENCRYPTED MESSAGE IS " << endl;
for (int i = 0; i < CipherText.size(); i++)
{
cout << CipherText[i];
}
cout << "\n \n"
<< endl;
}
/* --------------------------------------------------------------------------------------*/
void GetDataToDecrypt(void) // this function is used to get encrypted text from user to decrypt
{
cout << "ENTER THE MESSAGE TO DECRYPT " << endl;
cin >> CipherText1;
cout << "ENTER THE KEY " << endl;
cin >> key;
}
void DecryptedDataToDisplay() // this function is used to display the deciphered(decrypted) text
{
cout << "THE DECRYPTED MESSAGE IS " << endl;
for (int i = 0; i < PlainText1.size(); i++)
{
cout << PlainText1[i];
}
cout << "\n\n"
<< endl;
}
/* --------------------------------------------------------------------------------------*/
void EmptyString() // this simply helps to delete or empty the string and vector elements
{
CipherText.clear(); // these two are string
PlainText1.clear();
PlainText.erase(); // these two are vector
CipherText1.erase();
}
/* --------------------------------------------------------------------------------------*/
void CeaserEncrpyt(); // function prototypes in class which is defined after the main function
void CeaserDecrpyt();
/* --------------------------------------------------------------------------------------*/
void KeywordEncrypt();
void KeywordDecrypt();
string GenerateKey(string key);
/* used to create a new key with repeating elements upto plaintext length
this function works for the keyword encrypt function
*/
/* --------------------------------------------------------------------------------------*/
void VernamEncrypt(); // function prototyping
void VernamDecrypt();
/* --------------------------------------------------------------------------------------*/
void VigenereEncrypt();
void VigenereDecrypt();
string GenerateKey_Vigenere(string plaintext, string key);
// this generatekey function helps in key for Vigenere key which is used to cipher or decipher the text
/* --------------------------------------------------------------------------------------*/
void RailFenceEncrypt(); // function prototypes in class which is defined after the main function
void RailFenceDecrypt();
/* --------------------------------------------------------------------------------------*/
};
int main(void)
{
int ch;
CRYPTO encryption; // it helps to use the function of class CRYPTO
CRYPTO decryption;
cout << "-------WELCOME , THERE -------" << endl;
int choice;
while (true)
{
/*
In while loop , simply we have implemented functions choice within choice
*/
cout << " 1. CEASER CIPHER" << endl;
cout << " 2. CEASER DECIPHER" << endl;
cout << " 3. KEYWORD CIPHER" << endl;
cout << " 4. KEYWORD DECIPHER" << endl;
cout << " 5. VERNAM CIPHER" << endl;
cout << " 6. VERNAM DECIPHER" << endl;
cout << " 7. VIGENERE CIPHER" << endl;
cout << " 8. VIGENERE DECIPHER" << endl;
cout << " 9. RAIL FENCE CIPHER" << endl;
cout << " 10. RAIL FENCE DECIPHER" << endl;
cout << " 11. EXIT " << endl;
cout << "ENTER THE CHOICE TO GO WITH " << endl;
cin >> choice;
switch (choice)
{
case 1:
cout << "YOU HAVE SELECTED CEASER CIPHER " << endl;
encryption.CeaserEncrpyt(); // function call
break;
/* --------------------------------------------------------------------------------------*/
case 2:
cout << "YOU HAVE SELECTED CEASER DECIPHER" << endl;
decryption.CeaserDecrpyt();
break;
/* --------------------------------------------------------------------------------------*/
case 3:
cout << "YOU HAVE SELECTED KEYWORD CIPHER" << endl;
encryption.KeywordEncrypt();
break;
/* --------------------------------------------------------------------------------------*/
case 4:
cout << "YOU HAVE SELECTED KEYWORD DECIPHER" << endl;
decryption.KeywordDecrypt();
break;
/* --------------------------------------------------------------------------------------*/
case 5:
cout << "YOU HAVE SELECTED VERNAM CIPHER " << endl;
encryption.VernamEncrypt();
break;
case 6:
cout << "YOU HAVE SELECTED VERNAM DECIPHER " << endl;
decryption.VernamDecrypt();
break;
case 7:
cout << "YOU HAVE SELECTED VIGENERE CIPHER" << endl;
encryption.VigenereEncrypt();
break;
case 8:
cout << "YOU HAVE SELECTED VIGENERE CIPHERR" << endl;
decryption.VigenereDecrypt();
break;
case 9:
cout << "YOU HAVE SELECTED RAIL FENCE CIPHER" << endl;
encryption.RailFenceEncrypt();
break;
case 10:
cout << "YOU HAVE SELECTED RAIL FENCE DECIPHER" << endl;
decryption.RailFenceDecrypt();
break;
case 11:
cout << " ((: EXITED :)) " << endl;
exit(0);
default:
cout << " !!! INVALID CHOICE !!! " << endl;
}
}
}
/* ------------------------------------------------------------------------------------------------------------*/
void CRYPTO ::CeaserEncrpyt() // function declaration of member function of class CRYPT
{
GetDataToEncrypt();
int new_key = stoi(key);
int size = PlainText.length(); // it is used to get the length of text inputted
for (int i = 0; i < size; i++)
{
if (isupper(PlainText[i]))
{
CipherText.push_back(char(int(PlainText[i] + new_key - 65) % 26 + 65));
}
else if (islower(PlainText[i]))
{
CipherText.push_back(char(int(PlainText[i] + new_key - 97) % 26 + 97));
}
else
{
CipherText.push_back(char(PlainText[i]));
}
}
EncryptedDataToDisplay(); // to display the encrypted message
EmptyString();
}
/* ------------------------------------------------------------------------------------------------------------*/
void CRYPTO ::CeaserDecrpyt() //function declaration of member function of class CRYPT
{
GetDataToDecrypt();
int new_key = stoi(key);
/*
stoi is basically string to integer function and predefined , accept key(string types ) as parameter than convert into integer type
new_key simply used to store explicit type conversion data
(string) to (int)
this function is used to convert string into integer as all key are accepted by user in string datatypes
i.e, we have to convert in integer as integer is required as key from the user
*/
int size = CipherText1.length(); // it is used to get the length of text inputted
for (int i = 0; i < size; i++)
{
if (isupper(CipherText1[i]))
{
PlainText1.push_back(char(int(CipherText[i] - new_key - 65) % 26 + 65));
}
else if (islower(CipherText1[i]))
{
PlainText1.push_back(char(int(abs(CipherText1[i] - new_key - 97) % 26 + 97)));
}
else
{
PlainText1.push_back(char(CipherText1[i]));
}
}
DecryptedDataToDisplay();
EmptyString();
}
/* ------------------------------------------------------------------------------------------------------------*/
/*
this function is used by keyword encrypt and decrypt function
to create a key of same length as that of plaintext or cipher text
this function basically create a 26 character key from the user inputted key and use it cipher or decipher
but the key inputted must be unique and does not two same characters
*/
string CRYPTO ::GenerateKey(string key)
{
bool array[26] = {false};
int j; // IT IS USED FOR CHANGING THE VALUE IF MESSAGE IS IN UPPER CASE OR IN LOWER CASE
char encrypt[26];
int size = key.length(); // used to get the length of the string :: key
for (int i = 0; i < size; i++)
{
if (array[i] == false)
{
encrypt[i] = key[i];
array[i] = true;
}
}
isupper(encrypt[0]) ? j = 65 : j = 97;
for (int i = size; i < 26; i++)
{
if (array[i] == false)
{
encrypt[i] = char(j);
for (int k = 0; k < size; k++)
{
if (encrypt[i] == key[k])
{
++j;
encrypt[i] = char(j);
k = -1;
}
}
array[i] = true;
++j;
}
}
// for ( int i = 0 ; i <26 ; i++)
// {
// cout <<encrypt[i];
// }
return encrypt;
}
/* ------------------------------------------------------------------------------------------------------------*/
void CRYPTO ::KeywordEncrypt() //function declaration of member function of class CRYPT
{
GetDataToEncrypt();
string new_key = GenerateKey(key); // reference with the keyword generate key function to return new key
int size = PlainText.size();
for (int i = 0; i < size; i++)
{
if (isupper(PlainText[i]))
{
CipherText.push_back(new_key[PlainText[i] - 65]);
}
else if (islower(PlainText[i]))
{
CipherText.push_back(new_key[PlainText[i] - 97]);
}
else
{
CipherText.push_back(PlainText[i]);
}
}
EncryptedDataToDisplay();
EmptyString();
}
/* ------------------------------------------------------------------------------------------------------------*/
void CRYPTO ::KeywordDecrypt()
{
GetDataToDecrypt();
string new_key = GenerateKey(key);
int size = CipherText1.size();
for (int i = 0; i < size; i++)
{
if (isupper(CipherText1[i]))
{
for (int j = 0; j < 26; j++)
{
if (CipherText1[i] == new_key[j])
{
PlainText1.push_back(char(j + 65));
break;
}
}
}
else if (islower(CipherText1[i]))
{
for (int j = 0; j < 26; j++)
{
if (CipherText1[i] == new_key[j])
{
PlainText1.push_back(char(j + 97));
break;
}
}
}
else
{
PlainText1.push_back((CipherText1[i]));
}
}
DecryptedDataToDisplay();
EmptyString();
}
/* ------------------------------------------------------------------------------------------------------------*/
/*
to run this program input and key length is same and equal
such as input is "vipulkumar" and key is "abcdefghij"
then we get the desired encrypted text
*/
void CRYPTO ::VernamEncrypt()
{
GetDataToEncrypt();
int sum = 0;
int j = 0 ;
for (int i = 0; i < PlainText.size(); i++)
{
isupper(PlainText[i])? j = 65 : j = 97;
sum = ((PlainText[i] - j) + (key[i] - j));
if (sum > 26)
{
sum -= 26;
}
sum += j;
CipherText.push_back(char(sum));
}
EncryptedDataToDisplay();
EmptyString();
}
/* ------------------------------------------------------------------------------------------------------------*/
void CRYPTO ::VernamDecrypt() //function declaration of member function of class CRYPT
{
GetDataToDecrypt();
int sum = 0;
int j = 0 ;
for (int i = 0; i < CipherText1.size(); i++)
{
isupper(PlainText[i])? j = 65 : j = 97;
sum = ((CipherText1[i] - j) - (key[i] - j));
if (sum < 0)
{
sum += 26;
}
sum += j;
PlainText1.push_back(char(sum));
}
DecryptedDataToDisplay();
EmptyString();
}
/* ------------------------------------------------------------------------------------------------------------*/
string CRYPTO ::GenerateKey_Vigenere(string plaintext, string key)
{
int size_of_plaintext = plaintext.size() - key.size();
int j = 0;
for (int i = 0; i < size_of_plaintext; i++)
{
if (key.size() == plaintext.size())
{
break; // key length and plaintext(message) length is same
}
else
{
key.push_back(key[j]);
j++;
if (j == key.size())
{
j = 0;
}
}
}
return key;
}
/* ------------------------------------------------------------------------------------------------------------*/
void CRYPTO ::VigenereEncrypt() //function declaration of member function of class CRYPT
{
GetDataToEncrypt();
string new_key = GenerateKey_Vigenere(PlainText, key);
int j = 0;
for (int i = 0; i < PlainText.size(); i++)
{
isupper(PlainText[i])? j = 65 : j = 97;
CipherText.push_back(char(int((PlainText[i] - j) + (new_key[i] - j)) % 26) + j); // j is done to converrt into small letters
}
EncryptedDataToDisplay();
EmptyString();
}
/* ------------------------------------------------------------------------------------------------------------*/
void CRYPTO ::VigenereDecrypt() //function declaration of member function of class CRYPT
{
GetDataToDecrypt();
string new_key = GenerateKey_Vigenere(CipherText1, key);
int j = 0;
for (int i = 0; i < CipherText1.size(); i++)
{
isupper(CipherText1[i])? j = 65 : j = 97;
PlainText1.push_back(char(int((CipherText1[i] - j) - (new_key[i] - j)) % 26) + j); // j is done to converrt into small letters
}
DecryptedDataToDisplay();
EmptyString();
}
/* ------------------------------------------------------------------------------------------------------------*/
void CRYPTO ::RailFenceEncrypt()
{
GetDataToEncrypt();
int new_key = stoi(key); // to convert string to integer
int row = new_key; // to represent the no. of rows
int column = PlainText.length(); // to represent the no. of columns and columns equals to length of plaintext
char RailFence[row][column];
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
RailFence[i][j] = '%'; //filling blank space to the rail fence matrix
}
}
bool directionChange = false; // used to go downward in the RailFence matrix
int i = 0; //used for the row
int j = 0; //used for the column
for (int k = 0; k < column; k++)
{
if (i == 0 || i == new_key - 1)
{
directionChange = !directionChange;
}
RailFence[i][j] = PlainText[k];
if (directionChange == false)
{
--i;
}
else
{
++i;
}
j++; //always increase the column in the RailFence
}
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
if (RailFence[i][j] != '%')
{
CipherText.push_back(RailFence[i][j]);
}
}
}
EncryptedDataToDisplay();
EmptyString();
}
/* ------------------------------------------------------------------------------------------------------------*/
void CRYPTO ::RailFenceDecrypt()
{
GetDataToDecrypt();
int new_key = stoi(key);
int row = new_key;
int column = CipherText1.length();
char RailFence[row][column];
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
RailFence[i][j] = '_'; //filling blank space to the rail fence matrix
}
}
bool directionChange = false; // used to go downward in the RailFence matrix
int i = 0; //used for the row
int j = 0; //used for the column
for (int k = 0; k < column; k++)
{
if (i == 0 || i == new_key - 1)
{
directionChange = !directionChange;
}
RailFence[i][j] = '*';
if (directionChange == false)
{
--i;
}
else
{
++i;
}
j++; //always increase the column in the RailFence
}
int k = 0;
while (k < CipherText1.length())
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
if (RailFence[i][j] == '*')
{
RailFence[i][j] = CipherText1[k];
k++;
}
}
}
}
directionChange = false; // set direction to false as it changes at first and last row
i = 0, j = 0; // to move with in matrix
for (int k = 0; k < column; k++)
{
if (i == 0 || i == new_key - 1)
{
directionChange = !directionChange;
}
PlainText1.push_back(RailFence[i][j]);
if (directionChange == false)
{
--i;
}
else
{
++i;
}
j++; //always increase the column in the RailFence
}
DecryptedDataToDisplay();
EmptyString();
}
/* ------------------------------------------------------------------------------------------------------------*/