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

Programing language is c++ BACKGROUND INFORMATION: A cryptosystem is a system fo

ID: 3607181 • Letter: P

Question

Programing language is c++

BACKGROUND INFORMATION: A cryptosystem is a system for encryption and decryption. Encryption, also called encoding or enciphering message, while decryption, also called decoding or deciphering, involves just the opposite - changing the secret message back into its original readable form. In cryptography, a plaintext file is a file containing data in its original, or readable, form while a ciphertext file is a file containing data in coded, or scrambled, form. involves changing the original message into a secret In this assignment, you will be creating a modified stream cipher that shifts each original letter or digit to a new, encrypted letter or digit, respectively, corresponding to individual randomly generated integral key. For example, consider the plaintext to be encrypted is: "ABC 1.", without the quotes. Then, for each letter and digit, we generate a random integer between 3 and 277 that serves as the key for the corresponding letters and digits. Consider the following example Plaintext: Key: Ciphertext: ABC 1 76 97 74 75 YUY6 Thus, the A would be shifted 76 places in the alphabet, which means that it "rolls over" twice, and then gets shifted 24 places in the alphabet to the ciphertext Y. Notice that there are only four keys generated for the six characters. Whitespace (and any unsupported characters) should be discarded, so no key is generated in this case. Note that the whitespace has been removed in the ciphertext. Additionally, punctuation should be kept as is, meaning that it is not discarded, but left in its original form (e.g., the"." in the above example remains a .") in the ciphertext. Mathematically, each letter of the plaintext and key can be given a number and a formula can be derived to encrypt for c (i.e., ciphertext) and m (i.e., plaintext) using k (i.e., key) as follows C=(m+k) % 26 m = (26 + c- k) % 26 Note that formula assumes A is 0, B is 1, and so forth with nothing to distinguish between uppercase A and lowercase a. Since you will most likely be using the character ASCII values, you will have to modify these formulas to fit your needs (e.g., the letter A has an ASCIl value of 65), but this should give you a place to start. One other item to consider is that characters (i.e., letters and digits in this case) roll over. For example, in shifting the letter z, then next letter would be the letter A (both uppercase).

Explanation / Answer

//MAIN.CPP

#include <iostream> //adding all necessary includes

#include <cstdlib>

#include <fstream>

#include <cctype>

using namespace std;

void display_credentials();

void encrypt(ifstream& fin, ofstream& fout, int&);

void decrypt(ifstream& fin, ofstream& fout, int&);

int main()

{

ifstream fin; //declaring all variables and streams

ofstream fout;

int key; //holds users key

char response; //holds response for what user wants to do 'E' or 'D'

char plain[33];

char cipher[33];

char encrypted[33];

char decrypted[33];

display_credentials(); //displays the program header

while(response != 'D' && response != 'd' && response != 'e' && response != 'E')

{

cout << "Would you like to ENCRYPT or DECRYPT a file (E or D)?: ";

cin >> response;

}

if(response == 'E' || response == 'e') //if user inputs E or e, this happens

{

cout << "Enter the name of your input file you want to encrypt: ";

cin >> plain; //reads in users filename for plaintext information to encrypt

fin.open(plain); //opens users file

if(fin.fail()) //"just in case" the users plaintext file doesn't open the program will exit

{

cout << "fin file opening failed." << endl;

exit(EXIT_FAILURE);

}

cout << "Enter the name of the output file to write the ciphertext: ";

cin >> cipher; //reads in users filname to store coded information from plaintext file

fout.open(cipher);

if(fout.fail()) {

cout << "fout file opening failed." << endl;

exit(EXIT_FAILURE);

}

cout << "Enter the numerical key (i.e., an integer) used to encrypt: ";

cin >> key; //reads in users key for encryption

encrypt(fin, fout, key); //calls function to encrypt file

}

else if(response == 'D' || response == 'd') //Happens if user inputs 'd' or 'D'

{

cout << "Enter the name of your input file you want to decrypt: ";

cin >> encrypted; //reads in filename that is encrypted that user would like to decrypt

fin.open(encrypted); //opens encrypted file

if(fin.fail()) {

cout << "fin file opening failed." << endl;

exit(EXIT_FAILURE);

}

cout << "Enter the name of the output file to write the plaintext: ";

cin >> decrypted; //reads in filename user would like to create or open to store decrypted text

fout.open(decrypted); //opens file from line 106

if(fout.fail()) //"just in case" users encrypted file fails to open the program will quit

{

cout << "fout file opening failed." << endl;

exit(EXIT_FAILURE);

}

cout << "Enter the numerical key (i.e., an integer) used to decrypt: ";

cin >> key; //reads in users key for encryption

decrypt(fin, fout, key); //calls function to decrypt file

fin.close();

fout.close();

return 0;

}

}

void display_credentials() //function to display the credentials

{

cout << "| ENCRYPTION AND DECRYPTION |" << endl;

  

return;

}

void encrypt(ifstream& fin, ofstream& fout, int& key)

{

char next; //declaring necessary variables

char c;

fin.get(next);

while(! fin.eof()) //does while not end of file

{

if (! isspace(next)) //does as long "next" is not a space

{

if (isalpha(next)) //does if "next" is a letter

{

if(next >= 'A'&& next <= 'Z') //capital letter

{

c = ((next - 65 + key) % 26) + 65;

fout.put(c);

}

else if(islower(next)) //lowercase letter

{

c = ((next - 97 + key) % 26) + 97;

fout.put(c);

}

}

if (isdigit(next)) //does if "next" is a digit/number

{

c = ((next - 48 + key) % 10) + 48;

fout.put(c);

}

}

fin.get(next);

}

return;

}

void decrypt(ifstream& fin, ofstream& fout, int& key)

{

char next; //declaring necessary variables

char c;

fin.get(next);

while(! fin.eof()) //does while not at the end of the file

{

if (! isspace(next)) //does while "next" is not a space

{

if (isalpha(next)) //does if "next" is a letter

{

if(next >= 'A' && next <= 'Z') //capital letter

{

c = ((next - 65 + 26 - key) % 26) + 65;

fout.put(c);

}

else if(next >= 'a' && next <= 'z') //lowercase letter

{

c = ((next - 97 + 26 - key) % 26) + 97;

fout.put(c);

}

}

if (isdigit(next)) //does if "next" is a digit

{

c = ((next - 48 - key + 10 * 10) % 10) + 48;

fout.put(c);

}

}

fin.get(next);

}

return;

}