1 Overview In this assignment, students will develop and implement a program tha
ID: 3764922 • Letter: 1
Question
1 Overview
In this assignment, students will develop and implement a program that performs "encryption" and "decryption" based on an encryption key (i.e., a table of character mappings). Encryption is the process of encoding messages in such a way that only parties who have the encryption key can read messages. Decryption is the process of converting encrypted data back into its original form. The primary objective of this assignment for the students is to gain experience in file, character and string processing.
2 Program
When the program executes, it will ask the user if it should perform encryption (enter "en" for encryption) or decryption (enter "de" decryption).
"en":
"de":
If encryption is chosen by the user, your program will read the encryption key from the file encryptionKey.txt and the text from the file text.txt. Then, it will encrypt the content of the file text.txt. The function header should be:
void encrypt(string fileName);
If decryption is chosen by the user, your program will read the encryption key from the file encryptionKey.txt and the text from the file textEncrypted.txt. Then, it will decrypt the content of the file textEncrypted.txt. Namely, the program will convert encrypted text back into its original form. The function header should be:
void decrypt(string fileName);
Please note that the functions encrypt and decrypt will handle the file processing.
Program
encryptionKey.txt
text.txt
textEncrypted.txt
Program
encryptionKey.txt
textEncrypted.txt
textDecrypted.txt
For example, let’s assume that you are given the following encryption key:
Character e t a o i n s h r d l c u m w f
Code ~ # $ % ^ & * + { } / < > @ _
If the content of the input file is hakan, the encrypted output file will be as follows:
3 Program Structure
We use functions to break a problem into smaller sub-problems. This reduces program complexity. The main
function will be the driver of the program. The main function calls the other functions.
The steps of the program may be summarized as follows:
Write the function numberOfLines to determine the number of the lines in a file
int numberOfLines(string fileName);
Determine the number of the lines in the input text files text.txt or textEncrypted.txt and
encryptionKey.txt.
Create dynamic array of strings for the text file or arrays of characters for the encryption key.
Read the lines from the file and copy them to the dynamic array
Process characters in each line in the arrays based on the encryption table
Write the output text to the output file textEncrypted.txtor textDecrypted.txt.
3.1 Format of the encryptionKey.txt File
In each line, the first character is the letter and the second is the code.
The table of character mappings can be represented by two arrays of characters: one for the letter and one for
the code. Please note that an example of encryptionKey.txt is provided.
hakan +$k$&
e ~
t #
a $
Fall 2015
3.2 Format of the text.txt File
This is an ordinary text file that contains letters and punctuation marks. Please note that an example of the file text.txt is provided.
3.3 Determining the Number of Lines in a File
When the number of lines in a file is not provided, there may be several approaches to find it. For example, you can use the function getline in while loop and count the number of the lines.
while (getline(readFromFile, line))
{
number++;
}
Please note that you may need to close and re-open the file.
3.4 Sample User Interface
The main function of the program prompts the user to choose which of the two possible actions s/he wants. After that choice is made, the proper input files are read in the related functions and the output file is generated. Please note that you can hard code the file names. Namely, the user does not need to provide file names.
> Please enter "en" for encryption and "de" for decryption: en
> Files encryptionKey.txt and text.txt are being processed...
> Please open the output file textEncrypted.txt
or
> Please enter "de" for encryption and "de" for decryption: de
> Files encryptionKey.txt and textEncrypted.txt are being processed...
> Please open the file textDecrypted.txt
Fall 2015
4 Grading
Your file name must be as follows: FirstNameLastNameUHID.cpp
A correct solution: worth 100 points
Deductions:
ERROR DEDUCTED POINTS Program cannot be compiled 40
Program terminated unexpectedly during runtime
40 Proper indentation is not used 20
Function prototype is different, for each function
10 Code is not commented 10
.cpp files do not follow Naming Conventions for submissions, listed in Assignment Guidelines
10 The function numberOfLines is not defined 20
The function encyrpt is not defined
30 The function decyrpt is not defined 30
Encrypted test file cannot be decrypted properly
30 Text file cannot be encrypted properly 30
Content is not read from file, for each file
20 Dynamic arrays are not used 20
1. Feel free to discuss ideas and implementations with your classmates, however DO NOT share code.
2. If you have a question about the assignment, do not wait until the last minute to ask.
3. Normal deductions for late submissions will be in effect. Please see Assignment Guidelines.
4. ANY kind of cheating, will result in a grade of 0.
the enctyprion and decryption files are .txt files msg me if anyone can help
Explanation / Answer
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<string.h>
using namespace std;
//DECLARING DYNAMIC CHARACTER ARRAYS FOR encyrptionKey.txt
char *characterArray;
char *codeArray;
//FUNCTION FIND THE NUMBER OF LINES IN THE GIVEN FILE
int numberOfLines(string fileName){
string line;
int number=0;
ifstream readFromFile(fileName);
while(getline(readFromFile,line)){
number++;
}
readFromFile.close();
return number;
}
//PERFORMING THE ENCRYPTION OF THE GIVEN FILE
void encrypt(string fileName){
string *line;
int number=numberOfLines(fileName);
line=new string[number];
string tempLine;
int k=0;
//OPEN THE “text.txt” FOR READING
ifstream readFromFile(fileName);
//OPEN THE “textEncryption.txt”FORWRITING THE ENCRYPTED LINE
fstream writeToFile("textEncrypted.txt");
while(getline(readFromFile,tempLine)){
line[k]=tempLine;
//DO THE ENCRYPTION
for(int k1=0;k1<tempLine.length();k1++){
for(int k2=0;k2<sizeof(characterArray)/sizeof(characterArray[0]);k2++){
if(tempLine[k1]==characterArray[k2])
tempLine[k1]=characterArray[k2];
}
}
//WRITE THE ENCRYPTED LINE
writeToFile<<tempLine<<endl;
k++;
}
writeToFile.close();
readFromFile.close();
}
//PPERFORMING THE DECRYPTION
void decrypt(string fileName){
string *line;
int number=numberOfLines(fileName);
line=new string[number];
string tempLine;
int k=0;
ifstream readFromFile(fileName);
fstream writeToFile("textDecrypted.txt");
while(getline(readFromFile,tempLine)){
line[k]=tempLine;
//DO THE DECRYPTION
for(int k1=0;k1<tempLine.length();k1++){
for(int k2=0;k2<sizeof(codeArray)/sizeof(codeArray[0]);k2++){
if(tempLine[k1]==codeArray[k2])
tempLine[k1]=codeArray[k2];
}
}
writeToFile<<tempLine<<endl;
k++;
}
writeToFile.close();
readFromFile.close();
}
int main(){
int number;
string getInput;
number=numberOfLines("encyptionKey.txt");
characterArray=new char[number];
codeArray=new char[number];
ifstream readFromFile("encryptionKey.txt");
int k1=0;
if(readFromFile.is_open()){
while(readFromFile>>characterArray[k1]>>codeArray[k1]){
k1++;
}
}
readFromFile.close();
cout<<"Please enter en for encyption and de for decryption"<<endl;
cin>>getInput;
//ENCRYPTION
if(getInput=="en"){
cout<<"Files encryptionKey.txt and text.txt are being processed..."<<endl;
encrypt("text.txt");
cout<<"Please open the output file textEncrypted.txt"<<endl;
}
//DECRYPTION
else if(getInput=="de"){
cout<<"Files encryptionKey.txt and textEncrypted.txt are being processed..."<<endl;
decrypt("textEncrypted.txt");
cout<<"Please open the output file textDecrypted.txt"<<endl;
}
//system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.