Hey all!
I was going over this code to read a file on my desktop that decrypts a cesar cipher code and I am stuck trying to figure out how the shift is calculated in this program.
From what I can tell, Max e's is the shifted letter with the highest frequency. Since e's are the most common letter in English, the program is trying to set the highest frequency char in the cipher, to an 'e' in English. Which is good, as far as it goes, but there are many phrases where e's are NOT the most frequent letter, and then it will fall on it's face.
So, how can I tell the program to guess at the most frequent cipher letter to be an e in plain text BUT in case it isn't, then proceed to try an e shifted to the second most frequent letter in the text, and so on, until I find it?
A friend helped with that part but has poor English so it is difficult for him to explain it to me. Can someone please elaborate? Assistance is greatly appreciated! Let me know what you think:
I was going over this code to read a file on my desktop that decrypts a cesar cipher code and I am stuck trying to figure out how the shift is calculated in this program.
From what I can tell, Max e's is the shifted letter with the highest frequency. Since e's are the most common letter in English, the program is trying to set the highest frequency char in the cipher, to an 'e' in English. Which is good, as far as it goes, but there are many phrases where e's are NOT the most frequent letter, and then it will fall on it's face.
So, how can I tell the program to guess at the most frequent cipher letter to be an e in plain text BUT in case it isn't, then proceed to try an e shifted to the second most frequent letter in the text, and so on, until I find it?
A friend helped with that part but has poor English so it is difficult for him to explain it to me. Can someone please elaborate? Assistance is greatly appreciated! Let me know what you think:
Code:
#include <iostream> #include <string> #include <cctype> // isalpha, islower, isupper, functions #include <fstream> using namespace std; string caesarShift(string text, int shift); int main() { int maxEs = 0; // # of e's in maxString int currentEs = 0; // # of e'sin currentString string maxString; // decrypted caesar shift with most e's string currentString; //decrypted caesar shift string cipher; // Stores cipher text char ch; // Stores currentcharacter for reading ifstream fin("/Users/jasonrodriguez/Desktop/encrypted.txt"); //opens "encrypted.txt" file while( fin.get(ch) ) // readseach char into the cipher till EOF { cipher += ch; } fin.close(); // be safe andclose file for(int i=0; i < 26; i++) { currentEs =0; // Reset counter currentString =caesarShift(cipher, i); // get shifted text for(unsigned int x=0; x <currentString.size(); x++) // check each character of stringarray { if(currentString[x] == 'e' || currentString[x] == 'E') // check fore's { currentEs++; // increment Ecounter } } if(currentEs > maxEs) //if currentEs is greater than maxEs, replace max with current { maxEs =currentEs; maxString= currentString; } } cout << maxString << endl; return 0; } /** string caesarShift(string text, int shift) Decrypts Caesar Shift using text and shift */ string caesarShift(string text, int shift) { shift = shift % 26; // Morethan 26 is redundant and unneeded char ch = 0; // holds current character char chs = 0; // holds shiftedcharacter for(unsigned int i=0; i < text.size();i++) { ch = text[i]; if( isalpha(ch) ) { chs = ch -shift; // reverse shifting if( (islower(ch) && chs < 'a' ) // If is lowercase andshifted value is lower than 'a' || ( isupper(ch) && chs < 'A' ) ) // Ifis uppercase and shifted value is lower than 'A' { chs += 26; // Add 26(number ofletters) to get back to the correct place in alphabet } text[i] =chs; // Set character to shifted character } } return text; }
Comment