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 encryptstring string;
string decryptstring string;
void encryptFilestring string, string;
void decryptFilestring string, string;
int main
int s;
string inputFile, outputFile, key;
cout "Encrypt a fileDecrypt a file endl;
cout "Enter your selection: ;
cin s;
while s && s
cout "Invalid selection. Please enter to encrypt or 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
encryptFileinputFile outputFile, key;
cout "Encrypted" endl;
else if s
decryptFileinputFile outputFile, key;
cout "Decrypted" endl;
return ;
Vigenre cipher encryption function
string encryptstring plaintext, string key
string encryptedText ;
int keyIndex ;
int keyLength key.length;
Loop through each character in the plaintext
for char &c : plaintext
if isalphac Encrypt only alphabetic characters
char base islowerca : A; Determine case lower or upper
int shift tolowerkeykeyIndex keyLengtha; Get the shift amount
encryptedText c base shift base; Apply the shift
keyIndex; Only increment keyIndex for alphabetic characters
else
Nonalphabetic characters are added asis
encryptedText c;
return encryptedText;
Vigenre cipher decryption function
string decryptstring ciphertext, string key
string decryptedText ;
int keyIndex ;
int keyLength key.length;
Loop through each character in the ciphertext
for char &c : ciphertext
if isalphac Decrypt only alphabetic characters
char base islowerca : A; Determine case lower or upper
int shift tolowerkeykeyIndex keyLengtha; Get the shift amount
decryptedText c base shift base; Apply the reverse shift
keyIndex; Only increment keyIndex for alphabetic characters
else
Nonalphabetic characters are added asis
decryptedText c;
return decryptedText;
Encrypt the file using Vigenre cipher
void encryptFilestring inputFileName, string outputFileName, string key
ifstream fininputFileName;
ofstream foutoutputFileName;
string line;
if fin.isopen
cout "Error: Unable to open input file!" endl;
return;
if fout.isopen
cout "Error: Unable to open output file!" endl;
return;
Encrypt each line and write it to the output file
while getlinefin line
fout encryptline key endl;
fin.close;
fout.close;
Decrypt the file using Vigenre cipher
void decryptFilestring inputFileName, string outputFileName, string key
ifstream fininputFileName;
ofstream foutoutputFileName;
string line;
if fin.isopen
cout "Error: Unable to open input file!" endl;
return;
if fout.isopen
cout "Error: Unable to open output file!" endl;
return;
Decrypt each line and write it to the output file
while getlinefin line
fout decryptline key endl;
fin.close;
fout.close;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
