Question: Using C++ #include #include using namespace std ; // get the ciphered text string encrypt ( string str , int ); // decrypt the cipher
Using C++
#include
#include
using namespace std;
// get the ciphered text
string encrypt(string str, int);
// decrypt the cipher text
string decrypt(string str, int);
int main()
{
printf("Enter shift +/- 26: ");
int n;
cin>>n;
printf(" Enter plaintext message (A-Z only, no spaces): ");
string str;
cin>>str;
// get the ciphered text
string cipher = encrypt(str, n);
// decrypt the cipher text
string plaintext = decrypt(cipher, -n);
cout<<" ciphertext: "<<cipher<<" plaintext: "<<plaintext;
return 0;
}
// get the ciphered text
string encrypt(string str, int n)
{
int i;
for( i = 0 ; i < str.length() ; i++ )
{
// get the ascii code
int ascii = (int)str[i];
ascii = ascii + n;
if(ascii < 65)
ascii = 91 - ( 65 - ascii );
else if(ascii > 90)
ascii = 64 + (ascii - 90);
// update the character with the ciphered text
str[i] = (char)ascii;
}
return str;
}
// decrypt the cipher text
string decrypt(string str, int n)
{
int i;
for( i = 0 ; i < str.length() ; i++ )
{
// get the ascii code
int ascii = (int)str[i];
ascii = ascii + n;
if(ascii < 65)
ascii = 91 - ( 65 - ascii );
else if(ascii > 90)
ascii = 64 + (ascii - 90);
// update the character with the ciphered text
str[i] = (char)ascii;
}
return str;
}
You are now to extend the above program to take as inputs files. The program should be able to read a file and encode or decode it as needed.
For the sake of simplicity, we assume that you only need to change letters [A-Z] in the file. You can safely ignore other letters in the file (i.e., keep those as is.)
Encrypting a file to cyphertext
Encrypt a file in.txt containing plaintext to a file out.txt containing ciphertext using shift
$ cf -ein.txt out.txt
Example
Consider f1.txt
HELLO WORLD THIS IS AMAZING WHY IS THIS SO AMAZING I HAVE NO IDEA 11231
After running the following command
$ ./cf -e 3 f1.txt f2.txt File f2.txt looks like
KHOOR ZRUOG WKLV LV DPDCLQJ ZKB LV WKLV VR DPDCLQJ L KDYH QR LGHD 11231
Decrypting a file to plaintext
Decrypting a file in.txt containing ciphertext to a file out.txt containing plaintext using shift
$ cf -din.txt out.txt
Example
After running the following command
$ ./cf -d 3 f2.txt f3.txt File f3.txt looks like
HELLO WORLD THIS IS AMAZING WHY IS THIS SO AMAZING I HAVE NO IDEA 11231
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
