Question
Write a complete program in C++ that performs encryption/decryption of a text file. Use the rot method and cryptogram method, the program should ask the
Write a complete program in C++ that performs encryption/decryption of a text file. Use the "rot" method and "cryptogram" method, the program should ask the user to select which method that the user want to use.
For the "rot" method the program should ask the user for the key.
For the"cryptogram" method use the following key
I have the code but when running it on putty terminal I receive an error message, error provided below as well as code, please help fixing this problem.
// code
#include
using namespace std;
class Driver
{
private: string keyFile;
vector
Driver(){};
string filetoString(string filename); vector
string Driver::filetoString(string filename)
{
ifstream fin;
//Open a file and save it to a string.
fin.open(filename.data());
string word;
if(!fin)
{
cout
exit(1);
}
else
{
//Save data to string keyFile
while(!fin.eof())
keyFile.push_back(fin.get());
fin.close();
}
return keyFile;
}
vector
{
ifstream fin;
//Open a file and save it to a vector.
fin.open(filename.data());
string word;
if(!fin)
{
cout
exit(1);
}
else
{
//Save data to vector
while(fin>>word)
originalFile.push_back(word);
fin.close();
}
return originalFile;
}
void Driver::vectortoFile(const vector
{
ofstream fout;
//Read a vector and save it to a file.
fout.open(filename.data());
string word;
if(!fout)
{
cout
exit(1);
}
else
{
//Save data to file
for(int li=0; li { word = V[li]; fout } fout.close(); } return; } void Driver::displayVector(const vector { //Output vector V for confirmation. for(int li=0; li cout cout } //Encrypt using the ROT method. void Driver::EncryptROT() { //Declare and initialize variables. int rot = 0; string filename = "original.txt"; string save = "encrypt01.txt"; cout cin >> rot; cout //Load file from user input into vector V. vector //Declare vector encVec to store encrypted text. vector //Loop to read a single element and store it into string word. for(int li=0; li { string word = V[li]; //Loop to read each element of string word. for(int lj=0; lj { int value = word[lj]; //Encrypt lowercase. if(value>96 && value { value += rot; if(value>122) word[lj] = value - 26; else word[lj] = value; } //Encrypt uppercase. else if(value>64 && value { value += rot; if(value>90) word[lj] = value - 26; else word[lj] = value; } //If value is not a letter, then skip. else { word[lj] = value; } } //Store encrypted word in string encryptWord. string encryptWord = word; //Store string encryptWord in vector encVec. encVec.push_back(encryptWord); } //Save encVec to file "encrypt01.txt". vectortoFile(encVec, save); //Output vector decVec for confirmation. displayVector(encVec); system("pause"); return; } //Decrypt using the ROT method. void Driver::DecryptROT() { //Declare and initialize variables. int rot = 0; string filename = "encrypt01.txt"; string save = "decrypt01.txt"; //Ask the user for input. cout cin >> rot; cout //Load file "encrypt01.txt" into vector V. vector //Declare vector decVec to store decrypted text. vector //Loop to read a single element and store it into string word. for(int li=0; li { string word = V[li]; //Loop to read each element of string word. for(int lj=0; lj { int value = word[lj]; //Decrypt lowercase. if(value>96 && value { value -= rot; if(value word[lj] = value + 26; else word[lj] = value; } //Decrypt uppercase. else if(value>64 && value { value -= rot; if(value word[lj] = value + 26; else word[lj] = value; } //If value is not a letter, then skip. else { word[lj] = value; } } //Store decrypted word in string decryptWord. string decryptWord = word; //Store string decryptWord in vector decVec. decVec.push_back(decryptWord); } //Save decVec to file "decrypt01.txt". vectortoFile(decVec, save); //Output vector decVec for confirmation. displayVector(decVec); system("pause"); return; } //Encrypt using the cryptogram method. void Driver::EncryptCrypto() { cout //Declare and initialize variables. string filename = "original.txt"; string key = "key.txt"; string save = "encrypt02.txt"; //Load the cryptogram into string key2. string key2 = filetoString(key); //Load file from user input into vector V. vector //Declare vector encVec to store encrypted text. vector //Loop to read a single element and store it into string word. for(int li=0; li { string word = V[li]; //Loop to read each element of string word. for(int lj=0; lj { int value = word[lj]; //Encrypt lowercase. if(value>96 && value { char value2 = key2.at(value - 97); word[lj] = value2; } //Encrypt uppercase. else if(value>64 && value { char value2 = key2.at(value - 65); word[lj] = value2 - 32; } //If value is not a letter, then skip. else { word[lj] = value; } } //Store encrypted word in string encryptWord. string encryptWord = word; //Store string encryptWord in vector encVec. encVec.push_back(encryptWord); } //Save encVec to file "encrypt02.txt". vectortoFile(encVec, save); //Output vector decVec for confirmation. displayVector(encVec); system("pause"); return; } //Decrypt using the cryptogram method. void Driver::DecryptCrypto() { cout //Declare and initialize variables. string filename = "encrypt02.txt"; string key = "key.txt"; string save = "decrypt02.txt"; //Load the cryptogram into string key2. string key2 = filetoString(key); //Load a file into vector V and save it to vector decVec. vector //Declare vector decVec to store decrypted text. vector //Loop to read a single element and store it into string word. for(int li=0; li { string word = V[li]; //Loop to read each element of string word. for(int lj=0; lj { int value = word[lj]; //Decrypt lowercase. if(value>96 && value { char value = word[lj]; int value2 = key2.find(value, 0); word[lj] = value2 + 97; } //Decrypt uppercase. else if(value>64 && value { char value = word[lj] + 32; int value2 = key2.find(value, 0); word[lj] = value2 + 65; } //If value is not a letter, then skip. else { word[lj] = value; } } //Store decrypted word in string decryptWord. string decryptWord = word; //Store string decryptWord in vector decVec. decVec.push_back(decryptWord); } //Save decVec to a file. vectortoFile(decVec, save); //Output vector decVec for confirmation. displayVector(decVec); system("pause"); return; } //Declare functions. void menuLoop(); void menuEncrypt(); void menuDecrypt(); int main() { //Begin the program loop menuLoop(); cout system("pause"); return 0; } void menuLoop() { //Declare a variable for exiting the program. bool exit = false; //Initialize a variable for user input. int choice = 0; //Begin menu selection loop. while(exit!=true) { //Display user options. cout //Get user input. cin >> choice; //Switch for encryption or decryption. switch(choice) { case 1: { menuEncrypt(); break; } case 2: { menuDecrypt(); break; } case 3: { exit = true; break; } default: { cout break; } } } return; } //Submenu for performing encryptions. void menuEncrypt() { //Instantiate an object. Driver Encrypt; int choice = 0; //Display user options. cout cin >> choice; //Switch for ROT or Cryptogram encryption method switch(choice) { case 1: { Encrypt.EncryptROT(); break; } case 2: { Encrypt.EncryptCrypto(); break; } case 3: { break; } default: { cout break; } } return; } void menuDecrypt() { //Instantiate an object. Driver Decrypt; int choice = 0; //Display user options. cout //Get user input. cin >> choice; //Switch for ROT or Cryptogram decryption method. switch(choice) { case 1: { Decrypt.DecryptROT(); break; } case 2: { Decrypt.DecryptCrypto(); break; } case 3: { break; } default: { cout break; } } //Pause console window system("pause"); return; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started