-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
817 lines (753 loc) · 20.4 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
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
// Angel Odiel Treviño Villanueva
// A01336559
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
// =============== STRUCTURESFILLER ========================
// ---------------------------------------------------------
void fillMap(
std::map<char, int> &temp_map,
string str
) {
for(int i = 0; i < str.length(); i++) {
temp_map[str[i]] = i+1;
}
}
// ---------------------------------------------------------
void fillMapAlphabetValues(
std::map<int, char> &temp_map,
string str
) {
for(int i = 0; i < str.length(); i++) {
temp_map[i] = str[i];
}
}
// ---------------------------------------------------------
void fillMapBackwords(
//for alphabets that are coming backwords
std::map<char, int, greater <char> > &temp_map,
string str
) {
for(int i = str.length()-1, x=1; i >= 0; i--, x++) {
temp_map[str[i]] = x;
}
}
// ---------------------------------------------------------
void fillMapValues(
std::map<char, char> &temp_map,
string strKeys,
string strValues
) {
for (int i = 0; i < strKeys.length(); i++) {
temp_map[strKeys[i]] = strValues[i];
}
}
// ---------------------------------------------------------
void fillKeywordVector(
// for keys that need to store their value in the position
// of a certain alphabet
std::map<char, int> &AlphabetMap,
vector<int> &vectorKey,
string strKey
) {
for (int i = 0; i < strKey.length(); i++) {
vectorKey.push_back(AlphabetMap[strKey[i]]);
}
}
// ---------------------------------------------------------
void fillKeywordVectorMapBackwords(
std::map<char, int, greater <char> > &temp_map,
vector<int> &vectorKey,
string strKey
) {
for(int i = 0; i < strKey.length(); i++) {
vectorKey.push_back(temp_map[strKey[i]]);
}
}
// ---------------------------------------------------------
void fillVectorKeyValue(
vector<int> &vectorKey,
string strKey
) {
int intAux;
string strAux;
for(int i = 0; i < strKey.length(); i++) {
if(strKey[i] == '-') {
intAux = stoi(strAux);
vectorKey.push_back(intAux);
strAux = "";
} else {
strAux += strKey[i];
}
}
intAux = stoi(strAux);
vectorKey.push_back(intAux);
}
// ---------------------------------------------------------
void fillVectorString(
vector<char> &vector,
string str
) {
for(int i = 0; i < str.length(); i++) {
vector.push_back(str[i]);
}
}
// ---------------------------------------------------------
void fillMatrix(
vector<vector<int> > &Matrix,
int intRow,
int intCol,
string str
) {
int intCont = 0;
for(int i = 0; i < intRow; i++) {
for(int j = 0; j < intCol; j++) {
Matrix[i][j] = str[intCont];
intCont++;
}
}
}
// ---------------------------------------------------------
string generateRandomString(int size) {
string result = "";
char alphabet[] = "01";
for(int i = 0; i < size; i++) {
result += alphabet[rand() %(sizeof(alphabet) - 1)];
}
return result;
}
// ---------------------------------------------------------
string generateKeyPermutation(int size) {
string result = "";
vector<int> storage;
int aux;
for(int i = 1; i <= size; i++) {
storage.push_back(i);
}
// loop to create new number
for(int i = 0; i < size; i++) {
aux = rand() % storage.size();
result += std::to_string(storage[aux]);
if (i < size-1) {
result += '-';
}
storage.erase(storage.begin() + aux);
}
return result;
}
//----------------------------------------------------------
void fillFile(
vector<string> &collection,
string strName
) {
ofstream myfile;
string aux = "";
myfile.open(strName);
for(int i = 0; i < collection.size(); i++) {
aux = collection[i];
for(int j = 0; j < aux.length(); j++) {
myfile << aux[j];
if (j < aux.length()-1) {
myfile << ",";
}
}
myfile << "\n";
}
myfile.close();
}
// ================ HELPERCIPHERS ==========================
// ---------------------------------------------------------
string shiftToRight(
// this shift is for maps that dont need their order
// modified
std::map<char, int> &temp_map,
string str,
int intKey
) {
std::map<char,int>::iterator it;
string result;
for(int i = 0; i < str.length(); i++) {
it = temp_map.find(str[i]);
if(
//check if we found our char
it != temp_map.end()
) {
int pos = it->second;
if(
// check if we havent pass the size of the map
it->second + intKey < temp_map.size()
) {
while(it != temp_map.end()){
if(
it->second == pos+intKey
) {
result += it->first;
break;
}
it++;
}
} else {
int intPos = (intKey + it->second) % temp_map.size();
if(intPos == 0) {
intPos += temp_map.size();
}
it = temp_map.begin();
while(it != temp_map.end()) {
if(
it->second == intPos
) {
result += it->first;
break;
}
it++;
}
}
} else {
// keep the char as it was if not included in the alphabet
result += str[i];
}
}
return result;
}
// --------------------------------------------------------
string shiftToRightBackwords(
// this shift is for maps that come with descending order,
// basically does the same but due to how maps work needs
// to be another function
std::map<char, int, greater <char> > &temp_map,
string str,
int intKey
) {
std::map<char,int>::iterator it;
string result;
for(int i = 0; i < str.length(); i++) {
it = temp_map.find(str[i]);
if(
//check if we found our char
it != temp_map.end()
) {
int pos = it->second;
if(
// check if we havent pass the size of the map
it->second + intKey < temp_map.size()
) {
while(it != temp_map.end()){
if(
it->second == pos+intKey
) {
result += it->first;
break;
}
it++;
}
} else {
int intPos = (intKey + it->second) % temp_map.size();
if(intPos == 0) {
intPos += temp_map.size();
}
it = temp_map.begin();
while(it != temp_map.end()) {
if(
it->second == intPos
) {
result += it->first;
break;
}
it++;
}
}
} else {
// keep the char as it was if not included in the alphabet
result += str[i];
}
}
return result;
}
// ================== CIPHERS ==============================
// ---------------------------------------------------------
string encryptShiftCipher(
string strAlphabet,
string strKeyword,
int intKey
) {
std::map<char, int> Alphabet;
fillMap(Alphabet, strAlphabet);
return shiftToRight(
Alphabet,
strKeyword,
intKey
);
}
// ---------------------------------------------------------
string decryptShiftCipher(
string strAlphabet,
string strKeyword,
int intKey
){
std::map<char, int, greater <char> > Alphabet;
fillMapBackwords(Alphabet, strAlphabet);
return shiftToRightBackwords(
Alphabet,
strKeyword,
intKey
);
}
// ---------------------------------------------------------
string SubstitutionCipher(
// do to Substitution Cipher changing the values then we
// just reverse who is key and who is value inside our map
string strAlphabetKey,
string strAlphabetValue,
string strKeyword
) {
std::map<char, char>:: iterator it;
std::map<char, char> Alphabet;
string result = "";
fillMapValues(Alphabet, strAlphabetKey, strAlphabetValue);
for(int i = 0; i < strKeyword.length(); i++) {
it = Alphabet.find(strKeyword[i]);
if(
// check if we found our char
it != Alphabet.end()
) {
result += it->second;
} else {
result += strKeyword[i];
}
}
return result;
}
// ---------------------------------------------------------
string encryptVigenereCipher(
string strAlphabet,
string strKey,
string strKeyword
) {
std::map<char, int> Alphabet;
vector<int> Key;
string result = "";
string send;
int posVector;
fillMap(Alphabet, strAlphabet);
fillKeywordVector(Alphabet, Key, strKey);
posVector = 0;
for(int i = 0; i < strKeyword.length(); i++) {
send = "";
send.push_back(strKeyword[i]);
result += shiftToRight(Alphabet, send, Key[posVector]-1);
if (posVector < Key.size()-1) {
posVector++;
} else {
posVector = 0;
}
}
return result;
}
// ---------------------------------------------------------
string decryptVigenereCipher(
string strAlphabet,
string strKey,
string strKeyword
) {
std::map<char, int, greater <char> > Alphabet;
vector<int> Key;
string result = "";
string send;
int posVector;
fillMapBackwords(Alphabet, strAlphabet);
fillKeywordVectorMapBackwords(Alphabet, Key, strKey);
posVector = 0;
for(int i = 0; i < strKeyword.length(); i++) {
send = "";
send.push_back(strKeyword[i]);
result += shiftToRightBackwords(
Alphabet,
send,
// we do this substraction because our alphabet is backwords
strAlphabet.length() - Key[posVector]
);
if(posVector < Key.size()-1) {
posVector++;
} else {
posVector = 0;
}
}
return result;
}
// -------------------------------------------------
string encryptPermutationCipher(
string strKey,
string strKeyword
) {
vector<int> Key;
vector<char> Letters;
string strAux;
string result;
int intPos;
fillVectorKeyValue(Key, strKey);
if(
Key.size() > strKey.length()
) {
return "A number inside the key is bigger than the size of the key";
} else {
intPos = 0;
while (intPos < strKeyword.length()) {
Letters.clear();
strAux = strKeyword.substr(intPos, strKey.length());
fillVectorString(Letters, strAux);
if(
Letters.size() < Key.size()
) {
for (int i = Letters.size(); i <= strAux.length(); i++) {
Letters.push_back('A');
}
}
for (int i = 0; i < Letters.size(); i++) {
result+= Letters[Key[i]-1];
}
intPos+= strKey.length();
}
return result;
}
}
// ---------------------------------------------------------
string decryptPermutationCipher(
string strKey,
string strKeyword
) {
vector<int> Key;
vector<char> Letters;
vector<char> Result;
string strAux;
string result = "";
int intPos;
fillVectorKeyValue(Key, strKey);
if(
Key.size() > strKey.length()
) {
return "A number inside the key is bigger than the size of the key";
} else {
int intPos = 0;
while(intPos < strKeyword.length()) {
Letters.clear();
Result.clear();
for(int i = 0; i < strKey.length(); i++) {
Result.push_back(' ');
}
strAux = strKeyword.substr(intPos, strKey.length());
fillVectorString(Letters, strAux);
for (int i = 0; i < Letters.size(); i++) {
Result[Key[i]-1] = Letters[i];
}
for (int i = 0; i < Result.size(); i++) {
result += Result[i];
}
intPos+= strKey.length();
}
return result;
}
}
// ---------------------------------------------------------
string TranspositionCipher(
// Option help us determine how to fill out matrix
string strKeyword,
int intKey,
int intOption
) {
string strResult = "";
int intRow;
int intCol;
if (
// we are encrypting
intOption == 0
) {
intRow = strKeyword.length() / intKey;
intCol = intKey;
if (
strKeyword.length() % intKey > 0
){
intRow++;
}
} else {
intCol = strKeyword.length() / intKey;
intRow = intKey;
if (
strKeyword.length() % intKey > 0
){
intCol++;
}
}
vector<vector<int> > Matrix(intRow, vector<int>(intCol));
fillMatrix(Matrix, intRow, intCol, strKeyword);
for(int i = 0; i < intCol; i++) {
for(int j = 0; j < intRow; j++) {
strResult += Matrix[j][i];
}
}
return strResult;
}
// ---------------------------------------------------------
string encryptMultiplicationCipher() {
std::map<char, int>::iterator it;
std::map<char, int> Alphabet;
std::map<int, char> AlphabetValue;
string strKey;
string strAlphabet;
string strKeyword;
string strResult = "";
int intKey;
int Pos;
cout << "=================================" << endl;
cout << "Enter your Alphabet" << endl;
getline(cin, strAlphabet);
cout << "Enter your Keyword" << endl;
getline(cin, strKeyword);
cout << "Enter your Key" << endl;
cin >> intKey;
fillMap(Alphabet, strAlphabet);
fillMapAlphabetValues(AlphabetValue, strAlphabet);
for(int i = 0; i < strKeyword.length(); i++) {
it = Alphabet.find(strKeyword[i]);
if(
it != Alphabet.end()
){
Pos = ((it->second-1) * intKey)%26;
strResult += AlphabetValue[Pos];
} else {
strResult += strKeyword[i];
}
}
return strResult;
}
// ================ INPUTCIPHERS ===========================
// ---------------------------------------------------------
string useShiftCipher() {
string option;
string strAlphabet;
string strKeyword;
int intKey;
cout << "=================================" << endl;
cout << "Enter your Alphabet" << endl;
getline(cin, strAlphabet);
cout << "Enter your Keyword" << endl;
getline(cin, strKeyword);
cout << "Enter the Key" << endl;
cin >> intKey;
cout << "=================================" << endl;
cout << "Do you want to Encrypt or Decrypt?" << endl;
cin >> option;
cin.ignore();
if(
option.compare("Encrypt") == 0
) {
return encryptShiftCipher(strAlphabet, strKeyword, intKey);
} else if(
option.compare("Decrypt") == 0
) {
return decryptShiftCipher(strAlphabet, strKeyword, intKey);
} else {
return "Could not understand " + option;
}
}
// ---------------------------------------------------------
string useSubstitutionCipher() {
string option;
string strAlphabet;
string strCipherAlphabet;
string strKeyword;
cout << "=================================" << endl;
cout << "Enter your Alphabet" << endl;
getline(cin, strAlphabet);
cout << "Enter your Cipher Alphabet" << endl;
getline(cin, strCipherAlphabet);
cout << "Enter your Keyword" << endl;
getline(cin, strKeyword);
cout << "Do you want to Encrypt or Decrypt?" << endl;
cin >> option;
cin.ignore();
if(
strAlphabet.length() != strCipherAlphabet.length()
) {
return "Alphabet's size don't match ";
} else if(
option.compare("Encrypt") == 0
) {
return SubstitutionCipher(strAlphabet, strCipherAlphabet, strKeyword);
} else if (
option.compare("Decrypt") == 0
) {
return SubstitutionCipher(strCipherAlphabet, strAlphabet, strKeyword);
} else {
return "Could not understand " + option;
}
}
// ---------------------------------------------------------
string useVigenereCipher(
// basically is Shift Cipher but the key changes depending
// on the value of the character of our key on the alphabet
) {
string strOption;
string strAlphabet;
string strKey;
string strKeyword;
cout << "=================================" << endl;
cout << "Enter your Alphabet" << endl;
getline(cin, strAlphabet);
cout << "Enter your Key" << endl;
getline(cin, strKey);
cout << "Enter your Keyword" << endl;
getline(cin, strKeyword);
cout << "Do you want to Encrypt or Decrypt?" << endl;
cin >> strOption;
cin.ignore();
if(
strOption.compare("Encrypt") == 0
) {
return encryptVigenereCipher(strAlphabet, strKey, strKeyword);
} else if (
strOption.compare("Decrypt") == 0
) {
return decryptVigenereCipher(strAlphabet, strKey, strKeyword);
} else {
return "Could not understand " + strOption;
}
}
// ---------------------------------------------------------
string usePermutationCipher() {
string strOption;
string strKey;
string strKeyword;
cout << "=================================" << endl;
cout << "Enter your Key, enter divided by -" << endl;
getline(cin, strKey);
cout << "Enter your keyword" << endl;
getline(cin, strKeyword);
cout << "Do you want to Encrypt or Decrypt" << endl;
cin >> strOption;
cin.ignore();
if(
strOption.compare("Encrypt") == 0
) {
return encryptPermutationCipher(strKey, strKeyword);
} else if (
strOption.compare("Decrypt") == 0
) {
return decryptPermutationCipher(strKey, strKeyword);
} else {
return "Could not understand " + strOption;
}
}
// ---------------------------------------------------------
string useTranspositionCipher() {
string strOption;
string strKeyword;
int intKey;
cout << "=================================" << endl;
cout << "Enter your Key" << endl;
cin >> intKey;
cin.ignore();
cout << "Enter your Keyword" << endl;
getline(cin, strKeyword);
cout << "Do you want to Encrypt or Decrypt?" << endl;
cin >> strOption;
cin.ignore();
if (
strOption.compare("Encrypt") == 0
) {
return TranspositionCipher(strKeyword, intKey, 0);
} else if (
strOption.compare("Decrypt") == 0
) {
return TranspositionCipher(strKeyword, intKey, 1);
} else {
return "Could not understand " + strOption;
}
}
// ---------------------------------------------------------
void fillBunchPermutation() {
vector<string> plainText;
vector<string> cipherText;
int intMany;
int intOption;
string aux;
string key;
string firstFile;
string secondFile;
cout << "=================================" << endl;
cout << "How many do you need?" << endl;
cin >> intMany;
cout << "Name to store your plain strings?" << endl;
cin >> firstFile;
cout << "Name to store your cipher strings?" << endl;
cin >> secondFile;
cout << "Do you want a 1.random key or 2.your own?" << endl;
cin >> intOption;
srand(time(NULL));
// generate our plain strings
for (int i = 0; i < intMany; i++) {
aux = generateRandomString(25);
if(
// check if its not 0
aux.compare("0000000000000000000000000") == 0
) {
i--;
} else {
plainText.push_back(aux);
}
}
if (intOption == 1) {
key = generateKeyPermutation(25);
} else if (intOption == 2) {
cout << "Enter Key, numbers divided by -" << endl;
cin >> key;
}
cout << "Key= " << key << endl;
cout << "=================================" << endl;
aux = "";
// generate cipher text
for(int i = 0; i < intMany; i++) {
aux = encryptPermutationCipher(key, plainText[i]);
cipherText.push_back(aux);
}
fillFile(plainText, firstFile);
fillFile(cipherText, secondFile);
}
// ==================== MAIN ===============================
// ---------------------------------------------------------
int main() {
string result;
int intOption;
cout << "=================================" << endl;
cout << "What Cypher do you want to do?" << endl;
cout << "1. Shift Cipher" << endl;
cout << "2. Substitution Cipher" << endl;
cout << "3. Vigenere Cipher" << endl;
cout << "4. Permutation Cipher" << endl;
cout << "5. Transposition Cipher" << endl;
cout << "6. Encrypt Multiplication Cipher" << endl;
cout << "7. generate a ton of permutation" << endl;
cin >> intOption;
cin.ignore();
if(intOption == 1) {
result = useShiftCipher();
} else if (intOption == 2) {
result = useSubstitutionCipher();
} else if (intOption == 3) {
result = useVigenereCipher();
} else if (intOption == 4) {
result = usePermutationCipher();
} else if (intOption == 5) {
result = useTranspositionCipher();
} else if (intOption == 6) {
result = encryptMultiplicationCipher();
} else if (intOption == 7) {
fillBunchPermutation();
result = "done";
}
cout << "=================================" << endl;
cout << result << endl;
return 0;
}