-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParallelCryptography.cpp
1383 lines (1035 loc) · 34.4 KB
/
ParallelCryptography.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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/********************************************************************
* Project Name: Parallel Cryptographic Program *
* Team Members: Muhammad Owais Mushtaq [18k-1177] *
* Faiq Nadeem [18k-1194] *
* Syed Haris [18k-1162] *
*********************************************************************/
#include<bits/stdc++.h>
#include<conio.h>
#include<ctype.h>
#include<windows.h>
#include <algorithm>
#include <string.h>
using namespace std;
COORD coord= {0,0};
int FileCount=0;
//Prototypes of below Define Functions
void MainMenu();
std :: string Input_through_File();
void Encryption_Menu();
void Encryption(int c);
void Decryption_Menu();
void Decryption(int c);
void About();
/*-----------------------------------------------Functions for Consule based GUI------------------------------------------------------------*/
void delay(){
int i;
for(i=0;i<4500000;i++);
}
void SecDelay(){
int i;
for(i=0;i<25;i++){
delay();
}
}
void BoldLine(){
cout<<"\n\t\t";
for(int i=0;i<129;i++){
cout<<"\xDB";
}
cout<<"\t\t";
}
void MainScreen(){
string note="\n\n\n\n\tCaution! \n\tBefore Starting Project, Please make sure that console is in Full Screen.\n\n\t";
string upperline="\t\t\xc9\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xbb";
string lowerline="\t\t\xc8\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xbc";
string author="\n\t\t\xba\t\t* Muhammad Owais Mushtaq [18k-1177]\t* Faiq Nadeem [18k-1194]\t* Syed Haris Ahmed[18k-1162]\t\t \xba\n";
for(int i=0; i<note.size();i++){
cout<<note[i];
delay();
}
system("pause");
char s=1;
system("cls");
cout<<endl<<endl;
cout<<"\n\n\n\n\n\n\n";
BoldLine();
cout<<"\n\t\t\xDB\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\xDB";
cout<<"\n\t\t\xDB\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\xDB";
cout<<"\n\t\t\xDB\t\t\t\t\t\tProgram for Parallel Cryptography\t\t\t\t\t\t\xDB";
cout<<"\n\t\t\xDB\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\xDB";
cout<<"\n\t\t\xDB\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\xDB";
BoldLine();
cout<<endl<<endl<<endl;
cout<<"\t\tTeam Members: \n";
for(int i=0; i<upperline.size();i++){
cout<<upperline[i];
delay();
}
for(int i=0; i<author.size();i++){
cout<<author[i];
delay();
}
for(int i=0; i<lowerline.size();i++){
cout<<lowerline[i];
delay();
}
cout<<"\n\t\t\t\t\t\t\t\t\tLoading ...";
int i=0;
while(i<1){
system("color 9");
SecDelay();
system("color 1");
SecDelay();
system("color 9");
SecDelay();
system("color A");
SecDelay();
system("color 2");
SecDelay();
system("color A");
SecDelay();
system("color B");
SecDelay();
system("color 3");
SecDelay();
system("color B");
SecDelay();
system("color C");
SecDelay();
system("color 4");
SecDelay();
system("color C");
SecDelay();
system("color D");
SecDelay();
system("color 5");
SecDelay();
system("color D");
SecDelay();
system("color E");
SecDelay();
system("color 6");
SecDelay();
system("color E");
SecDelay();
system("color F");
i++;
}
}
void gotoxy(int x,int y){
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
void Box(int a,int b,int c,int d){
int i;
system("cls");
gotoxy(20,10);
for (i=a; i<=b; i++)
{
gotoxy(i,3);
printf("\xcd");
gotoxy(i,5);
printf("\xcd");
gotoxy(i,c);
printf("\xcd");
gotoxy(i,d);
printf("\xcd");
}
gotoxy(a,3);
printf("\xc9");
gotoxy(a,4);
printf("\xba");
gotoxy(a,5);
printf("\xc8");
gotoxy(b,3);
printf("\xbb");
gotoxy(b,4);
printf("\xba");
gotoxy(b,5);
printf("\xbc");
for(i=c; i<=d; i++)
{
gotoxy(a,i);
printf("\xba");
gotoxy(b,i);
printf("\xba");
}
gotoxy(a,c);
printf("\xc9");
gotoxy(a,d);
printf("\xc8");
gotoxy(b,c);
printf("\xbb");
gotoxy(b,d);
printf("\xbc");
}
void HeadingBox(){
int a=40,b=115;
for (int i=a; i<=b; i++)
{
gotoxy(i,3);
printf("\xcd");
gotoxy(i,5);
printf("\xcd");
}
gotoxy(a,3);
printf("\xc9");
gotoxy(a,4);
printf("\xba");
gotoxy(a,5);
printf("\xc8");
gotoxy(b,3);
printf("\xbb");
gotoxy(b,4);
printf("\xba");
gotoxy(b,5);
printf("\xbc");
}
void Highlight(char c, int count){
if (c == 'a'){
gotoxy(52,15);
cout<<" * Encryption";
gotoxy(52,18);
cout<<" * Decryption";
gotoxy(52,21);
cout<<" * About Program";
gotoxy(52,24);
cout<<" * Quit";
switch (count){
case 1:{
gotoxy(52,15);
cout<<"-> * Encryption";
break;
}
case 2:{
gotoxy(52,18);
cout<<"-> * Decryption";
break;
}
case 3:{
gotoxy(52,21);
cout<<"-> * About Program";
break;
}
case 4:{
gotoxy(52,24);
cout<<"-> * Quit";
break;
}
}
}else if (c == 'b' || c == 'c'){
gotoxy(52,15);
cout<<" * Input through text file";
gotoxy(52,18);
cout<<" * Input through typing";
gotoxy(52,21);
cout<<" * Main Menu";
gotoxy(52,24);
cout<<" * Quit";
switch (count){
case 1:{
gotoxy(52,15);
cout<<"-> * Input through text file";
break;
}
case 2:{
gotoxy(52,18);
cout<<"-> * Input through typing";
break;
}
case 3:{
gotoxy(52,21);
cout<<"-> * Main Menu";
break;
}
case 4:{
gotoxy(52,24);
cout<<"-> * Quit";
break;
}
}
}else if(c == 'd' || c == 'e'){
gotoxy(120,33);
cout<<" * Back";
gotoxy(120,36);
cout<<" * Quit";
switch (count){
case 1:{
gotoxy(120,33);
cout<<"-> * Back";
break;
}
case 2:{
gotoxy(120,36);
cout<<"-> * Quit";
break;
}
}
}else if(c == 'f'){
gotoxy(62,30);
cout<<" * Main Menu";
gotoxy(62,33);
cout<<" * Quit";
switch (count){
case 1:{
gotoxy(62,30);
cout<<"-> * Main Menu";
break;
}
case 2:{
gotoxy(62,33);
cout<<"-> * Quit";
break;
}
}
}
}
void curser(char c ,int no){
int count=1;
char ch='0';
gotoxy(85,15);
while(1){
switch(ch){
case 80:
count++;
if (count==no+1) count=1;
break;
case 72:
count--;
if(count==0) count=no;
break;
}
Highlight(c,count);
ch=getch();
if(ch=='\r'){
if(c == 'a' && no == 4){
switch(count){
case 1: Encryption_Menu();
break;
case 2: Decryption_Menu();
break;
case 3: About();
break;
default:
for(int i=0;i<16; i++){
cout<<endl;
}
exit(0);
break;
}
}else if(c == 'b' && no == 4){
switch(count){
case 1: Encryption(1);
break;
case 2: Encryption(2);
break;
case 3: MainMenu();
break;
default:
for(int i=0;i<16; i++){
cout<<endl;
}
exit(0);
break;
}
}else if(c == 'c' && no == 4){
switch(count){
case 1: Decryption(1);
break;
case 2: Decryption(2);
break;
case 3: MainMenu();
break;
default:
for(int i=0;i<16; i++){
cout<<endl;
}
exit(0);
break;
}
}else if(c == 'd' && no==2){
if(count==1)Encryption_Menu();
else{
for(int i=0;i<10; i++){
cout<<endl;
}
exit(0);
}
}else if(c == 'e' && no==2){
if(count==1)Decryption_Menu();
else{
for(int i=0;i<10; i++){
cout<<endl;
}
exit(0);
}
}else if(c == 'f' && no==2){
if(count==1)MainMenu();
else{
for(int i=0;i<7; i++){
cout<<endl;
}
exit(0);
}
}
}
}
}
void MainMenu(){
system("cls");
Box(40,115,8,40);
gotoxy(66,4);
cout<<"Program for Parallel Cryptography";
gotoxy(52,15);
cout<<" * Encryption";
gotoxy(52,18);
cout<<" * Decryption";
gotoxy(52,21);
cout<<" * About Program";
gotoxy(52,24);
cout<<" * Quit";
curser('a',4);
}
void Encryption_Menu(){
system("cls");
Box(40,115,8,40);
gotoxy(66,4);
cout<<"Encryption Input Methods";
gotoxy(52,15);
cout<<" * Input through text file";
gotoxy(52,18);
cout<<" * Input through typing";
gotoxy(52,21);
cout<<" * Main Menu";
gotoxy(52,24);
cout<<" * Quit";
curser('b',4);
}
void Decryption_Menu(){
system("cls");
Box(40,115,8,40);
gotoxy(66,4);
cout<<"Decryption Input Methods";
gotoxy(52,15);
cout<<" * Input through text file";
gotoxy(52,18);
cout<<" * Input through typing";
gotoxy(52,21);
cout<<" * Main Menu";
gotoxy(52,24);
cout<<" * Quit";
curser('c',4);
}
/*-----------------------------------------------Code of Consule based GUI Ends here------------------------------------------------------------*/
// ------------------------------------------ METHOD 1: Caeser Cypher BEGINS ------------------------------------------
/*The following functions applies the Caesar Cypher Method to encrypt the given input*/
/* It works by repacing each letter in the text with the letter that appears 6 positions ahead of it. */
string Encryption_Method_1( string input ){
string output = "";
output.resize(input.length()+1);
// output += 146; //Here, the first character is embedded in the program so that the decryption program can
//determine which key or cypher to use.
int shift = 6; //We shift each letter forward by 6 chracters
int l;
// Here, we go through the text and work with each individual character
#pragma omp parallel shared(input,output) private(l)
{
#pragma omp for schedule(dynamic) ordered
for (l = 0 ; l < input.length() ; l++ ){
/* We start off by checking if the current letter in the given input is in uppercase,
in which case we increase the letter's ASCII character value by 6 characters, and
in order to store it as as string, we subtract 65 from it (The ASCII characters for uppercase letters
start from 65) and proceed to MOD it by 26 (Since there are 26 characters in the alphabet)
anf finally cionvert it into char and store it in the output
*/
if ( isupper(input[l]) ){
output[l] = char ( int ( input[l] + shift - 65 ) % 26 + 65);
}
/* In order to ensure greater security,
we shift all non-alphabetical characters by 3 places instead of 6*/
else if( ( int(input[l]) < char(65) ) || ( int(input[l]) >= char(91) && int(input[l]) <= char(96) ) || int(input[l]) >= char(123) )
{
if( int(input[l]) == 32 ){ //space encountered
output[l] = '-';
}
else{
output[l] = input[l] + 3;
}
}
// Here, we do the same for lowercase letters
else{
output[l] = char ( int ( input[ l ] + shift - 97 ) % 26 + 97);
}
}
}
output[input.length()] = 'Ö';
// Here, we print the output
return output;
}
string Decryption_Method_1(string input){
string output = ""; //Here, we have ommitted the @ symbol since it was only used to identify the required method
int shift = 20; //We shift each letter backward by 20 chracters since the input used 6 for forward shift, so, 26-6=20
output.resize(input.length());
int l;
#pragma omp parallel shared(input,output) private(l)
{
// Here, we go through the text and work with each individual character
#pragma omp for schedule(dynamic) ordered
for (l = 0 ; l < input.length() ; l++ ){
/* We start off by checking if the current letter in the given input is in uppercase,
in which case we increase the letter's ASCII character value by 6 characters, and
in order to store it as as string, we subtract 65 from it (The ASCII characters for uppercase letters
start from 65) and proceed to mod it by 26 (Since there are 26 characters in the alphabet)
anf finally cionvert it into char and store it in the output
*/
if (isupper(input[l])){
output[l] = char(int(input[l] + shift - 65 ) % 26 + 65);
}
else if((int(input[l]) < char(65)) || (int(input[l]) >= char(91) && int(input[l]) <= char(96) ) || int(input[l]) >= char(123)){
if( input[l] == '-' ){ //space encountered
output[l] = 32;
}
else{
output[l] = input[l] - 3;
}
}
// Here, we do the same for lowercase letters
else{
output[l] = char(int(input[ l ] + shift - 97 ) % 26 + 97);
}
}
}
// Return the resulting string
return output;
}
// ------------------------------------------ METHOD 1 ENDS ------------------------------------------
// ------------------------------------------ METHOD 2: Keyword Cypher BEGINS ------------------------------------------
/* The following method takes in a particuar keyword (e.g. "ParallelProj18k" )
and inserts all of the non-numeric and non-repeating characters at the beginning of a 'Reference Key'
which is then used to encrypt the data
eg:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \
P A R L E O J K B C D F G H I M N Q S T U V W X Y Z
*/
// This is the set of etters in the english alphabet
string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string alphabet_LC = "abcdefghijklmnopqrstuvwxyz";
// The following function will be used to compute the refrence key which will be used to convert the inpout into cyphertext
string Make_Ref_Key(string keyword){
string Ref_key = ""; //This stores the key which will be used to convert the inpout into cyphertext
bool alpha[26] = {0}; /* Here, we have created an array which will be used to count whether a particular letter
in the key has already appeared.
Each index position in the array is used to represent the ASCII character of each letter
after subtracting 65 from it.
for example, the ASCII character for A is 65, Therefore, alpha[i-65] or alpha[65-65] is used
to store the occurance of A.
*/
// This loop inserts the keyword
// at the start of the encoded string
/* Here, we insert the given keyword at the beginning of the Reference key and
simply insert letters alphabetically after that */
for ( int i = 0 ; i < keyword.size() ; i++ )
{
//For uppercase letters
if ( keyword[i] >= 'A' && keyword[i] <= 'Z' ) //for uppercase characters
{
// Here, we check if the current character has alreaday been inserted,
// if it has been inserted, we do nothing
// otherwise we insert the character into the reference key and
if ( alpha[ keyword[i] - 65 ] == 0 ){
Ref_key += keyword[i];
alpha[ keyword[ i ] - 65 ] = 1;
}
}
else if( keyword[ i ] >= 'a' && keyword[ i ] <= 'z') //for lowercase letters
{
if ( alpha[ keyword[ i ] - 97 ] == 0){
Ref_key += keyword[ i ] - 32;
alpha[ keyword[i] - 97 ] = 1;
}
}
}
// This loop inserts the remaining
// characters in the encoded string.
for (int i = 0; i < 26; i++){
if(alpha[i] == 0){
alpha[ i ] = 1;
Ref_key += char(i + 65);
}
}
return Ref_key;
}
// Function that will encrypt the message
string Encryption_Method_2(string input){
string Ref_Key = Make_Ref_Key("ParallelProj18k");
int index; // stores index position from which element will be referenced
string result = ""; // stores result
result.resize(input.length()+1);
int i;
#pragma omp parallel shared(input,result,index,Ref_Key) private(i)
{
#pragma omp for schedule(dynamic) ordered
for (int i = 0 ; i < input.size() ; i++){
//Here, we encrypt lowercase letters
if ( input[i] >='a' && input[i] <= 'z'){
index = input[i] - 97;
result[i] = tolower( Ref_Key[index]);
}
//Here, we encrypt Uppercase letters
else if (input[i] >='A' && input[i] <='Z'){
index = input[i] - 65;
result[i] = Ref_Key[index];
}
else if( int( input[i] ) >= 48 && int( input[i] ) <= 57 ){
result[i] = input[i] + 10;
}
else{
result[i] = input[i];
}
}
}
result[input.length()] = 'û';
// here, we return the result
return result;
}
// Decryption Functions begin
string Decryption_Method_2(string input){
string Ref_Key = Make_Ref_Key("ParallelProj18k");
/* Now, we will use the reference key and the alphabet set to decrypot the input */
//Here, we store the positions at which each letter appears in the reference key
map <char,int> posts;
int index; // stores index position from which element will be referenced
string result = ""; // stores result
result.resize(input.length());
for(int i = 0 ; i < Ref_Key.size() ; i++){
posts [ Ref_Key [i] ] = i;
}
int i;
#pragma omp parallel shared(input,result,posts,alphabet,index) private(i)
{
#pragma omp for schedule(dynamic) ordered
for (i = 0; i < input.size() ; i++){
//Here, we encrypt lowercase letters
if ( input[i] >='a' && input[i] <='z'){
index = posts[ toupper(input[i])];
result[i] = tolower(alphabet[ index ]);
}
//Here, we encrypt uppercase letters
else if( input[i] >='A' && input[i] <='Z'){
index = posts[ input[i] ];
result[i] = alphabet[ index ];
}
else if( int( input[i] ) >= 58 && int( input[i] ) <= 67 ){
result[i] = input[i] - 10;
}
else // al other characs get stored as is
{
result[i] = input[i];
}
}
}
// returning result
return result;
}
//Decyption functions end
// ------------------------- METHOD 2 ENDS ---------------------------------
// ------------------------- METHOD 3: Columnar Transposition Cypher BEGINS --------------------------------
/* In the following function:
We take a particular keyword and use it's individual letters as columns for a table
then each cell below the letter is assigned a number acc. to it's position in the alphabet(e.g. A = 1)
then we start entering each character from the input text one-by-one and fill the table
then we read each column from top to bottom acc. to the numbers we previously assigned,
*/
// Key for Columnar Transposition
/*
The keyword is used to fill up the column headings in the table.
Each keyword character's index position is used to set the priority for the column-wise printing of the cypher later
REQUREMENTS:
1) Each charac shld be unique
2) No lowercase characs
*/
map < int , int > Ref_Table;
void set_ref_table_order(){
string keyword = "PDC_18K";
//Here, we create the table's laypout
for(int i = 0; i < keyword.length() ; i++){
Ref_Table[ keyword[ i ] ] = i;
}
}
// Function that will encrypt the message
string Encryption_Method_3(string input){
int rows;
int column;
int j = 0;
string keyword = "PDC_18K";
string result = "£"; // this will store the result
/* this gives us the number of columns in the matrix*/
column = keyword.length();
/* this gives us the number of rows in the matrix*/
rows = input.length() / column;
if ( input.length() % column ) {
rows = rows + 1;
}
char matrix[ rows ][ column ]; //Here, we create the matrix in which our input text will be stored in a row-by-row manner (i.e row stays the same until al cloumn values ahve been filed)
/*Here, we run 2 loops which alow us to traverse through the table we previousluy created and insert the input values one by one*/
/* We begin filling up the table/matrix with the values in the input text (in the orcer in which they appear) */
for (int i = 0 , k = 0 ; i < rows; i++) //entering ith row
{
for (int j = 0 ; j < column ; ) //entering ith column
{
if( input[ k ] == '\0') // if we reach end of line
{
/* Adding the padding character '_' */
matrix[ i ][ j ] = '_';
j++;
}
else{
/* Adding only space and alphabet into matrix*/
matrix[i][j] = input[k];
j++;
}
k++;
}
}
/* We stop filling up the table/matrix with the values in the input text (in the orcer in which they appear) */
for ( map <int,int> :: iterator z = Ref_Table.begin(); z != Ref_Table.end(); z++){
j = z->second;
// getting result text from matrix column wise using permuted key
for (int i=0; i<=rows; i++){
result += matrix[i][j];
}
}
result += "£";
return result;
}
// Decryption
string Decryption_Method_3 (string result){
string keyword = "PDC_18K";
set_ref_table_order();
/* this gives us the number of columns in the matrix*/
int column = keyword.length();
/* this gives us the number of rows in the matrix*/
int rows = result.length()/column;
// this will store the result of our encrypted text
char Resultant_Matrix[ rows ][ column ];
/* here, we insert each character into matrix column by column */
for (int j = 0, k = 0; j < column; j++){
for (int i = 0 ; i < rows ; i++) {
Resultant_Matrix[i][j] = result[k++];
}
}
/* here, we update the key order, so that we can decrypt the input file */
int index = 0;
for( map<int,int> :: iterator z = Ref_Table.begin(); z != Ref_Table.end(); z++){
z->second = index++;
}
/* Here we rearrange the matrix column by column according
to the order which we had previously set, we do this by inserting into new matrix */
char Final_res[rows][column];
map< int , int > :: iterator z = Ref_Table.begin();
int k = 0;
for ( int l = 0, j ; keyword[ l ] != '\0' ; k++ ){
j = Ref_Table[ keyword[ l++ ] ];
for (int i=0; i<rows; i++){
Final_res[i][k] = Resultant_Matrix[i][j];
}
}
/* getting Message using matrix */
string input = "";
for (int i = 1; i < rows; i++){
for(int j = 0; j < column; j++){
if( Final_res[i][j] != '_'){
input += Final_res[i][j];
}
}
}
return input;
}
// ------------------------------------------ METHOD 3 ENDS ------------------------------------------
// ------------------------------------------ METHOD 4: Affine Cypher BEGINS ----------------------------------------
/*
The Affine cipher is a type of substitution cipher, in which each letter in an alphabet is mapped to its numeric equivalent, (e.g A -> 1) and is
encrypted using a simple mathematical function, and converted back to a letter. The formula used means that each letter encrypts to one other letter,
and back again, meaning the cipher is essentially a substitution cipher with a rule governing which letter goes to which.
The whole process relies on working modulo m (the length of the alphabet used).
In the affine cipher, the letters of an alphabet of size m are first mapped to the integers in the range 0
m-1.
The key for the Affine cipher consists of 2 arbitrary numbers ( a and b)
*/