Question: I'm working on a Vigenere cipher C++ program and it's not properly encoding and decoding multiple paragraphs of text from a file. How would I
I'm working on a Vigenere cipher C++ program and it's not properly encoding and decoding multiple paragraphs of text from a file. How would I fix this? It reads the first string of text perfectly, but then the next lines are incorrect.
#include
using namespace std;
class Vigenere{ public: Vigenere(string& key); string encrypt(string& line, string& key); string decrypt(string& line, string& key);
private: string key; char shift(char, int); };
Vigenere::Vigenere(string& thekey) { key = thekey; }
// precondition: plaintext is UPPERCASE letters only string Vigenere::encrypt(string& plaintext, string& key){
int a = 0; string ciphertext=""; int b, c; char ch;
for(unsigned int i = 0; i < plaintext.size(); i++) { b = toupper(plaintext[i]) - 'A'; if(b >= 0 && b < 26) { c = key[a] - 'A'; ch = 'A' + ( b + c) % 26; a = (a + 1) % key.length(); ciphertext += ch;
} }
return ciphertext; }
// precondition: plaintext is UPPERcASE letters only string Vigenere::decrypt(string& ciphertext, string& key){ int a = 0; string decoded = ""; int b, c; char ch; for(unsigned int i = 0; i < ciphertext.size(); i++) { b = toupper(ciphertext[i]) - 'A'; if(b >= 0 && b < 26) { c = key[a] - 'A'; ch = 'A' + ( b - c + 26) % 26; decoded += ch; a = (a + 1) % key.length(); } } return decoded; }
char Vigenere::shift(char c, int key){ int result = (c-'A'+key); if (result >= 26) result = result - 26; if (result < 0) result = result + 26;
return result+'A';
}
int main(int argc, char *argv[]){
if (argc != 3){ cerr << "USAGE: " << argv[0] << " -d|e key" << endl; exit(1); }
string key = string(argv[2]); // OUR KEYWORD
string keyword = ""; for(unsigned int i = 0; i < key.size(); i++) keyword += toupper(key[i]); key = keyword;
bool encrypt; string option(argv[1]); string line; string encodedLine;
if (option == "-e"){ encrypt = true; } else { encrypt = false; }
Vigenere cipher(key);
while (getline(cin, line)) {
if (encrypt){ encodedLine = cipher.encrypt(line, key); } else { encodedLine = cipher.decrypt(line, key); } cout << encodedLine << endl; } return 0; }
For example, if the following is read in as a text file, with keyword babington:
Myself with ten gentlemen and a hundred of our followers will undertake the delivery of your royal person from the hands of your enemies. For the dispatch of the usurper, from the obedience of whom we are by the excommunication of her made free, there be six noble gentlemen, all my private friends, who for the zeal they bear to the Catholic cause and your Majesty's service will undertake that tragical execution.
It should return the following when encoded, with keyword babington:
NYTMYLPWGITFVTKGHYFMFVNTWOUVNEZRJHTBVRGWYRHKRSS XQYRNBQFRUIXKMVREEMQIKKMBGYPCEXHMNMPFZFUGTEPMUPR NTBQTOGGBAKSAFMJMFLHFGIEEQFVTHPIOGBUKNGHSPFZSXHA GIEPJRJBSADEPNJNHAJFASMOEMVRFXDWZSNBVDAUQBTHTUFR NIQKYFRFTIMEKUSFJXOWORXUROTMMZKGOYMMZXEOOOGF FSQRTWGJIOGWEZASMFAMBUKRPRBRUWGNXQNUHPTVIVOHTE BVQEHIENAKMFZRGFFRWQPKPWYMUOLRXMOXFTIIGZKOTJCBT RDXQHUIPV
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
