-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVigenere.cpp
More file actions
579 lines (481 loc) · 21 KB
/
Vigenere.cpp
File metadata and controls
579 lines (481 loc) · 21 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
// Winter Thomas and Ricardo Romanach
#include "Caesar.h"
#include "Vigenere.h"
#include "DataManipulation.h"
#include <iomanip>
#include <string>
#include <cmath>
/////////////////////////
// Main User Functions //
/////////////////////////
// Solves the vigenere cipher and allows for the user to edit the solution
void Vigenere::solveVigenere(std::string encryptedText){
// Simplify text for calculating period and keys
std::string cleanEncryptedText = Caesar::cleanText(encryptedText);
// Set a max period to check for
int maxPeriodChecked = sqrt(cleanEncryptedText.length()) + 1;
// Calculate the most likely periods
double* periodList = calculateIdealPeriod(cleanEncryptedText, maxPeriodChecked);
// Get most likely period
int period = getHighestValueIndex(periodList, maxPeriodChecked) + 1;
// Calculate the correlation frequencies for the alphabets
double** decodedAlphabetFrequencies = calculateKeys(cleanEncryptedText, period);
// Get most likely key
int* key = calculateMostLikelyKey(decodedAlphabetFrequencies, period);
// Decrypt text
std::string decryptedText;
decryptedText = decodeVigenere(encryptedText, key, period);
int integerInput = 0;
while (integerInput != 7){
std::cout << "\nPeriod:\t" << period;
std::cout << "\nKey:\t" << convertKeyToString(key, period);
std::cout << "\nText:\t" << decryptedText;
std::cout << "\n\n1. Change to next most likely period";
std::cout << "\n2. Set period";
std::cout << "\n3. Change to next most likely alphabet";
std::cout << "\n4. Set an alphabet";
std::cout << "\n5. Set the key";
std::cout << "\n6. Output to a file";
std::cout << "\n7. Quit\n";
integerInput = DataManipulation::getIntegerInput(1, 7);
// Both of these variables are used rarely in the switch case
int tempInput;
std::string tempString;
switch (integerInput){
case 1:
// Remove the current period from the list of most likely periods
if (period <= maxPeriodChecked){
periodList[period - 1] = -1;
}
// Get the new period
period = getHighestValueIndex(periodList, maxPeriodChecked) + 1;
// Recalculate all values that change from a new period
decodedAlphabetFrequencies = calculateKeys(cleanEncryptedText, period);
key = calculateMostLikelyKey(decodedAlphabetFrequencies, period);
decryptedText = decodeVigenere(encryptedText, key, period);
break;
case 2:
// Remove the current period from the list of most likely periods
if (period <= maxPeriodChecked){
periodList[period - 1] = -1;
}
// Get the new period
std::cout << "\nPlease enter a period";
std::cout << "\nMax value is " << cleanEncryptedText.length() / 2 << "\n";
period = DataManipulation::getIntegerInput(1, cleanEncryptedText.length() / 2);
// Recalculate all values that change from a new period
decodedAlphabetFrequencies = calculateKeys(cleanEncryptedText, period);
key = calculateMostLikelyKey(decodedAlphabetFrequencies, period);
decryptedText = decodeVigenere(encryptedText, key, period);
break;
case 3:
std::cout << "\n";
printLettersInBox(convertKeyToString(key, period));
std::cout << "\n" << combineTextWithKey(decryptedText, convertKeyToString(key, period));
// Get which alphabet to change
std::cout << "Input which alphabet to change:\n";
tempInput = DataManipulation::getIntegerInput(0, period);
// Remove the old alphabet's key from the list of most likely keys
decodedAlphabetFrequencies[tempInput][key[tempInput]] = -1;
// Get the new most likely key
key[tempInput] = getHighestValueIndex(decodedAlphabetFrequencies[tempInput], LANGUAGE_LETTER_COUNT);
// Decrypt the text with they new key
decryptedText = decodeVigenere(encryptedText, key, period);
break;
case 4:
std::cout << "\n";
printLettersInBox(convertKeyToString(key, period));
std::cout << "\n" << combineTextWithKey(decryptedText, convertKeyToString(key, period));
// Get which alphabet to change
std::cout << "Input which alphabet to change:\n";
tempInput = DataManipulation::getIntegerInput(0, period);
// Remove the old alphabet's key from the list of most likely keys
decodedAlphabetFrequencies[tempInput][key[tempInput]] = -1;
// Get the new key
std::cout << "\nChange the key for the alphabet to:\n";
key[tempInput] = Caesar::convertLetterToNumber(DataManipulation::getLetterInput());
// Decrypt the text with they new key
decryptedText = decodeVigenere(encryptedText, key, period);
break;
case 5:
// Get the new key
tempString = getKeyUserInput(cleanEncryptedText.length() / 2);
key = convertStringToKey(tempString);
// Recalculate everything to account for the new key
for (int i = 0; i < period; i++){
delete[] decodedAlphabetFrequencies[i];
}
delete[] decodedAlphabetFrequencies;
period = tempString.length();
decodedAlphabetFrequencies = calculateKeys(cleanEncryptedText, period);
decryptedText = decodeVigenere(encryptedText, key, period);
break;
case 6:
std::cout << "\nWhat file would you like to output to?\n> ";
getline(std::cin, tempString);
DataManipulation::writeStringToFile(tempString, "Period: " + std::to_string(period) + "\nKey: " + convertKeyToString(key, period) + "\nDecrypted Text:\n" + decryptedText);
break;
}
}
// Delete arrays
delete[] periodList;
delete[] key;
for (int i = 0; i < period; i++){
delete[] decodedAlphabetFrequencies[i];
}
delete[] decodedAlphabetFrequencies;
}
// Does all steps for decrypting a vigenere cypher automatically
// hasExtraInfo also returns the period and the key in the returned string
std::string Vigenere::solveMostLikelyVigenere(std::string encryptedText, bool hasExtraInfo){
// Simplify text for calculating period and keys
std::string cleanEncryptedText = Caesar::cleanText(encryptedText);
// Set a max period to check for
int maxPeriodChecked = sqrt(cleanEncryptedText.length()) + 1;
// Calculate the most likely periods
double* periodList = calculateIdealPeriod(cleanEncryptedText, maxPeriodChecked);
// Get most likely period
int period = getHighestValueIndex(periodList, maxPeriodChecked) + 1;
// Calculate the correlation frequencies for the alphabets
double** decodedAlphabetFrequencies = calculateKeys(cleanEncryptedText, period);
// Get most likely key
int* key = calculateMostLikelyKey(decodedAlphabetFrequencies, period);
// Decrypt text
std::string decryptedText = "";
if (hasExtraInfo){
decryptedText += "\nYour period is calculated to be " + std::to_string(period) + "\n\n";
decryptedText += "Your key is " + convertKeyToString(key, period) + "\n\n";
decryptedText += "Decrypted Text:\n";
}
decryptedText += decodeVigenere(encryptedText, key, period);
// Delete arrays
delete[] periodList;
delete[] key;
for (int i = 0; i < period; i++){
delete[] decodedAlphabetFrequencies[i];
}
delete[] decodedAlphabetFrequencies;
return decryptedText;
}
// Prints the result of solveVigenere with extra info
void Vigenere::printMostLikelyVigenere(std::string encryptedText){
std::cout << solveMostLikelyVigenere(encryptedText, true);
}
std::string Vigenere::encrypt(){
std::string text;
std::string key;
text = DataManipulation::getUserInput("Please enter the text to encrypt:");
key = getKeyUserInput(text.length());
return encrypt(text, key);
}
std::string Vigenere::encrypt(std::string text, std::string key){
int period = key.length();
std::string encryptedText = "";
int currentAlphabet = 0;
int* keyArr = new int[period];
keyArr = convertStringToKey(key);
for (char character : text){
// If the character isn't a letter, just save the character and continue
if (!isalpha(character)){
encryptedText += character;
continue;
}
// Check if lowercase letter
if (islower(character)){
character += keyArr[currentAlphabet];
// Check for underflow
if (!islower(character)){
character -= LANGUAGE_LETTER_COUNT;
}
}
// Check if uppercase letter
else {
character += keyArr[currentAlphabet];
// Check for underflow
if (!isupper(character)){
character -= LANGUAGE_LETTER_COUNT;
}
}
encryptedText += character;
// Increment which alphabet is currently being used
currentAlphabet++;
if (currentAlphabet == period){
currentAlphabet = 0;
}
}
delete[] keyArr;
return encryptedText;
}
///////////////////////////////
// Core Decryption Functions //
///////////////////////////////
// Determines the most likely period using Index of Correlation (IC) Values
double* Vigenere::calculateIdealPeriod(std::string cleanEncryptedText, int maxPeriodChecked){
int numberOfAlphabetsSuggestingPeriodOfOne;
std::string* alphabets;
double* periodsList = new double[maxPeriodChecked];
for (int periodBeingChecked = 1; periodBeingChecked <= maxPeriodChecked; periodBeingChecked++){
alphabets = splitAlphabets(cleanEncryptedText, periodBeingChecked);
numberOfAlphabetsSuggestingPeriodOfOne = 0;
// Counts the number of split alphabets that suggests a period of 1
for (int i = 0; i < periodBeingChecked; i++){
if (ICCalculator(alphabets[i]) > VALUE_SUGGESTING_PERIOD_OF_ONE){
numberOfAlphabetsSuggestingPeriodOfOne++;
}
}
delete[] alphabets;
periodsList[periodBeingChecked - 1] = (double) numberOfAlphabetsSuggestingPeriodOfOne / periodBeingChecked;
}
return periodsList;
}
// Returns a 2D array that has for each alphabet (number of alphabets = period)
// an array that contains the corerelation freqeuncy for each possible shift
double** Vigenere::calculateKeys(std::string cleanEncryptedText, int period){
double** decodedAlphabetFrequencies = new double*[period];
std::string* alphabets = splitAlphabets(cleanEncryptedText, period);
for (int i = 0; i < period; i++){
decodedAlphabetFrequencies[i] = Caesar::getCorrelationOfFrequencies(alphabets[i]);
}
delete[] alphabets;
return decodedAlphabetFrequencies;
}
// Returns the most likely key from the information gathered from the alphabet frequencies
int* Vigenere::calculateMostLikelyKey(double** decodedAlphabetFrequencies, int period){
int* key = new int[period];
for (int i = 0; i < period; i++){
key[i] = getHighestValueIndex(decodedAlphabetFrequencies[i], LANGUAGE_LETTER_COUNT);
}
return key;
}
// decrypts the provided string based off the period and key supplied to it
std::string Vigenere::decodeVigenere(std::string encryptedText, int* key, int period){
std::string decryptedText = "";
int currentAlphabet = 0;
for (char character : encryptedText){
// If the character isn't a letter, just save the character and continue
if (!isalpha(character)){
decryptedText += character;
continue;
}
// Check if lowercase letter
if (islower(character)){
character -= key[currentAlphabet];
// Check for underflow
if (!islower(character)){
character += LANGUAGE_LETTER_COUNT;
}
}
// Check if uppercase letter
else {
character -= key[currentAlphabet];
// Check for underflow
if (!isupper(character)){
character += LANGUAGE_LETTER_COUNT;
}
}
decryptedText += character;
// Increment which alphabet is currently being used
currentAlphabet++;
if (currentAlphabet == period){
currentAlphabet = 0;
}
}
return decryptedText;
}
//////////////////////
// Helper Functions //
//////////////////////
// Index of Coincidence (IC) Calculator
// This is used to calculate if the period of a key is valid. If this returns a value greater than 0.06,
// then it is more likely that the period of the alphabet is one. If this is true for the majority of
// the alphabets in a Vigenere cipher, then you have most likely selected a valid period length.
double Vigenere::ICCalculator(std::string text){
int* freq = Caesar::getFrequencyOfLetters(text);
int totalLetters = 0;
for (int i = 0; i < LANGUAGE_LETTER_COUNT; i++){
totalLetters += freq[i];
}
double total = 0;
for (int i = 0; i < LANGUAGE_LETTER_COUNT; i++){
total += freq[i] * (freq[i] - 1);
}
total *= 1 / (double) (totalLetters * (totalLetters - 1));
return total;
}
// Splits the encrypted text into a number of alphabets equal to the calculated period and returns an array of the alphabets
std::string* Vigenere::splitAlphabets(std::string cleanEncryptedText, int period){
std::string* alphabets = new std::string[period];
int periodTracker = 0;
for (int i = 0; i < period; i++){
alphabets[i] = "";
}
for (char i : cleanEncryptedText){
alphabets[periodTracker] += i;
periodTracker++;
//Resets counter if exceed period
periodTracker %= period;
}
return alphabets;
}
// Returns the index of the highest value in an array
int Vigenere::getHighestValueIndex(double* listOfValues, int sizeOfList){
int highestValueIndex = 0;
double highestValue = listOfValues[0];;
for (int i = 0; i < sizeOfList; i++){
if (listOfValues[i] > highestValue){
highestValue = listOfValues[i];
highestValueIndex = i;
}
}
return highestValueIndex;
}
// Given a specified string and some internal settings, this function will print the individual letters of
// that string inside a box with its index on top of it.
void Vigenere::printLettersInBox(std::string text) {
// Character per line is set to 19 unless its shorter than the length of the text, at which its set to the text's length
const int CHARACTERS_PER_ROW = text.length() > 19 ? 19 : text.length();
const int SPACING = 5;
const int BOX_WIDTH = CHARACTERS_PER_ROW * SPACING + 2;
int textLength = text.size();
//Assuming each row of letters has CHARACTERS_PER_ROW characters with two spaces on each side
int rows = ceil((float)textLength / CHARACTERS_PER_ROW);
std::cout << std::setfill('-') << std::setw(BOX_WIDTH + SPACING - 1) << '-' << "\n";
int charactersPrinted = 0;
int numbersPrinted = 0;
for (int i = 0; i < rows; i++){
std::cout << std::setfill(' ') << "|" << std::setw(BOX_WIDTH + SPACING - 2) << "|" << "\n";
std::cout << "|";
for (int j = 0; j < CHARACTERS_PER_ROW; j++){
int current = j + (CHARACTERS_PER_ROW * i);
if (current < textLength){
std::cout << std::setw(SPACING) << current;
numbersPrinted++;
}
}
// This line takes care of the last "|" to be printed for each row. If a row is full, this should only add SPACING
// number of spaces. If the last line is not full, then it will figure out how many spaces need to go before the "|".
std::cout << std::setw(((CHARACTERS_PER_ROW - (numbersPrinted % CHARACTERS_PER_ROW)) % CHARACTERS_PER_ROW) * SPACING + SPACING) << "|" << "\n";
std::cout << "|";
for (int j = 0; j < CHARACTERS_PER_ROW; j++){
if (charactersPrinted < textLength){
std::cout << std::setw(SPACING) << text.at(j + (CHARACTERS_PER_ROW * i));
charactersPrinted++;
}
}
std::cout << std::setw(((CHARACTERS_PER_ROW - (charactersPrinted % CHARACTERS_PER_ROW)) % CHARACTERS_PER_ROW) * SPACING + SPACING) << "|" << "\n";
}
std::cout << "|" << std::setw(BOX_WIDTH + SPACING - 2) << "|" << "\n";
std::cout << std::setfill('-') << std::setw(BOX_WIDTH + SPACING - 1) << '-' << "\n";
}
// Takes the decrypted text and its key and puts them together so that it is visible
// which part of the key goes to which part of the text
std::string Vigenere::combineTextWithKey(std::string text, std::string key){
const int CHARACTERS_PER_ROW = 101;
int totalRows = ceil((double) text.length() / CHARACTERS_PER_ROW);
int lastCharacterColumn = text.length() % CHARACTERS_PER_ROW;
std::string finalText = "";
std::string textPart;
bool isLetter[CHARACTERS_PER_ROW];
char character;
int keyLength = key.length();
int keyPos = 0;
// For each row of text that will be in the output
for (int currentRow = 0; currentRow < totalRows; currentRow++){
textPart = "";
// For each column in that row
for (int currentColumn = 0; currentColumn < CHARACTERS_PER_ROW; currentColumn++){
// Get the character at that spot
character = text[currentRow * CHARACTERS_PER_ROW + currentColumn];
// save if its a letter or not
isLetter[currentColumn] = isalpha(character);
// And add the character to the text part
textPart += character;
// If last character stop
if (lastCharacterColumn == currentColumn && currentRow + 1 == totalRows){
break;
}
}
// For each column in the row
for (int currentColumn = 0; currentColumn < CHARACTERS_PER_ROW; currentColumn++){
// If that column is corresponding to a letter
if (isLetter[currentColumn]){
// Add the key part to the text
finalText += key[keyPos];
keyPos++;
if (keyPos == keyLength){
keyPos = 0;
}
}
// Otherwise put a space
else {
finalText += " ";
}
// If last character stop
if (lastCharacterColumn == currentColumn && currentRow + 1 == totalRows){
break;
}
}
// Add the text part to the end of the key part with new lines so that it looks like
// each key part lines up with its text part
finalText += "\n" + textPart + "\n\n";
}
return finalText;
}
// Asks the user for a key, validates it, and converts it to a string of all capital letters
std::string Vigenere::getKeyUserInput(int maxKeyLength){
std::string userInput;
bool hasBadInput;
while (true){
std::cout << "\nPlease enter a key:\n> ";
getline(std::cin, userInput);
// Make sure the key is an appropriate length
if ((int) userInput.length() > maxKeyLength){
std::cout << "\nMax key length is " << maxKeyLength << "\n";
continue;
}
// Make sure the user input something
if (userInput.empty()){
std::cout << "\n";
continue;
}
hasBadInput = false;
// Convert key to a string while validating it is only letters
for (int i = 0; i < (int) userInput.length(); i++){
// If lowercase letter
if (islower(userInput[i])){
userInput[i] = toupper(userInput[i]);
continue;
}
// If uppercase letter
if (isupper(userInput[i])){
continue;
}
// If not a letter
hasBadInput = true;
std::cout << "\nThe key can only have letters in it.\n";
break;
}
// If the key is complete
if (!hasBadInput){
return userInput;
}
// Otherwise ask for a new key
}
}
// Takes the key as an integer array and returns it as a string of letters
std::string Vigenere::convertKeyToString(int* key, int period){
std::string stringKey = "";
for (int i = 0; i < period; i++){
stringKey += Caesar::convertNumberToLetter(key[i]);
}
return stringKey;
}
// converts a key from a string to an integer array
int* Vigenere::convertStringToKey(std::string stringKey){
int keyLength = stringKey.length();
int* key = new int[keyLength];
for (int i = 0; i < keyLength; i++){
key[i] = Caesar::convertLetterToNumber(stringKey[i]);
}
return key;
}