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

c++ encryption and decryption 1 Overview In this assignment, students will devel

ID: 3764993 • Letter: C

Question

c++ encryption and decryption

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).

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:

voiddecrypt(string fileName);

Please note that the functions encrypt and decrypt will handle the file processing.

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:

1. Write the function numberOfLines to determine the number of the lines in a file int numberOfLines(string fileName);

2. Determine the number of the lines in the input text files text.txt or textEncrypted.txt and encryptionKey.txt.

3. Create dynamic array of strings for the text file or arrays of characters for the encryption key.

4. Read the lines from the file and copy them to the dynamic array

5. Process characters in each line in the arrays based on the encryption table.

6. Write the output text to the output file textEncrypted.txt or 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.

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

Explanation / Answer

/*
* 1729.cpp
*
* Created on: Nov 24, 2015
* Author: aravindv
*/

#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<string>
using namespace std;

int numberOfLines(string fileName) {
   string temp;
   int res = 0;
   ifstream in(fileName);

   while (getline(in, temp))
       res++;

   in.close();

   return res;

}

void encrypt(string fileName) {

   string *line;

   int nol = numberOfLines(fileName);//number of lines in text.txt file
   int nok = numberOfLines("encryptionKey.txt");//number of lines in encyptionKey.txt (number of charecter-code pairs)
   char key_codes[nok + 1][2];
   string temp;
   int k = 0;
   ifstream in(fileName);
   fstream out("textEncrypted.txt");//file to be written after encryption

   ifstream key("encryptionKey.txt");// key file
while(key>>key_codes[k][0]>>key_codes[k][1])//read key codes from encryptionKey.txt to key_codes[][] array
k++;


   while (getline(in, temp)) {


       for (int i = 0; i < temp.length(); i++)//for each letter in the line which is read
       {
           for (int j = 0; j < nok; j++) // replace the character with its code
           {
               if (temp[i] == key_codes[j][0])
                   temp[i] = key_codes[j][1];//encryption
           }

       }

       out << temp << endl;
      
      
   }
  
   out.close();
   in.close();
   key.close();
}

void decrypt(string fileName) {
   int nol = numberOfLines(fileName);
   int nok = numberOfLines("encryptionKey.txt");
   char key_codes[nok + 1][2];
   string temp;
   int k = 0;

   ifstream in(fileName);
   fstream out("textDecrypted.txt");//file to be written after encryption

   ifstream key("encryptionKey.txt");
   while(key>>key_codes[k][0]>>key_codes[k][1])
   k++;//read key codes from encryptionKey.txt to key_codes[][] array


   while (getline(in, temp)) {

       for (int i = 0; i < temp.length(); i++) //for each letter in the line which is read
       {
           for (int j = 0; j < nok; j++) // replace the code with its corresponding character
           {
               if (temp[i] == key_codes[j][1])
                   temp[i] = key_codes[j][0];//encryption
           }

       }

       out << temp << endl;
   }
   out.close();
   in.close();
}

int main() {
   int number;
   string s;

   cout << "Please enter en for encryption and de for decryption" << endl;

   cin >> s;

   if (s == "en") {
       cout << "Files encryptionKey.txt and text.txt are being processed..."
               << endl;
       encrypt("text.txt");
       cout << "Please open the output file textEncrypted.txt" << endl;
   }

   else if (s == "de") {
       cout<< "Files encryptionKey.txt and textEncrypted.txt are being processed..."<< endl;
       decrypt("textEncrypted.txt");
       cout << "Please open the output file textDecrypted.txt" << endl;
   }
   return 0;

}

*****************************************************************

text.txt

*****************************************************************

aravind
vijay
kumar

***************************************************

encryptionKey.txt

***************************************************

q 1
w 2
e 3
r 4
t 5
y 6
u 7
i 8
o 9
p 0
a !
s @
d #
f $
g %
h ^
j &
k *
l (
z )
x -
c _
v =
b +
n ;
m :

************************************************************************

textEncrypted.txt

***********************************************************************

!4!=8;#
=8&!6
*7:!4

*****************************************************************

textDecrypted.txt

*****************************************************************

aravind
vijay
kumar

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