Question: Please help me to rewrite following code in different way: #include #include #include using namespace std; string encrypt ( string , string ) ; string

Please help me to rewrite following code in different way: #include
#include
#include
using namespace std;
string encrypt(string, string);
string decrypt(string, string);
void encryptFile(string, string, string);
void decryptFile(string, string, string);
int main(){
int s;
string inputFile, outputFile, key;
cout << "Encrypt a file(1)/Decrypt a file(2)"<< endl;
cout << "Enter your selection: ";
cin >> s;
while (s !=1 && s !=2){
cout << "Invalid selection. Please enter 1 to encrypt or 2 to decrypt: ";
cin >> s;
}
cout << "Enter name for input file: ";
cin >> inputFile;
cout << "Enter name for output file: ";
cin >> outputFile;
// Any word is fine but no numbers only intergers and it has to be same for encypt and decrypt
cout << "Enter a string key to execute it: ";
cin >> key;
if (s ==1){
encryptFile(inputFile, outputFile, key);
cout << "Encrypted" << endl;
} else if (s ==2){
decryptFile(inputFile, outputFile, key);
cout << "Decrypted" << endl;
}
return 0;
}
// Vigenre cipher encryption function
string encrypt(string plaintext, string key){
string encryptedText ="";
int keyIndex =0;
int keyLength = key.length();
// Loop through each character in the plaintext
for (char &c : plaintext){
if (isalpha(c)){// Encrypt only alphabetic characters
char base = islower(c)?'a' : 'A'; // Determine case (lower or upper)
int shift = tolower(key[keyIndex % keyLength])-'a'; // Get the shift amount
encryptedText +=(c - base + shift)%26+ base; // Apply the shift
keyIndex++; // Only increment keyIndex for alphabetic characters
} else {
// Non-alphabetic characters are added as-is
encryptedText += c;
}
}
return encryptedText;
}
// Vigenre cipher decryption function
string decrypt(string ciphertext, string key){
string decryptedText ="";
int keyIndex =0;
int keyLength = key.length();
// Loop through each character in the ciphertext
for (char &c : ciphertext){
if (isalpha(c)){// Decrypt only alphabetic characters
char base = islower(c)?'a' : 'A'; // Determine case (lower or upper)
int shift = tolower(key[keyIndex % keyLength])-'a'; // Get the shift amount
decryptedText +=(c - base - shift +26)%26+ base; // Apply the reverse shift
keyIndex++; // Only increment keyIndex for alphabetic characters
} else {
// Non-alphabetic characters are added as-is
decryptedText += c;
}
}
return decryptedText;
}
// Encrypt the file using Vigenre cipher
void encryptFile(string inputFileName, string outputFileName, string key){
ifstream fin(inputFileName);
ofstream fout(outputFileName);
string line;
if (!fin.is_open()){
cout << "Error: Unable to open input file!" << endl;
return;
}
if (!fout.is_open()){
cout << "Error: Unable to open output file!" << endl;
return;
}
// Encrypt each line and write it to the output file
while (getline(fin, line)){
fout << encrypt(line, key)<< endl;
}
fin.close();
fout.close();
}
// Decrypt the file using Vigenre cipher
void decryptFile(string inputFileName, string outputFileName, string key){
ifstream fin(inputFileName);
ofstream fout(outputFileName);
string line;
if (!fin.is_open()){
cout << "Error: Unable to open input file!" << endl;
return;
}
if (!fout.is_open()){
cout << "Error: Unable to open output file!" << endl;
return;
}
// Decrypt each line and write it to the output file
while (getline(fin, line)){
fout << decrypt(line, key)<< endl;
}
fin.close();
fout.close();
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!