-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHilly.c
3676 lines (3211 loc) · 131 KB
/
Hilly.c
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
/** Hilly Program
Author: Yahya Almardeny
2016
Last Update: 01/10/2016
*/
#include<stdio.h>
#include<stdbool.h>
#include<string.h>
#include<Shlobj.h>
#include<math.h>
#include "cMenu.h"
#include "matrix.h"
/*Global Vars*/
typedef double** M;
M key;
M extensionMatrix;
M keyInverse;
M extensionMatrixInverse;
double diagonalSum=0;
double secondaryDiagonalSum = 0;
double *determinant;
double xEncDecryption[1000000];
double cipherLettersDecryption[1000000];
char cipher_Text[10000000]; // for Decryption
char plainText[1000000]; // for Decryption
char plain_Text[1000000]; // for Encryption
char cipherText[10000000]; // for Encryption
double substition_Table[74];
char symbolsSubstitutionTable[]={'*','#','&','%','$','+','?','!','@','^' ,'-'}; // default without shift yet
char padding[2];
/*Functions*/
/*INTRO*/
int intro();
int mainMenu();
int findConsoleScreenWidth();
void centerText(char *text, size_t color, size_t textWidth);
void printCharByChar(char *text, size_t delay, size_t color, char *newline);
/*MIS. SHARED FUNCTIONS */
M create_key();
void create_extensionMatrix();
void delete_matrix(M matrix);
void deleteColumnMatrix(M columnMatrix);
void emptyArray(char *theArray);
void resetSymbolsSubstitutionTable();
void resetSubstitutionTable();
int gcd(int a);
/*ENCRYPTION*/
int keyMatrixEnc();
int generateKey();
int insertKey(M key);
int loadKey();
int saveKey(M key);
int saveInsertedKey(M key);
int validateKey(M key);
int drawKey(int vpos, M key);
int drawKeyFromFile(int indent, int vpos, M key);
double controlledRandom();
int symbols();
int shiftSymbols(char *arrayOfSymbols);
int mod(double x , int y);
double sum_t();
int loadSymbols();
int insertSymbols();
int saveInsertedSymbols();
int plainTextFun();
int typePlainText();
int loadPlainText();
int plainTextLength();
int encrypt();
int saveCipherText();
int encryption();
int addPadding();
double roundExtentionX(double extentionX);
/*DECRYPTION*/
int keyMatrixDec();
int cipherTextFun();
int typeCipherText();
int loadCipherText();
int savePlainText();
int cipherTextLength();
int find_NumberOfAddedChars();
int decrypt(char *cipher_Text);
double convertCharToDouble(char *arrayOfChars, double arrayOfDoubles[]);
void removePadding();
double roundP(double p);
void findExtensionMatrixInverse();
void findKeyInverse();
void emptyDoubleArray();
int decryption();
/*How To Use & about*/
void howToUse();
void about();
int main(){
intro();
int option =0;
while(option != 5){
system("cls");
option = mainMenu();
switch(option){
case 1:
if(encryption()){printf("\n\n Press Enter To Continue..."); while(getchar()!='\n'); break;}
else {break;}
case 2:
if(decryption()){printf("\n\n Press Enter To Continue..."); while(getchar()!='\n'); break;}
else {break;}
break;
case 3:
howToUse();
break;
case 4:
about();
break;
case 5:
exit(0);
}
}
return 0;}
/*HILLY INTRO & MAIN MENU*/
int mainMenu(){
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO info;
info.dwSize = 100;
info.bVisible = FALSE;
SetConsoleCursorInfo(hOut, &info);
system("cls");
printf("\n\n\n");
centerText("",0, 5);
printCharByChar("HILLY", 0, 5,"\n");
centerText("",0, 34);
printCharByChar("An Improved Version Of Hill Cipher", 0, 5,"\n");
centerText("",0, 34);
printCharByChar("----------------------------------", 0, 15,"\n\n");
int option = VMenu((findConsoleScreenWidth()/2)-11, -1,2,">",15,"",0,15,3,5, "Encryption", "Decryption", "How To Use This Program", "About", "Exit");
switch(option){
case 1:
return 1;
case 2:
return 2;
case 3:
return 3;
case 4:
return 4;
case 5:
exit(0);
}
}
int intro(){
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO info;
info.dwSize = 100;
info.bVisible = FALSE;
SetConsoleCursorInfo(hOut, &info);
printf("\n\n\n");
centerText("",0, 5);
printCharByChar("HILLY", 80, 15,"\n");
centerText("",0, 34);
printCharByChar("An Improved Version Of Hill Cipher", 60, 8,"\n\n\n");
CONSOLE_SCREEN_BUFFER_INFO SBInfo;
GetConsoleScreenBufferInfo(hOut, &SBInfo);
COORD c;
c.Y = SBInfo.dwCursorPosition.Y;
c.X = SBInfo.dwCursorPosition.X;
SetConsoleCursorPosition(hOut, c);
Sleep(500);
int x=0;
for(;x<2;x++){
SetConsoleCursorPosition(hOut, c);
centerText("Loading ",10, 7);
Sleep(500);
SetConsoleCursorPosition(hOut, c);
centerText("Loading. ",11, 7);
Sleep(500);
SetConsoleCursorPosition(hOut, c);
centerText("Loading.. ",13, 7);
Sleep(500);
SetConsoleCursorPosition(hOut, c);
centerText("Loading...",12, 7);
Sleep(500);
}
return 0;}
int findConsoleScreenWidth(){
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO SBInfo;
GetConsoleScreenBufferInfo(hOut, &SBInfo);
int pos = SBInfo.dwCursorPosition.Y;
if (pos==0){pos++;printf("\n");}
int theEnd =pos+1, width=0;
while (pos<theEnd){
printf(" ");
GetConsoleScreenBufferInfo(hOut, &SBInfo);
pos = SBInfo.dwCursorPosition.Y;
width++;
}
for (pos=0;pos<width;pos++){printf("\b");}
return width-10; // I liked this position more :D
}
void centerText(char *text, size_t color, size_t textWidth){
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO info;
info.dwSize = 100;
info.bVisible = FALSE;
SetConsoleCursorInfo(hOut, &info);
int width = findConsoleScreenWidth();
int indent;
if (textWidth){
double even;
if((even = ((double)textWidth-((double)textWidth/2)))>0){textWidth++;}
for (indent=0; indent<((width/2)-(textWidth/2)); indent++){printf(" ");}
}
else {
int len = strlen(text);
for (indent=0; indent<((width/2)-(len/2)); indent++){printf(" ");}
}
SetConsoleTextAttribute(hOut, color);
printf(text);
SetConsoleTextAttribute(hOut, 15);
}
void printCharByChar(char *text, size_t delay, size_t color, char *newline){
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hOut, color);
while(*text){
printf("%c", *text);
Sleep(delay);
text++;
}
SetConsoleTextAttribute(hOut, 15);
printf(newline);
}
/*MIS. FUNCTIONS SHARED BETWEEN ENCRYPTION & DECRYPTION*/
M create_key(){
int x;
M key = calloc(6,sizeof(double*));
for (x=0;x<6;x++){key[x] = calloc(6, sizeof(double));}
return key;
}
void create_extensionMatrix(){
int col,row;
extensionMatrix = calloc(6,sizeof(double*));
for (col=0;col<6;col++){extensionMatrix[col] = calloc(6, sizeof(double));}
if (diagonalSum!=0){
for (col=0;col<6;col++){
for (row=0;row<6;row++){
extensionMatrix[col][row] = key[col][row] + diagonalSum;
}
}
}
else{
for (col=0;col<6;col++){
for (row=0;row<6;row++){
extensionMatrix[col][row] = key[col][row] + secondaryDiagonalSum;
}
}
}
}
void delete_matrix(M matrix){
int x;
for (x=0;x<6;x++){free(matrix[x]);}
free(matrix);
}
void deleteColumnMatrix(M columnMatrix){
int i;
for(i=0;i<6;i++){free(columnMatrix[i]);}
free(columnMatrix);
}
void emptyArray(char *theArray){
int i=0;
while(theArray[i]){
theArray[i]=0;
i++;
}
}
void resetSymbolsSubstitutionTable(){
symbolsSubstitutionTable[0]= '*';
symbolsSubstitutionTable[1]= '#';
symbolsSubstitutionTable[2]= '&';
symbolsSubstitutionTable[3]= '%';
symbolsSubstitutionTable[4]= '$';
symbolsSubstitutionTable[5]= '+';
symbolsSubstitutionTable[6]= '?';
symbolsSubstitutionTable[7]= '!';
symbolsSubstitutionTable[8]= '@';
symbolsSubstitutionTable[9]= '^';
symbolsSubstitutionTable[10]= '-';
}
void resetSubstitutionTable(){
int i;
for (i=0;i<74;i++){
substition_Table[i]=0;
}
}
/*ENCRYPTION*/
/*THE KEY*/
int keyMatrixEnc(){ // The Key Matrix For Encryption
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
int doneKey =0;
while (!doneKey){
system("cls"); // when choose to Cancel
printf("\n");
SetConsoleTextAttribute(hOutput, 3);
printf("==================================================================\n");
printf("\t\t\t KEY");
printf("\n==================================================================\n\n");
int x=0,y=0,option=0;
key = create_key();
option= HMenu(1, -1, 5, ">", 15, "|", 9, 15, 128, 4, "Generate Key", "Type Key", "Load Key From File", "Main Menu");
switch (option){
case 1:
doneKey=generateKey();
if(doneKey==1){return 1;}
break;
case 2:
doneKey=insertKey(key);
if(doneKey==1){return 1;}
else{delete_matrix(key);}
break;
case 3:
doneKey=loadKey();
if(doneKey==1){return 1;}
else {delete_matrix(key);}
break;
case 4:
return -1;
}
}
return 0;
}
int generateKey(){ // Generate Key Randomly for the User
printf("\n");
determinant = calloc(1, sizeof(double));
int option = HMenu(1, -1, 4, ">", 15, "|", 9, 15, 128, 4, "Positive Integers", "Negative Integers", "Pos. & Neg.", "Cancel");
int x,y, option1 = 1, vpos = -1;
switch (option){
case 1:
while(option1 == 1){ // while Re-generate is chosen
diagonalSum=0; *determinant=0;
while(diagonalSum==0||*determinant==0||gcd((int)*determinant)==1){ // secondaryDiagonalSum is ignored here because we have plenty of possibilities before taking it into account
for(x=0;x<6;x++){
for(y=0;y<6;y++){
key[x][y] = controlledRandom();
if(x==y){diagonalSum += key[x][y];}
}
}
determinant = mDet(6,"lf", key);
}
vpos = drawKey(vpos, key);
printf("\n");
option1 = HMenu(1, -1, 5, ">", 15, "|", 9, 15, 128,4, "Re-generate", "Save to File", "Continue Without Save", "Cancel");
}
if(option1 == 2){if(saveKey(key)){return 1;} else {return 0;}}
else if(option1 == 3){return 1;}
else if (option1 == 4){ delete_matrix(key); free(determinant); diagonalSum=0; return 0;}
case 2:
while(option1 == 1){
diagonalSum=0; *determinant=0;
while(diagonalSum==0 || *determinant==0 || gcd((int)*determinant)==1){
for(x=0;x<6;x++){
for(y=0;y<6;y++){
key[x][y] = controlledRandom() * -1;
if(x==y){diagonalSum += key[x][y];}
}
}
determinant = mDet(6,"lf", key);
}
vpos = drawKey(vpos, key);
printf("\n");
option1 = HMenu(1, -1, 5, ">", 15, "|", 9, 15, 128,4, "Re-generate", "Save to File", "Continue Without Save", "Cancel");
}
if(option1 == 2){if(saveKey(key)){return 1;} else {return 0;}}
else if(option1 == 3){return 1;}
else if (option1 == 4){ delete_matrix(key); free(determinant); diagonalSum=0; return 0;}
case 3:
while(option1 == 1){
diagonalSum=0; *determinant=0;
while(diagonalSum==0 || *determinant==0|| gcd((int)*determinant)==1){
for(x=0;x<6;x++){
for(y=0;y<6;y++){
key[x][y] = controlledRandom() - controlledRandom();
if(x==y){diagonalSum += key[x][y];}
}
}
determinant = mDet(6,"lf", key);
}
vpos = drawKey(vpos, key);
printf("\n");
option1 = HMenu(1, -1, 5, ">", 15, "|", 9, 15, 128,4, "Re-generate", "Save to File", "Continue Without Save", "Cancel");
}
if(option1 == 2){if(saveKey(key)){return 1;} else {return 0;}}
else if(option1 == 3){return 1;}
else if (option1 == 4){ delete_matrix(key); free(determinant); diagonalSum=0; return 0;}
case 4:
return 0;
}
return 0;
}
int insertKey(M key){ // When The User Inserts The Key Typing on The Keyboard (Important: Shared between Encryption & Decryption!)
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
int attempts=0;
while(attempts<6){ // number of attempts
printf("\n Insert Each Integer Separated by a Comma or a Space then Press Enter:\n\n");
bool detect_non_digit = false;
char line[256];
char *line_pointer = line;
char *ptr = line;
long numbers[37]; // should hold the 36 integers for the key
int i=0; // index for number array
printf(" ");
SetConsoleTextAttribute(hOutput,8);
if (fgets(line, sizeof(line), stdin)){ // reads the entire line and check for invalid input (non-digit except comma, space and minus sign)
SetConsoleTextAttribute(hOutput,15);
while (*ptr != '\n'){
if (!(isdigit(*ptr))){
if (*ptr != ' ' && *ptr != ',' && *ptr != '-'){ // only allowed from "Non-digits"
detect_non_digit = true;
break;
}
}
ptr++;
}
if (detect_non_digit == false){
*ptr = ',';
ptr++;
*ptr= '\n';
while (*line_pointer!='\n'){
int x =0;
while(*line_pointer!=',' && *line_pointer!=' '){
char eachInput[25];
eachInput[x] = *line_pointer;
x++;
line_pointer++;
if (*line_pointer==',' || *line_pointer==' ' || *line_pointer =='-'){ // that means it's the end of all digits of each number
eachInput[x] = '\0';
numbers[i]= strtol(eachInput , NULL, 10);
i++;
}
}
line_pointer++;
}
if (i==0) {return 0;} // that means user did not enter any input
else if (i==1) {SetConsoleTextAttribute(hOutput,1);
printf("\n /You Have Inserted Only 1 Integer, Please Insert 36 Integers!/\n");
SetConsoleTextAttribute(hOutput, 15);}
else if (i<36){ SetConsoleTextAttribute(hOutput, 1);
printf("\n /You Have Inserted Only %d Integers, Please Insert 36 Integers!/\n", i);
SetConsoleTextAttribute(hOutput, 15);}
else if (i>36) {SetConsoleTextAttribute(hOutput, 1);
printf("\n /You Have Inserted %d Integers, Please Insert Only 36 Integers!/\n", i);
SetConsoleTextAttribute(hOutput, 15);}
else if (i==36){
int col,row, y;
for (col=0,y=0;col<6;col++){
for(row=0;row<6;row++,y++){
key[col][row] = (double)numbers[y]; // fill the Key
}
}
if (validateKey(key)){SetConsoleTextAttribute(hOutput,3); // then validate for determinant, DiagonalSum, and secondaryDiagonalSum
printf("\n");
for (col=0;col<6;col++,printf("\n")){
for(row=0;row<6;row++){
printf(" %.0lf", key[col][row]);
}
}
if(saveInsertedKey(key)){return 1;}
else{return 0;}
}
else {SetConsoleTextAttribute(hOutput,12);
printf("\n\tInvalid Key!\n");
SetConsoleTextAttribute(hOutput, 15);}
}
}
else if (detect_non_digit == true) {SetConsoleTextAttribute(hOutput,12); printf("\n\tInvalid Input!\n"); SetConsoleTextAttribute(hOutput, 15);}
}
attempts++;
if(attempts==6){SetConsoleTextAttribute(hOutput, 12); printf("\n\t***Failure to Receive Valid Input, No More Retries***\n"); SetConsoleTextAttribute(hOutput, 15); Sleep(1600); return 0;}
}
}
int loadKey(){ // When The User Choose To Load The Key from a File (Important: Shared between Encryption & Decryption!)
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
int attempts=0;
while(attempts<6){ // number of attempts allowed
printf("\n");
bool enter = true; // reference if the "Enter" Key has been pressed without any input
bool not36 = false; // reference for the number of the Key elements
int option = HMenu(7, -1, 4, ">", 15, "|", 9, 15, 128, 3, "Load from Desktop", "Load from Directory", "Cancel");
char line[256];
char *line_ptr = line;
switch(option){
case 1:
printf("\n\tInsert The File Name: ");
if(scanf("%256[0-9a-zA-Z ,.'!@#$%^&()-_=+~{[]}; ]", line)){
int x,y;
FILE *fp;
/*Works on Windows Only!*/
TCHAR szPath[MAX_PATH];
SHGetFolderPath(NULL,CSIDL_DESKTOPDIRECTORY,NULL,0,szPath);
strcat(szPath,"\\");strcat(szPath, line);strcat(szPath,".txt"); // concatenate to form a proper directory
fp = fopen(szPath, "r");
if (fp == NULL) {SetConsoleTextAttribute(hOutput,1);
printf("\n\tCould not Find the File!\n");
SetConsoleTextAttribute(hOutput,15);
/*free the stdin buffer*/
while(getchar() != '\n'); enter=false; break;}
else {
for(x=0;x<6;x++){ // attempt to read the file and fill the Key
for(y=0;y<6;y++){
if(fscanf(fp, "%lf", &key[x][y])<1){ // if could not read
fclose(fp);
not36 = true; // report that could not read 36 integers
x=6;
break;
}
}
}
if(validateKey(key) && (!not36)){ // if it's o.k
printf("\n");
fclose(fp);
int option1 = HMenu(12, -1, 5, ">", 15, "|", 9, 15, 128, 3, "Accept", "Load Different Key", "Cancel");
switch(option1){
case 1:
while(getchar()!='\n');
drawKeyFromFile(4, -1,key);
return 1;
case 2:
while(getchar()!='\n');
delete_matrix(key);
key = create_key();
loadKey();
return 1;
case 3:
while(getchar()!='\n');
return 0;
}
}
else {
fclose(fp);
SetConsoleTextAttribute(hOutput,12);
printf("\n\tInvalid Key!\n");
SetConsoleTextAttribute(hOutput,15);
break;
}
}
}
else {while(getchar() != '\n'); enter=false; break;} // if the user hit Enter without inserting anything
break;
case 2:
printf("\n\tInsert The Directory: ");
if(scanf("%256[0-9a-zA-Z:.\\/ ~$@!""£%^&*()_-+=|?<>'#;={[]}]", line)){ // try to be able to read everything, don't judge now
int x,y;
FILE *fp;
bool dir = false; // reference if the input looks to be valid directory
while (*line_ptr!='\0'){
if(*line_ptr == '\\' || *line_ptr == '/' || *line_ptr == ':'){ // indicates that a directory may has been inserted
dir = true;
break;
}
line_ptr++;
}
if (dir == true){
fp = fopen(line, "r");
if (fp == NULL) {
SetConsoleTextAttribute(hOutput,1);
printf("\n\tCould not Find the File!\n");
SetConsoleTextAttribute(hOutput,15);
while(getchar() != '\n'); enter=false; break;}
else {
for (x=0;x<6;x++){
for(y=0;y<6;y++){
if(fscanf(fp, "%lf", &key[x][y])<1){ // because when it reads it returns 1
not36 = true;
fclose(fp);
x=6;
break;
}
}
}
if(validateKey(key) && (!not36)){ // check validity of the Key
printf("\n");
fclose(fp);
int option1 = HMenu(12, -1, 5, ">", 15, "|", 9, 15, 128, 3, "Accept", "Load Different Key", "Cancel");
switch(option1){
case 1:
while(getchar()!='\n');
drawKeyFromFile(4, -1,key);
return 1;
case 2:
while(getchar()!='\n');
delete_matrix(key);
key = create_key();
loadKey();
return 1;
case 3:
while(getchar()!='\n');
return 0;
}
}
else {
fclose(fp);
SetConsoleTextAttribute(hOutput,12);
printf("\n\tInvalid Key!\n");
SetConsoleTextAttribute(hOutput,15);
break;
}
}
}
else {SetConsoleTextAttribute(hOutput,12); printf("\n\tInvalid Directory!\n"); SetConsoleTextAttribute(hOutput,15); break;}
}
else {/*free Stdin*/ while(getchar() != '\n'); enter=false; break;} // if could not read any input
break;
case 3:
return 0;
}
if(enter){while(getchar()!='\n');}
attempts++;
if (attempts==6){SetConsoleTextAttribute(hOutput,12);printf("\n\t***Failure to receive any valid input, No More Retries***\n");SetConsoleTextAttribute(hOutput,15);Sleep(1600); return 0;}
}
}
int saveKey(M key){ // To Save The Key Generated By The Program on the User's Computer
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
int attempts=0;
while (attempts<6){ //number of attempts allowed
printf("\n");
bool enter = true;
char line[256];
int option = HMenu(6, -1, 5, ">", 15, "|", 9, 15, 128, 3, "Save on Desktop", "Save to Directory", "Cancel");
switch (option){
case 1:
printf("\n\tInsert The Key File Name: ");
if(scanf("%256[0-9a-zA-Z ,.'!@#$%^&()-_=+~{[]}; ]", line)){
int x,y;
FILE *fp;
TCHAR szPath[MAX_PATH];
SHGetFolderPath(NULL,CSIDL_DESKTOPDIRECTORY,NULL,0,szPath);
strcat(szPath,"\\");strcat(szPath, line);strcat(szPath,".txt");
fp = fopen(szPath, "w");
if (fp == NULL) {SetConsoleTextAttribute(hOutput, 1);
printf("\n\tCould not Create the File!\n");
SetConsoleTextAttribute(hOutput, 15);
while(getchar() != '\n'); enter=false; break;}
else {
for (x=0;x<6;x++,fprintf(fp,"\n")){
for(y=0;y<6;y++){
fprintf(fp, "%.0lf\t", key[x][y]);
}
}
fclose(fp);
printf("\n\tDONE!\n");
while(getchar() != '\n'); // to free Stdin
return 1;
}
}
else {SetConsoleTextAttribute(hOutput, 12);
printf("\n\tInvalid File Name!\n");
SetConsoleTextAttribute(hOutput, 15);
while(getchar() != '\n'); enter=false; break;}
case 2:
printf("\n\tInsert The Directory: ");
if(scanf("%256[0-9a-zA-Z :.\\/~$@!""£%^&*()_-+=|?<>'#;{[]} ]", line)){
char *line_ptr = line;
bool dir = false;
while (*line_ptr!='\0'){
if(*line_ptr == '\\' || *line_ptr == '/' || *line_ptr == ':'){ // indicates that a directory may has been inserted
dir = true;
break;
}
line_ptr++;
}
if (dir==true){
int x,y;
FILE *fp;
fp = fopen(line, "w");
if (fp == NULL) {SetConsoleTextAttribute(hOutput, 1);
printf("\n Could not Create the File!\n");
SetConsoleTextAttribute(hOutput, 15);
while(getchar() != '\n'); enter=false; break;}
else{
for (x=0;x<6;x++,fprintf(fp,"\n")){
for(y=0;y<6;y++){
fprintf(fp, "%.0lf\t", key[x][y]);
}
}
fclose(fp);
printf("\n\tDONE!\n");
while(getchar() != '\n'); // to free Stdin
return 1;
}
}
else {SetConsoleTextAttribute(hOutput, 12); printf("\n\tInvalid Directory\n"); SetConsoleTextAttribute(hOutput, 15); break;}
}
else {while(getchar() != '\n'); enter=false; break;}
case 3:
return 0;
}
if(enter) {while(getchar()!='\n');}
attempts++;
if (attempts==6){SetConsoleTextAttribute(hOutput, 12);printf("\n***Failure To Receive Valid Input, No More Retries***\n");SetConsoleTextAttribute(hOutput, 15);Sleep(1600); return 0;}
}
}
int saveInsertedKey(M key){ // To Save The Key Inserted By User on the User's Computer
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
int attempts=0;
while (attempts<6){ //number of attempts allowed
printf("\n");
bool enter = true;
int option = HMenu(1, -1, 4, ">", 15, "|", 9, 15, 128, 4, "Save on Desktop", "Save to Directory", "Continue Without Save", "Cancel");
char line[256];
switch (option){
case 1:
printf("\n\tInsert The Key File Name: ");
if(scanf("%256[0-9a-zA-Z ,.'!@#$%^&()-_=+~{[]}; ]", line)){
int x,y;
FILE *fp;
TCHAR szPath[MAX_PATH];
SHGetFolderPath(NULL,CSIDL_DESKTOPDIRECTORY,NULL,0,szPath);
strcat(szPath,"\\");strcat(szPath, line);strcat(szPath,".txt");
fp = fopen(szPath, "w");
if (fp == NULL) {SetConsoleTextAttribute(hOutput, 1);
printf("\n\tCould not Create the File!\n");
SetConsoleTextAttribute(hOutput, 15);
while(getchar() != '\n'); enter=false; break;}
else {
for (x=0;x<6;x++,fprintf(fp,"\n")){
for(y=0;y<6;y++){
fprintf(fp, "%.0lf\t", key[x][y]);
}
}
fclose(fp);
printf("\n\tDONE!\n");
while(getchar() != '\n'); // to free Stdin
return 1;
}
}
else {SetConsoleTextAttribute(hOutput, 12);
printf("\n\tInvalid File Name!\n");
SetConsoleTextAttribute(hOutput, 15);
while(getchar() != '\n'); enter=false; break;}
case 2:
printf("\n\tInsert The Directory: ");
if(scanf("%256[0-9a-zA-Z:.\\/~$@!""£%^&*()_-+=|?<>'#;{[]} ]", line)){
char *line_ptr = line;
bool dir = false;
while (*line_ptr!='\0'){
if(*line_ptr == '\\' || *line_ptr == '/' || *line_ptr == ':'){ // indicates that a directory may has been inserted
dir = true;
break;
}
line_ptr++;
}
if (dir==true){
int x,y;
FILE *fp;
fp = fopen(line, "w");
if (fp == NULL) {SetConsoleTextAttribute(hOutput, 1);
printf("\n Could not Create the File!\n");
SetConsoleTextAttribute(hOutput, 15);
while(getchar() != '\n'); enter=false; break;}
else{
for (x=0;x<6;x++,fprintf(fp,"\n")){
for(y=0;y<6;y++){
fprintf(fp, "%.0lf\t", key[x][y]);
}
}
fclose(fp);
printf("\n\tDONE!\n");
while(getchar() != '\n'); // to free Stdin
return 1;
}
}
else {SetConsoleTextAttribute(hOutput, 12); printf("\n\tInvalid Directory\n"); SetConsoleTextAttribute(hOutput, 15); break;}
}
else {while(getchar() != '\n'); enter=false; break;}
case 3:
return 1;
case 4:
return 0;
}
if(enter) {while(getchar()!='\n');}
attempts++;
if (attempts==6){SetConsoleTextAttribute(hOutput, 12); printf("\n***Failure To Receive Valid Input, No More Retries***\n");SetConsoleTextAttribute(hOutput, 15);Sleep(1600);return 0;}
}
}
int validateKey(M key){ // Validate Inserted Key By User or Uploaded From File (Important: Shared between Encryption & Decryption!)
int x, y;
determinant = calloc(1, sizeof(double));
diagonalSum = 0, secondaryDiagonalSum=0;
for (x=0;x<6;x++){
for(y=0;y<6;y++){
if (x==y){diagonalSum += key[x][y];}
if ((y+x)==5){secondaryDiagonalSum += key[x][y];}
}
}
determinant = mDet(6, "lf", key);
int greaterCommonDivisor = gcd(*determinant);
if (*determinant!=0 && diagonalSum!=0 && greaterCommonDivisor!=1){free(determinant); return 1;}
else if (*determinant!=0 && secondaryDiagonalSum!=0 && greaterCommonDivisor!=1){free(determinant); return 1;}
else {free(determinant); return 0;}
}
int drawKey(int vpos, M key){ // Draw The Key that generated by the program on the Console
int x,y,i=3,j=1;
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE); // general STDout Handle
if(vpos<0){
/*Find The Cursor Position*/
CONSOLE_SCREEN_BUFFER_INFO SBInfo;
GetConsoleScreenBufferInfo(hOutput, &SBInfo);
vpos = SBInfo.dwCursorPosition.Y;
}
COORD cursor;
for(x=0;x<6;x++,i=3,j+=2){
for(y=0; y<6; y++){
cursor.X =i;
cursor.Y = vpos+j;
SetConsoleCursorPosition(hOutput, cursor);
SetConsoleTextAttribute(hOutput, 3);
char numberLength[10];
sprintf(numberLength, "%.0lf", key[x][y]);
WriteConsoleA(hOutput, numberLength, strlen(numberLength), NULL, NULL);
i+=7;
WriteConsoleA(hOutput, " ", 6, NULL, NULL);
}
}
printf("\n\n");
return vpos;
}
int drawKeyFromFile(int indent, int vpos, M key){ // Draw The Key that loaded from a file on the Console (Important: Shared between Encryption & Decryption!)
printf("\n");
int x,y,i=3,j=1;
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE); // general Stdout Handle
if(vpos<0){
/*Find The Cursor Position*/
CONSOLE_SCREEN_BUFFER_INFO SBInfo;
GetConsoleScreenBufferInfo(hOutput, &SBInfo);
vpos = SBInfo.dwCursorPosition.Y;
}
COORD cursor;
cursor.Y = vpos;
SetConsoleCursorPosition(hOutput, cursor);
SetConsoleTextAttribute(hOutput, 3);
printf("\n\t");
for (x=0;x<6;x++, printf("\n\t")){
for(y=0;y<6;y++){
printf("%.0lf\t", key[x][y]);
}
}
SetConsoleTextAttribute(hOutput, 15);
printf("\n\t DONE!\n");
return vpos;
}
double controlledRandom(){
int upperlimits[300]; // 300 different upper limits
int x;
for (x=0;x<300;x++){ // generate upper limits
upperlimits[x]= x+1;
}
double result = (double) (rand()%(upperlimits[rand()%300]));
return result;
}