Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A simple form of encryption can be performed on text by using a simple tool know

ID: 3634098 • Letter: A

Question

A simple form of encryption can be performed on text by using a simple tool known as a substitution cipher. In this approach, every character maps to a different character according to the cipher. This type of encryption is common in the word puzzles known as cryptograms. In this assignment, you will allow the user to encrypt or decrypt a file using a character based substitution cipher.


In our program, our ciphers will be guaranteed to be composed of 26 unique, lowercase, alphabetic characters. The position of the characters in the cipher correspond to the unencrypted character. For example, an ‘a’ will be replaced with the first letter in the cipher and a ‘z’ will be replaced with the last letter. The following example illustrates how the cipher will be used:

/* substitution cipher */
bcdefghijklmnopqrstuvwxyza

/* unencrypted message */
Hello World!

/* encrypted message */
Ifmmp Xpsme!
You will read the 26 character cipher from a file called “cipher.txt” into an array. The first element of the array “array[0]” will store the substitution character for ‘a’. The second element stores the substitution for ‘b’. This pattern continues until the end of the array. Once you have read the cipher into an array, you can close the file as it is no longer needed.

Your program will ask the user if she wants to encrypt or decrypt the input file. If the user chooses to encrypt, then the program will read an unencrypted message from the file unencrypted.txt and write an encrypted message to the screen. If she chooses to decrypt, then the program will read an encrypted message from the file encrypted.txt and write an unencrypted message to the screen. The user will indicate her choice by responding to the prompt with one of the characters from the set {‘D’, ‘d’, ‘E’, ‘e’}. The program will continue to prompt the user until a valid entry is provided.

When performing the encryption/decryption, only alphabetic characters will be converted. Any other characters will not be changed. It is important to note that capital letters will be converted according the the same cipher and will maintain their case.

It may be helpful to be reminded that characters are really just small integers. As such, it is completely acceptable to add and subtract characters from each other. For example, the expression (‘b’ - ‘a’) evaluates to the value 1.

Your program will be compiled as follows:

$ g++ project_5.cpp -o crypto
$

Encryption
The following example will encrypt the file unencrypted.txt according to the cipher.txt file.

$ ./crypto
Would you like to (E)ncrypt or (D)ecrypt the input file? a
Would you like to (E)ncrypt or (D)ecrypt the input file? b
Would you like to (E)ncrypt or (D)ecrypt the input file? e

...snl ugkn pgzr gsqqknr, ugkn uk svvbu jekklbm eznh, ugkn uk vkp zp eznh jebm
kikey izvvshk snl kikey gsmvkp, jebm kikey rpspk snl kikey fzpy, uk uzvv
dk sdvk pb rqkkl oq pgsp lsy ugkn svv bj Hbl'r fgzvlekn, dvsfc mkn snl
ugzpk mkn, Xkur snl Hknpzvkr, Qebpkrpsnpr snl Fspgbvzfr, uzvv dk sdvk pb
xbzn gsnlr snl rznh zn pgk ubelr bj pgk bvl Nkheb rqzezposv:

Jekk sp vsrp! Jekk sp vsrp!

Pgsnc Hbl Svmzhgpy, uk sek jekk sp vsrp!
$

Decryption
The following example will decrypt the file encrypted.txt according to the cipher.txt file.

$ ./crypto
Would you like to (E)ncrypt or (D)ecrypt the input file? R
Would you like to (E)ncrypt or (D)ecrypt the input file? T
Would you like to (E)ncrypt or (D)ecrypt the input file? D

Row, row, row your boat,
Gently down the stream.
Merrily, merrily, merrily, merrily,
Life is but a dream.
$

Missing Cipher File
If the ‘cipher.txt’ file is not found, the following will be displayed:

$ ./crypto
Missing file 'cipher.txt'
$


Missing Unencrypted File
If the user wishes to encrypt, and the ‘unencrypted.txt’ file does not exist, the error is as follows:

$ ./crypto
Would you like to (E)ncrypt or (D)ecrypt the input file? E

Missing file 'unencrypted.txt'
$


Missing Encrypted File
Similarly, if the user wishes to decrypt, and the ‘encrypted.txt’ file does not exist, the error is as follows:


$ ./crypto
Would you like to (E)ncrypt or (D)ecrypt the input file? D

Missing file 'encrypted.txt'
$


Tips

You will need to use the cctype library functions.

If your program is working, you should be able to decrypt:

Kn ysc rxt hoxq flkg ... nkhgf sn xvv ysc xho xaogsuo!

Gorstqvy, K akvv xaxhq ysc 5 wstcg jsktfg st ysch lsuoashd kn ysc aoxh x lxf kt rvxgg st Ustqxy, Tsbouwoh 28. K ecgf flktd kf ascvq wo rssv fs dtsa als kg "kt flo dtsa" ... dtsa alxf K uoxt?

Alot ysc fcht kt ysch lsuoashd, ahkfo st flo fsj sn kf tozf fs ysch txuo flxf ysc xho aoxhktm x lxf, gs flxf K mkbo ysc flo wstcg jsktfg.

Mssq ashd!

using this cipher xwrqonmlkedvutsjihgfcbazyp

Explanation / Answer

#include using std :: cout; using std :: endl; void encrypt( char [ ] ); // prototypes of functions used in the code void decrypt( char * ePtr ); int main( ) { // create a string to encrypt char string[ ] = "this is a secret!"; cout
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote