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

In this assignment, you will write a program that performs asimple encryption on

ID: 3612679 • Letter: I

Question

In this assignment, you will write a program that performs asimple encryption on a file. We will take advantage of the factthat given a sequence of ones and zeros, if you XOR it with a givenkey (another sequence of ones and zeros), the results will almostalways be changed. Moreover, the new scrambled sequence of ones andzeros will be returned to the original data if XOR with thesame key is applied again.

Thus, by generating a random key, andperforming XOR on every byte of the file we can quickly and easilyscramble the contents. The process is easily reversed by performingXOR on the scrambled data with the same key.

We want to process the data in bytes, so use the char type forall your reading and writing of files.

Write a C++ program that

1)      Prompts the user for a choiceof encrypt or decrypt

2)      Prompts for the input andoutput filenames

3)      Has a function forencryption

a.       Generates a randomkey

b.      Stores the key in the encryptedfile

c.       Encrypts the data fromthe input file by performing XOR of the key with every byte fromthe input file

4)      Has a function fordecryption

a.       Reads the key from theencrypted file

b.      Decrypts the data from theinput file by performing XOR of the key with every byte from thefile.

5)      Performs the encryption ordecryption based on the user’s choice.

Explanation / Answer

#include<iostream>
#include <fstream>
using namespace std;
void Encrypt(ofstream&, ifstream&);
void Decrypt(ofstream&, ifstream&);
   int main()
   {char filename[30];
    char ch,type;
    int i;
    ifstream input;
    ofstream output;
   cout<<"Enter E for Encrypt, D for Decrypt:";
   cin>>type;
   cout<<"what is the name of the input file youare using? ";
   cin>>filename;  
  input.open(filename);         
  if(input.fail())           
       { cout<<"input file didnot open please check it ";
        system("pause");
        return 1;
        }
   cout<<"what is the name of the output file youare using? ";
   cin>>filename;  
  output.open(filename);          //open file
  if(output.fail())            //is it ok?
       { cout<<"output file didnot open please check it ";
        system("pause");
        return 1;
        }
if(toupper(type)=='E')
     Encrypt(output, input);
else
     Decrypt(output,input);      
output.close();
input.close();
system("pause");
return 0;

}  
void Encrypt(ofstream& output, ifstream& input)
{     char key=rand()%255+1;
      output<<key<<endl;
      char in;
      input.get(in);
      while(input)
         {if(in=='')
              output<<'';
          else
              {in=in^key;
               output<<in;
               }
          input.get(in);
           }
            
      return;
}
void Decrypt(ofstream& output, ifstream& input)
{     char key;
      input>>key;
      char in;
      input.get(in);
      while(input)
         {if(in=='')
              output<<'';
          else
              {in=in^key;
               output<<in;
               }
          input.get(in);
           }
      return;
}