Question: File Encryption and Decryption Write a c++ program that uses a map to assign codes to each letter of the alphabet. For example: map codes

File Encryption and Decryption

Write a c++ program that uses a map to assign codes to each letter of the alphabet. For example: map codes =

{ {'A', '%'}, {'a', '9'}, {'B', '@'}, {'b', '#'}, etc ...};

Using this example, the letter A would be assigned the symbol %, the letter a would be assigned the number 9, the letter B would be assigned the symbol @, and so forth. The program should open a specified text file, read its contents, then use the map to write an encrypted version of the files contents to a second file. Each character in the second file should contain the code for the corresponding character in the first file. Write a second program that opens an encrypted file and displays its decrypted contents on the screen. A driver program, lab_encryption_driver.cpp, is provided. This driver program will populate the keys to code and decode messages. Complete the encryption() function to code/decode the message. Example of program input/output:

---------------------------------------------------- Enter a message ('end' to stop):

This is my secret message...

--- coded message: 5wKU,KU,qo,UiR^i),qiUU2/irrr

--- decode message: This is my secret message...

Enter a message ('end' to stop):

sharon.wu@reedleycollege.edu

--- coded message: Uw2^`WrL[J^iilHioR`HHi/iril[

--- decode message: sharon.wu@reedleycollege.edu

Enter a message ('end' to stop):

Programming is fun!

--- coded message: 4^`/^2qqKW/,KU,j[W!

--- decode message: Programming is fun!

Enter a message ('end' to stop):

end

--- coded message: iWl

--- decode message: end Press any key to continue . .

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

Lab_Encryption_driver.cpp

//

// lab_Encryption.cpp

//

#include

#include

#include

#include

#include

#include

using namespace std;

void init_code(map &code, map &decode);

void encryption(map&code, string &message, string &codedMSG);

int main()

{

map

code, // use code map to encrypt message

decode; // use decode map to decrypt message

init_code(code, decode); // populate code and decode keys

//-------encryption------------------

string message, codedMSG, decodedMSG;

do

{

cout << "Enter a message ('end' to stop): ";

getline(cin, message);

encryption(code, message, codedMSG);

cout << "--- coded message: " << codedMSG << endl;

encryption(decode, codedMSG, decodedMSG);

cout << "--- decode message: " << decodedMSG << endl;

cout << endl;

} while (message != "end");

system("pause");

return 0;

}

void encryption(map&code, string &message, string &codedMSG)

{

}

void init_code(map &code, map &decode)

{

map key1 = {

{ ' ', ',' },{ '!', '!' },{ '"', 'X' },{ '#', 'Q' },{

'$', ' ' },

{ '%', ';' },{ '&', 'D' },{ '\'', 'v' } ,{ '(', ' =

' } ,{ ')', 'M' } ,

{ '*', 'a' } ,{ '+', 'N' },{ ',', '@' } ,{ '-', 'd' } ,{

'.', 'r' } ,

{ '/', 'S' },{ '0', ':' },{ '1', 'V' } ,{ '2', 'z' } ,{

'3', '1' } ,

{ '4', '(' },{ '5', 'O' },{ '6', '6' } ,{ '7', 'B' } ,{

'8', '>' } ,

{ '9', '\'' },{ ':', '&' },{ ';', 'p' } ,{ '<', 'y' } ,{

'=', '$' } ,

{ '>', '"' },{ '?', 'T' },{ '@', 'J' } ,{ 'A', 'C' } ,{

'B', 'c' } ,

{ 'C', 'Z' },{ 'D', '9' },{ 'E', '#' } ,{ 'F', 'e' } ,{

'G', '0' } ,

{ 'H', 'f' },{ 'I', 'E' },{ 'J', '.' } ,{ 'K', '}' } ,{

'L', '8' } ,

{ 'M', 'g' },{ 'N', 's' },{ 'O', 'x' } ,{ 'P', '4' } ,{

'Q', 'h' } ,

{ 'R', '_' },{ 'S', 'A' },{ 'T', '5' } ,{ 'U', '<' } ,{

'V', 'I' } ,

{ 'W', '{' },{ 'X', 't' },{ 'Y', 'm' } ,{ 'Z', 'u' } ,{

'[', ']' } ,

{ '\\', 'Y' },{ ']', 'n' },{ '^', 'b' } ,{ '_', '*' } ,{

'`', '%' } ,

{ 'a', '2' },{ 'b', '7' },{ 'c', 'R' } ,{ 'd', 'l' } ,{

'e', 'i' } ,

{ 'f', 'j' },{ 'g', '/' },{ 'h', 'w' } ,{ 'i', 'K' } ,{

'j', '|' } ,

{ 'k', 'F' },{ 'l', 'H' },{ 'm', 'q' } ,{ 'n', 'W' } ,{

'o', '`' } ,

{ 'p', '+' },{ 'q', 'G' },{ 'r', '^' } ,{ 's', 'U' } ,{

't', ')' } ,

{ 'u', '[' },{ 'v', '?' },{ 'w', 'L' } ,{ 'x', '3' } ,{

'y', 'o' } ,

{ 'z', 'P' },{ '{', '\\' },{ '|', 'k' } ,{ '}', '-' } };

