Question
URGENT C++ and Ubuntu Bash terminal I only need Task 3 Please. Posted below contains task 2 code. Introduction The Caesar cipher is a substitution
URGENT
C++ and Ubuntu Bash terminal
I only need Task 3 Please. Posted below contains task 2 code.
Introduction
The Caesar cipher is a substitution cipher where each letter in the original message (called the plaintext) is replaced with a letter corresponding to a certain number of letters up or down in the alphabet. The encrypted message (ciphertext) is not easily readable.
For example, here's the Caesar Cipher encryption of a message, using a left shift of 3.
Plaintext:
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
Ciphertext:
QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD
It is straightforward to recover the original message by using the opposite shift.
Task 1 (Weightage 50%)
You are asked to write a program that takes a shift value between +/- 26 and a plaintext message (no spaces) and returns the corresponding ciphertext. The program should also implement a decryption routine that reconstructs the original plaintext from the ciphertext.
Example usage
$ caesar Enter shift +/- 26: -3 Enter plaintext message (A-Z only, no spaces): THE ciphertext: QEB plaintext: THE
or
$ caesar Enter shift +/- 26: 1 Enter plaintext message (A-Z only, no spaces): ZZZ ciphertext: AAA plaintext: ZZZ
We assume that the user message only consists of uppercase English alphabet (A-Z).
Task 2 (Weightage 25%)
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 . Flag -e here refers to encryption.
$ cf -e in.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 . Flag -d here refers to decryption.
$ cf -d in.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
TASK 2 CODE
#include #include #include #include //only from A-Z
using namespace std; // get the ciphered text string encrypt(string str, int); // decrypt the cipher text string decrypt(string str, int);
int main(int argc, char* argv[]) {
if(argc != 5) { cout << "Usage: " << argv[0] << "
" << endl; return 1; }
bool encryptFlag = false; //check whethere we have to encrypt or decrypt if(strcmp(argv[1], "-e") == 0) encryptFlag = true; else encryptFlag = false;
//get the no. of shift int n = atoi(argv[2]);
ifstream infile(argv[3]); ofstream outfile(argv[4]);
if(infile.fail()) { cout << "ERROR: unable to open input file " << argv[3] << endl; return 1; }
if(outfile.fail()) { cout << "ERROR: unable to open output file " << argv[4] << endl; return 1; }
string line; string converted; while(getline(infile, line)) { if(encryptFlag) converted = encrypt(line, n); else converted = decrypt(line, n);
outfile << converted << endl; } cout << "Please check file " << argv[4] << endl; infile.close(); outfile.close();
return 0;
}
// get the ciphered text
string encrypt(string str, int n) { int i; for( i = 0 ; i < str.length() ; i++ ) { // to get ascii convert string to int int ascii = (int)str[i]; if(ascii >= 'A' && ascii <= 'Z') { 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]; if(ascii >= 'A' && ascii <= 'Z') {
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;
}
COMPLETE TASK 3 THANK YOU
Task 3 (Weightage 25%)
Now change the code such that the following commands work as intended. Notice here we are using IO redirection to specify input and output files/streams.
Encryption
$ ./cf -e 3 < f1.txt > f2.txt
and
$ cat f1.txt | ./cf -e 3 > f2.txt
Decryption
$ ./cf -d 3 < f3.txt > f3.txt
and
$ cat f2.txt | ./cf -d 3 > f3.txt
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