Question: C++ Programming Question..... Introduction The Caesar cipher is a substitution cipher where each letter in the original message (called the plaintext) is replaced with a

C++ Programming Question.....

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.

-------------------------------------------------------------------------------------------------------------------------------

Part 1

You are asked to make 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).

Part 2

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.

$ ./caesar -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

$ ./caesar -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.

$ caesar -d  in.txt out.txt 

Example

After running the following command

$ ./caesar -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 

Part 3

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

$ ./caesar -e 3 < f1.txt > f2.txt 

and

$ cat f1.txt | ./caesar -e 3 > f2.txt 

Decryption

$ ./caesar -d 3 < f3.txt > f3.txt 

and

$ cat f2.txt | ./caesar -d 3 > f3.txt 

Part 4

Thus far the program only handles capital letters A-Z. Now add support for small letters a-z and digits 0-9 Assume that for letters shift values are between -26 and +26 and for digits shift values are between -10 and +10. This program will support both input and output files and io redirection.

Usage

Say file in.txt contains

Good dog 2 

Then the following command make output file as seen below

$ ./caesar -e 1 3 in.txt output.txt 

Contents of output.txt file

Hppe eph 5 

Similarly, we can decrypt output.txt to create original.txt as follows

$ ./caesar -d 1 3 in.txt original.txt 

The contents of the original.txt are

Good dog 2 

Program help

$ ./caesar -[e|d]   [file-input] [file-ouput] - `-e` refers to encryption - `-d` refers to decryption - `shift-letter` a value between -26 and +26 - `shift-digit` a value between -10 and +10 - `file-input` name of the input file. If none specified, read from `stdin` - `file-output` name of the output file. If none specified, write to `stdout`

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 Databases Questions!