Question

In: Computer Science

Background: You can create a simple form of encryption using the xor Boolean operator. For example,...

Background:
You can create a simple form of encryption using the xor Boolean operator. For example, if you want to encrypt the letter 'A':

  1. Choose another letter as a "key", say 'X'
  2. Encrypt 'A' with the xor function: encrypted = 'A' xor 'X'
  3. Now you can decrypt by xor'ing your encrypted value with 'X' again: decrypted = encrypted xor 'X'

Your task:
Write a C++ program that meets the following requirements:

  1. Asks the user for the name of an input file and a "key" character
  2. Encrypt each character in the file using the provided key and the xor function. (Note: The program should handle all standard alphanumeric characters.)
  3. Your program should save the results of the encryption into a new file with the same base name, but with a ".xor" file extension.
  4. The program should also provide an option to decrypt a given file.
  5. Include appropriate error checking.

Submit:

  • Fully documented source code

Documentation should include:

  • Pseudocode
  • Top comment block (The name of the program, The name of the programmer, The date of the program, and A brief description of the purpose of the program)
  • Function comments (Description, pre and post conditions)
  • Internal comments for all functions

Solutions

Expert Solution

// C++ program to implement XOR encryption and decryption

#include <iostream>

#include <fstream>

#include <cctype>

using namespace std;

string encrypt(string line, char key);

string decrypt(string line, char key);

int main() {

       char key;

       string filename;

       ifstream fin;

       cout<<"Enter the filename : ";

       getline(cin,filename); // provide full path to file

       filename = filename.substr(0,filename.length()-1);

       cout<<"Enter the key character : ";

       cin>>key;

       fin.open(filename.c_str());

       if(fin.is_open())

       {

             string outFile = filename.substr(0,filename.find('.'))+".xor";

             ofstream fout(outFile.c_str());

             string line;

             char option;

             while(!fin.eof())

             {

                    getline(fin,line);

                    fout<<encrypt(line,key);

             }

             fin.close();

             fout.close();

             cout<<"Do you want to decrypt the file (y/n) ? ";

             cin>>option;

             if(tolower(option) == 'y')

             {

                    cout<<"Decrypted file : "<<endl;

                    fin.open(outFile.c_str());

                    if(fin.is_open())

                    {

                           while(!fin.eof())

                           {

                                 getline(fin,line);

                                 cout<<decrypt(line,key);

                           }

                    }

                    fin.close();

             }

       }else

             cout<<"Unable to open file : "<<filename<<endl;

       return 0;

}

// function to encrypt a line using XOR

// Pre-condition : line is not null and key is not empty

// Post-condition : line and key is unchanged

// Returns : string which is obtained by XOR encryption of the standard alphanumeric characters and key

string encrypt(string line, char key)

{

       string encryptedLine="";

       for(size_t i=0;i<line.length();i++)

       {

             encryptedLine += line.at(i)^key;

       }

       return encryptedLine;

}

// function to decrypt a line using XOR

// Pre-condition : line is not null and key is not empty

// Post-condition : line and key is unchanged

// Returns : string which is obtained by XOR decryption of the standard alphanumeric characters and key

string decrypt(string line, char key)

{

       string decryptedLine="";

       for(size_t i=0;i<line.length();i++)

       {

             decryptedLine += (line.at(i)^key);

       }

       return decryptedLine;

}

//end of program

Output:

Input file:

Output file:

Console:


Related Solutions

c++ using class... define operator overloading and give simple example how we can use operator overloading...
c++ using class... define operator overloading and give simple example how we can use operator overloading by writing simple program in which different operators are used to add, subtract, multiply and division.
2)Prove, using Boolean Algebra theorems, that the complement of XOR gate is XNOR gate (Hint :...
2)Prove, using Boolean Algebra theorems, that the complement of XOR gate is XNOR gate (Hint : Prove that AB + AB = AB + AB by using De-Morgan’s theorem)
using PDO, MYSQL, and Html, how can i create a simple registration and login form for...
using PDO, MYSQL, and Html, how can i create a simple registration and login form for cPanel?
2)Prove, using Boolean Algebra theorems, that the complement of XOR gate is XNOR gate(Hint : Prove...
2)Prove, using Boolean Algebra theorems, that the complement of XOR gate is XNOR gate(Hint : Prove that AB + AB = AB + ABby using De-Morgan’s theorem)3)Draw the K-Map for the following Boolean function. Obtain the simplified Sum of Products (SOP) expression, using the K-Map minimization procedure .?(????)=∑?(1,2,3,5,7,9,11,13)
Create a simple Chart of Accounts for an owner/ operator business such as a lawn mowing...
Create a simple Chart of Accounts for an owner/ operator business such as a lawn mowing round and also mention how the opening balance for each cost centre/ account can be established? Discuss in 80–100 words.
Using PHP and MYSQL and with a simple customer database, how can I create a simple...
Using PHP and MYSQL and with a simple customer database, how can I create a simple log in and registration system for an ecommerce site
Write a simple javascript program using express and node.js to create a simple webpage that can...
Write a simple javascript program using express and node.js to create a simple webpage that can lead to other pages within the program. if possible, Comment everything out so I could understand how every things works.  But basically, server should be hosted on localhost:8000 and should have a simple starting page. Maybe a welcome message, with at least two "links" that goes to a "homepage" and then a "exit" page.
Can you explain the hardware (using xor gates) implementation of the circuit that calculate CRC key,...
Can you explain the hardware (using xor gates) implementation of the circuit that calculate CRC key, suppose the polynomial key is x^4 + x^2 +x+1 and the message 100110011010. I need the hardware implementation only.
(Elgamal encryption): given elgamal encryption ciphersystem: a)show how can we create a new legal encryption from...
(Elgamal encryption): given elgamal encryption ciphersystem: a)show how can we create a new legal encryption from two different encryptions that we don't know their decryptions b)how can an adversary take advantage of the scheme at a) (what's written above), in order to attack a preknown encrypted text? elaborate
Create an example of a table that violates first normal form. Create an example of a...
Create an example of a table that violates first normal form. Create an example of a table that is in first normal form but violates second normal form. Create an example of a table that is in second normal form but violates third normal form. Please use own table or no plagiarism please.Thank you.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT