-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
634 lines (487 loc) · 18.1 KB
/
main.cpp
File metadata and controls
634 lines (487 loc) · 18.1 KB
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
#include <iostream>
#include <vector>
#include<string>
#include<fstream>
#include<random>
#include <ctime>
#include <math.h>
using namespace std;
vector<double> freqOfLetters(){
vector<double> freqVec;
freqVec.push_back(0.008838384); //a
freqVec.push_back(0.01010101); //b
freqVec.push_back(0.01952862); //c
freqVec.push_back(0.007575758); //d
freqVec.push_back(0.00979021); //e
freqVec.push_back(0.007070707); //f
freqVec.push_back(0.013131313); //g
freqVec.push_back(0.003703704); //h
freqVec.push_back(0.012698413); //i
freqVec.push_back(0.00102); //j
freqVec.push_back(0.016161616); //k
freqVec.push_back(0.01010101); //l
freqVec.push_back(0.013131313); //m
freqVec.push_back(0.010966811); //n
freqVec.push_back(0.006313131); //o
freqVec.push_back(0.009090909); //p
freqVec.push_back(0.006060606); //q
freqVec.push_back(0.014141414); //r
freqVec.push_back(0.013131313); //s
freqVec.push_back(0.008080808); //t
freqVec.push_back(0.008754209); //u
freqVec.push_back(0.012121212); //v
freqVec.push_back(0.005050505); //w
freqVec.push_back(0.004040404); //x
freqVec.push_back(0.011111111); //y
freqVec.push_back(0.004040404); //z
return freqVec;
}
/*
This function checks if an int is in a vector. It's really trivial
*/
bool isInThere(vector<int> vec, int elem){
for(size_t i = 0; i < vec.size(); i++){
if (elem == vec[i]){
return true;
}
}
return false;
}
int isInThereInt(vector<int> vec, int elem, int counter=0){
for(size_t i=0; i<vec.size(); i++){
if (elem == vec[i]){
counter++;
}
}
return counter;
}
/*
This function sorts the words by their lengths. So we have a vector of string vectors where all the 1 letter, 2 letter,
3 letter, words, are sorted by size.
*/
void lengthWordSorter(vector<string> dict,vector<vector<string>>& dictByLength, int max_length){
for(size_t i=0; i< max_length; i++){
vector<string> wordsOfSomeLength;
dictByLength.push_back(wordsOfSomeLength);
}
for(size_t i = 0; i<dict.size(); i++){
int strLen = (dict[i]).length();
if(dict[i] != " "){
(dictByLength[strLen-1]).push_back(dict[i]);
}
}
}
/*
I tried to randomly pick words from the text dictionary that would sum to be 500 characters but I don't
have it working right. I kind of messed up with the random function too. The problem is the sum of the characters is less than 500.
Like 489 instead of 500 so I don't know how to fix that problem. And my list of random words always starts with the same word.
*/
vector<string> createPlainText(vector<string>& dict, vector<vector<string>>& dictByLength){
int sumLenString = 0;
vector<string> plainTextVec;
vector<int> indexAlreadyVisited;
indexAlreadyVisited.push_back(-1);
random_device rd;
mt19937 generator(rd());
uniform_int_distribution<int> dictDist(0,dict.size());
while(sumLenString < 500 ){
uniform_int_distribution<int> lengthDist(0, dictByLength[500-sumLenString].size());
int temp = lengthDist(generator);
if(500-sumLenString>0 && 1000-sumLenString < 10 && dictByLength[500-sumLenString][temp] != " "){
vector<string> tempVecString = dictByLength[1000-sumLenString];
plainTextVec.push_back(tempVecString[temp]);
indexAlreadyVisited.push_back(temp);
//break;
}
//int random = dictDist(generator);
// int random = rand() % dict.size();
if(!isInThere(indexAlreadyVisited, dictDist(generator)) && dict[dictDist(generator)] != " "){
plainTextVec.push_back(dict[dictDist(generator)]);
indexAlreadyVisited.push_back(dictDist(generator));
sumLenString += (dict[dictDist(generator)]).length();
}
}
/*
for(size_t i = 0; i < plainTextVec.size(); i++){
cout<<plainTextVec[i]<<endl;
}
*/
return plainTextVec;
}
/*
This function returns a vector of vector ints that serve as the encryption for
the letters. So a gets 8 random numbers between 1 and 102, b gets 1 random number between 1 and 102 that is not
already used by a and so on for each character.n From this table we can encrypt a message. How it would work is that if
you read in the character a you randomly pink one of the eight numbers assigned to the "a" vector or in this case the first
vector of the vector of vector ints. That will be the encryption for that. Then repeat for each character.
*/
vector<vector<int>> genCryptTable(){
vector<int> chooseIntsFrom;
int numUniqueInts = 103;
for(size_t i = 0; i < numUniqueInts; i++){
chooseIntsFrom.push_back(i);
}
vector<vector<int>> cryptTable;
// "a"
vector<int> charNums0;
for(size_t i=0; i<8; i++){
int random = rand() % numUniqueInts;
charNums0.push_back(chooseIntsFrom[random]);
chooseIntsFrom.erase(chooseIntsFrom.begin() + random);
numUniqueInts--;
}
cryptTable.push_back(charNums0);
//"b"
vector<int> charNums1;
int random = rand() % numUniqueInts;
charNums1.push_back(chooseIntsFrom[random]);
chooseIntsFrom.erase(chooseIntsFrom.begin() + random);
numUniqueInts -= 1;
cryptTable.push_back(charNums1);
//"c"
vector<int> charNums2;
for(size_t i=0; i<3; i++){
int random = rand() % numUniqueInts;
charNums2.push_back(chooseIntsFrom[random]);
chooseIntsFrom.erase(chooseIntsFrom.begin() + random);
numUniqueInts-=1;
}
cryptTable.push_back(charNums2);
//"d"
vector<int> charNums3;
for(size_t i=0; i<4; i++){
int random = rand() % numUniqueInts;
charNums3.push_back(chooseIntsFrom[random]);
chooseIntsFrom.erase(chooseIntsFrom.begin() + random);
numUniqueInts-=1;
}
cryptTable.push_back(charNums3);
//"e"
vector<int> charNums4;
for(size_t i=0; i<13; i++){
int random = rand() % numUniqueInts;
charNums4.push_back(chooseIntsFrom[random]);
chooseIntsFrom.erase(chooseIntsFrom.begin() + random);
numUniqueInts-=1;
}
cryptTable.push_back(charNums4);
//"f"
vector<int> charNums5;
for(size_t i=0; i<2; i++){
int random = rand() % numUniqueInts;
charNums5.push_back(chooseIntsFrom[random]);
chooseIntsFrom.erase(chooseIntsFrom.begin() + random);
numUniqueInts-=1;
}
cryptTable.push_back(charNums5);
//"g"
vector<int> charNums6;
for(size_t i=0; i<2; i++){
int random = rand() % numUniqueInts;
charNums6.push_back(chooseIntsFrom[random]);
chooseIntsFrom.erase(chooseIntsFrom.begin() + random);
numUniqueInts-=1;
}
cryptTable.push_back(charNums6);
//"h'
vector<int> charNums7;
for(size_t i=0; i<6; i++){
int random = rand() % numUniqueInts;
charNums7.push_back(chooseIntsFrom[random]);
chooseIntsFrom.erase(chooseIntsFrom.begin() + random);
numUniqueInts-=1;
}
cryptTable.push_back(charNums7);
//"i"
vector<int> charNums8;
for(size_t i=0; i<7; i++){
int random = rand() % numUniqueInts;
charNums8.push_back(chooseIntsFrom[random]);
chooseIntsFrom.erase(chooseIntsFrom.begin() + random);
numUniqueInts-=1;
}
cryptTable.push_back(charNums8);
//"j"
vector<int> charNums9;
int random1 = rand() % numUniqueInts;
charNums9.push_back(chooseIntsFrom[random1]);
chooseIntsFrom.erase(chooseIntsFrom.begin() + random1);
numUniqueInts -= 1;
cryptTable.push_back(charNums9);
//"k"
vector<int> charNums10;
int random2 = rand() % numUniqueInts;
charNums10.push_back(chooseIntsFrom[random2]);
chooseIntsFrom.erase(chooseIntsFrom.begin() + random2);
numUniqueInts -= 1;
cryptTable.push_back(charNums10);
//"l"
vector<int> charNums11;
for(size_t i=0; i<4; i++){
int random = rand() % numUniqueInts;
charNums11.push_back(chooseIntsFrom[random]);
chooseIntsFrom.erase(chooseIntsFrom.begin() + random);
numUniqueInts-=1;
}
cryptTable.push_back(charNums11);
//"m"
vector<int> charNums12;
for(size_t i=0; i<2; i++){
int random = rand() % numUniqueInts;
charNums12.push_back(chooseIntsFrom[random]);
chooseIntsFrom.erase(chooseIntsFrom.begin() + random);
numUniqueInts-=1;
}
cryptTable.push_back(charNums12);
//"n"
vector<int> charNums13;
for(size_t i=0; i<7; i++){
int random = rand() % numUniqueInts;
charNums13.push_back(chooseIntsFrom[random]);
chooseIntsFrom.erase(chooseIntsFrom.begin() + random);
numUniqueInts-=1;
}
cryptTable.push_back(charNums13);
//"o"
vector<int> charNums14;
for(size_t i=0; i<8; i++){
int random = rand() % numUniqueInts;
charNums14.push_back(chooseIntsFrom[random]);
chooseIntsFrom.erase(chooseIntsFrom.begin() + random);
numUniqueInts-=1;
}
cryptTable.push_back(charNums14);
//"p"
vector<int> charNums15;
for(size_t i=0; i<2; i++){
int random = rand() % numUniqueInts;
charNums15.push_back(chooseIntsFrom[random]);
chooseIntsFrom.erase(chooseIntsFrom.begin() + random);
numUniqueInts-=1;
}
cryptTable.push_back(charNums15);
//"q"
vector<int> charNums16;
int random3 = rand() % numUniqueInts;
charNums16.push_back(chooseIntsFrom[random3]);
chooseIntsFrom.erase(chooseIntsFrom.begin() + random3);
numUniqueInts -= 1;
cryptTable.push_back(charNums16);
//"r"
vector<int> charNums17;
for(size_t i=0; i<6; i++){
int random = rand() % numUniqueInts;
charNums17.push_back(chooseIntsFrom[random]);
chooseIntsFrom.erase(chooseIntsFrom.begin() + random);
numUniqueInts-=1;
}
cryptTable.push_back(charNums17);
//"s"
vector<int> charNums18;
for(size_t i=0; i<6; i++){
int random = rand() % numUniqueInts;
charNums18.push_back(chooseIntsFrom[random]);
chooseIntsFrom.erase(chooseIntsFrom.begin() + random);
numUniqueInts-=1;
}
cryptTable.push_back(charNums18);
//"t"
vector<int> charNums19;
for(size_t i=0; i<9; i++){
int random = rand() % numUniqueInts;
charNums19.push_back(chooseIntsFrom[random]);
chooseIntsFrom.erase(chooseIntsFrom.begin() + random);
numUniqueInts-=1;
}
cryptTable.push_back(charNums19);
//"u"
vector<int> charNums20;
for(size_t i=0; i<3; i++){
int random = rand() % numUniqueInts;
charNums20.push_back(chooseIntsFrom[random]);
chooseIntsFrom.erase(chooseIntsFrom.begin() + random);
numUniqueInts-=1;
}
cryptTable.push_back(charNums20);
//"v"
vector<int> charNums21;
int random4 = rand() % numUniqueInts;
charNums21.push_back(chooseIntsFrom[random4]);
chooseIntsFrom.erase(chooseIntsFrom.begin() + random4);
numUniqueInts -= 1;
cryptTable.push_back(charNums21);
//"w"
vector<int> charNums22;
for(size_t i=0; i<2; i++){
int random = rand() % numUniqueInts;
charNums22.push_back(chooseIntsFrom[random]);
chooseIntsFrom.erase(chooseIntsFrom.begin() + random);
numUniqueInts-=1;
}
cryptTable.push_back(charNums22);
//"x"
vector<int> charNums23;
int random5 = rand() % numUniqueInts;
charNums23.push_back(chooseIntsFrom[random5]);
chooseIntsFrom.erase(chooseIntsFrom.begin() + random5);
numUniqueInts -= 1;
cryptTable.push_back(charNums23);
//"y"
vector<int> charNums24;
for(size_t i=0; i<2; i++){
int random = rand() % numUniqueInts;
charNums24.push_back(chooseIntsFrom[random]);
chooseIntsFrom.erase(chooseIntsFrom.begin() + random);
numUniqueInts-=1;
}
cryptTable.push_back(charNums24);
//"z"
vector<int> charNums25;
int random6 = rand() % numUniqueInts;
charNums25.push_back(chooseIntsFrom[0]);
chooseIntsFrom.erase(chooseIntsFrom.begin() + random6);
numUniqueInts -= 1;
cryptTable.push_back(charNums25);
return cryptTable;
}
vector<vector<int>> encrypt(vector<string>& dict, vector<vector<string>>& dictByLength){
vector<vector<int>> cryptTable = genCryptTable();
vector<vector<int>> encryptedText;
vector<string> trialPlainText = createPlainText(dict, dictByLength);
//generates plain text
cout<<"The plain text is: "<<endl;
for(size_t i=0; i<trialPlainText.size(); i++){
cout<<trialPlainText[i]<<endl;
}
for(size_t i=0; i < trialPlainText.size(); i++){
vector<int> wordAsNumbers;
for(size_t j=0; j<trialPlainText[i].size()-1; j++){
int character = trialPlainText[i][j] % 97;
int random = rand() % cryptTable[character].size();
wordAsNumbers.push_back(cryptTable[character][random]);
}
encryptedText.push_back(wordAsNumbers);
}
for(size_t i=0; i<encryptedText.size(); i++){
for(size_t j=0; j<encryptedText[i].size(); j++){
cout<<encryptedText[i][j]<<" ";
}
cout<<endl;
}
return encryptedText;
}
vector<double> freqOfLettersInEncText(vector<vector<int>>& encryptedText){
vector<double> encFreqLine;
vector<int> encInLine;
for(size_t i=0; i<encryptedText.size(); i++){
for(size_t j=0; j<encryptedText[i].size(); j++){
encInLine.push_back(encryptedText[i][j]);
}
}
for(size_t i=0; i<103; i++){
int counter = isInThereInt(encInLine, i);
encFreqLine.push_back((counter*1.0)/encInLine.size());
}
return encFreqLine;
}
/*
This is my decryption scheme that attempts to decrypt to the encryptedText.
vector<string>& dict is the dictionary, vector<vector<string>>& dictByLength sorts the dictionary
by length of words. Finally vector of vector ints encrypted Text is each word encrypted.*/
vector<string> decrypt(vector<string>& dict, vector<vector<string>>& dictByLength, vector<vector<int>>& encryptedText){
vector<string> decryptedText;
int minIndex = 0;
vector<double> freqVec = freqOfLetters();
cout<<endl;
vector<double> encFreqLine = freqOfLettersInEncText(encryptedText);
cout<<endl;
for(size_t i=0; i<encryptedText.size(); i++){
vector<string> wordSpecificLength = dictByLength[encryptedText[i].size()];
double min = INT_MAX;
double theoFreqSum = 0;
for(size_t m=0; m<encryptedText[i].size(); m++){
theoFreqSum += encFreqLine[encryptedText[i][m]]*encFreqLine[encryptedText[i][m]];
}
for(size_t j=0; j<wordSpecificLength.size(); j++){
double expFreqSum = 0;
for(size_t k=0; k<wordSpecificLength[j].length()-1; k++){
expFreqSum += freqVec[wordSpecificLength[j][k]%97]*encFreqLine[encryptedText[i][k]];
}
if(min > fabs(expFreqSum - theoFreqSum)){
min = fabs(expFreqSum - theoFreqSum);
minIndex = j;
}
if(wordSpecificLength.size()-1 == j){
decryptedText.push_back(wordSpecificLength[minIndex]);
}
}
}
return decryptedText;
}
/////////////
///MAIN//////
/////////////
int main(){
srand(time(0));
ifstream ifs("dictionary.txt");
if (!ifs) {
cerr << "Could not open the file.\n";
}
//filling up dictionary vector
string line;
vector<string> dictionary;
while (getline(ifs, line)) {
if(!(line.empty())){
dictionary.push_back(line);
}
}
ifs.close();
ifs.clear();
int max = 0;
for(size_t i = 0; i<dictionary.size(); i++){
if((dictionary[i]).length() > max){
max = (dictionary[i]).length();
}
}
cout<<"Max length is: "<<max<<endl;
cout<<"The number of words in the dictionary is: "<<dictionary.size()<<endl;
vector<vector<string>> dictByLength;
//This sorts the word by length.
lengthWordSorter(dictionary,dictByLength, max);
for(size_t i=0; i<dictByLength.size(); i++){
cout<<"The number of "<<(i)<<" letter words are: "<<(dictByLength[i].size())<<endl;
}
//generates the crypt table that will be used for encryption
cout<<"Crypt Table"<<endl;
vector<vector<int>> cryptTable = genCryptTable();
for(size_t i=0; i<cryptTable.size(); i++){
for(size_t j=0; j<cryptTable[i].size(); j++ ){
cout<<cryptTable[i][j]<<" ";
}
cout<<endl;
}
//this is the encrypted text
cout<<"encrypted text"<<endl;
vector<vector<int>> encText = encrypt(dictionary, dictByLength);
vector<int> encInLine;
for(size_t i=0; i<encText.size(); i++){
for(size_t j=0; j<encText[i].size(); j++){
encInLine.push_back(encText[i][j]);
}
}
//This is the statistics of the encrypted text
cout<<endl;
cout<<"Statistics of encrypted text"<<endl;
for(size_t i=0; i<103; i++){
int counter = isInThereInt(encInLine, i);
double percent =((counter*1.0)/encInLine.size())*100.0;
cout<<i<<": "<<counter<<" percent: "<<percent<<endl;
}
//This is the decrypted text coming out. Obviously it doesn't work.
cout<<"Decrypted Text"<<endl;
vector<string> decryptedText = decrypt(dictionary, dictByLength, encText);
for(size_t i=0; i<decryptedText.size(); i++){
cout<<decryptedText[i]<<endl;
}
return 0;
}