map key2 = {

{ ' ', '$' } ,{ '!', '!' } ,{ '"', '>' } ,{ '#', 'E' } ,{

'$', '=' } ,

{ '%', '`' } ,{ '&', ':' } ,{ '\'', '9' } ,{ '(',

'4' } ,{ ')', 't' } ,

{ '*', '_' } ,{ '+', 'p' } ,{ ',', ' ' } ,{ '-', '}' } ,{

'.', 'J' } ,

{ '/', 'g' } ,{ '0', 'G' } ,{ '1', '3' } ,{ '2', 'a' } ,{

'3', 'x' } ,

{ '4', 'P' } ,{ '5', 'T' } ,{ '6', '6' } ,{ '7', 'b' } ,{

'8', 'L' } ,

{ '9', 'D' } ,{ ':', '0' } ,{ ';', '%' } ,{ '<', 'U' } ,{

'=', '(' } ,

{ '>', '8' } ,{ '?', 'v' } ,{ '@', ',' } ,{ 'A', 'S' } ,{

'B', '7' } ,

{ 'C', 'A' } ,{ 'D', '&' } ,{ 'E', 'I' } ,{ 'F', 'k' } ,{

'G', 'q' } ,

'H', 'l' } ,{ 'I', 'V' } ,{ 'J', '@' } ,{ 'K', 'i' } ,{

'L', 'w' } ,

{ 'M', ')' } ,{ 'N', '+' } ,{ 'O', '5' } ,{ 'P', 'z' } ,{

'Q', '#' } ,

{ 'R', 'c' } ,{ 'S', '/' } ,{ 'T', '?' } ,{ 'U', 's' } ,{

'V', '1' } ,

{ 'W', 'n' } ,{ 'X', '"' } ,{ 'Y', '\\' } ,{ 'Z',

'C' } ,{ '[', 'u' } ,

{ '\\', '{' } ,{ ']', '[' } ,{ '^', 'r' } ,{ '_',

'R' } ,{ '`', 'o' } ,

{ 'a', '*' } ,{ 'b', '^' } ,{ 'c', 'B' } ,{ 'd', '-' } ,{

'e', 'F' } ,

{ 'f', 'H' } ,{ 'g', 'M' } ,{ 'h', 'Q' } ,{ 'i', 'e' } ,{

'j', 'f' } ,

{ 'k', '|' } ,{ 'l', 'd' } ,{ 'm', 'Y' } ,{ 'n', ']' } ,{

'o', 'y' } ,

{ 'p', ';' } ,{ 'q', 'm' } ,{ 'r', '.' } ,{ 's', 'N' } ,{

't', 'X' } ,

{ 'u', 'Z' } ,{ 'v', '\'' } ,{ 'w', 'h' } ,{ 'x',

'O' } ,{ 'y', ' < ' } ,

{ 'z', '2' } ,{ '{', 'W' } ,{ '|', 'j' } ,{ '}', 'K' } };

code = key1;

decode = key2;

}

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!