Explore how information can be encrypted to securely transfer between systems, develop a simple program that can encrypt/decrypt a file. The program should take as its first command line argument the file to be encrypted/decrypted. The second command line argument should be the encrypted/decrypted file to create. The third command line argument should be the password to use during the encryption/decryption process. Use the attached program Project2b.cpp as a starting point. Implement the processFile() method to perform the encryption/decryption.
In this program we will use the XOR operator which is its own inverse. This will allow us to just have one method that can either do encryption or decryption. Basically running the program on a file once will encrypt it. Running the program again with the same password on the encrypted file will decrypt it.
So say that your file contains the data “Orson Welles” and you encrypt it with the password “Rosebud”. Your processFile() should output the following characters to the second filename:
'O' ^ 'R' == 0x1d
'r' ^ 'o' == 0x1d
's' ^ 's' == 0x00
'o' ^ 'e' == 0x0a
'n' ^ 'b' == 0x0c
' ' ^ 'u' == 0x55
'W' ^ 'd' == 0x33
'e' ^ 'R' == 0x37
'l' ^ 'o' == 0x03
'l' ^ 's' == 0x1f
'e' ^ 'e' == 0x00
's' ^ 'b' == 0x11
If you run the encrypted file through our program again using the same password you would get:
0x1d ^ 'R' == 'O'
0x1d ^ 'o' == 'r'
0x00 ^ 's' == 's'
0x0a ^ 'e' == 'o'
0x0c ^ 'b' == 'n'
0x55 ^ 'u' == ' '
0x33 ^ 'd' == 'W'
0x37 ^ 'R' == 'e'
0x03 ^ 'o' == 'l'
0x1f ^ 's' == 'l'
0x00 ^ 'e' == 'e'
0x11 ^ 'b' == 's'
For example:
> ./Project2B 996-0.txt 996-0.txt.crypt Rosebud
> ./Project2B 996-0.txt.crypt 996-0.txt.decrypt Rosebud
> diff 996-0.txt 996-0.txt.decrypt
The diff command shouldn’t print any differences. On Windows use the “comp” command instead of “diff”.
Project2b.cpp
/******************************************************************************
* This program encrypts and decrypts a file using a simple substitution cipher.
*
* Copyright©2021 Richard Lesh. All rights reserved.
*****************************************************************************/
#include
#include
#include
using namespace std;
void processFile(ifstream& ifs, ofstream& ofs, string password) {
// loop reading characters from ifs
// Encrypt/decrypt using XOR with password
// Put modified character to ofs
}
int main(int argc, char **argv) {
if (argc != 4) {
cout
exit(1);
}
string in_file(argv[1]);
string out_file(argv[2]);
string password(argv[3]);
try {
ifstream ifh(in_file.c_str(), ios_base::binary);
if (!ifh.good()) {
throw ios_base::failure("Problem opening input file!");
}
ofstream ofh(out_file.c_str(), ios_base::binary);
if (!ofh.good()) {
throw ios_base::failure("Problem opening output file!");
}
processFile(ifh, ofh, password);
ifh.close();
ofh.close();
} catch (ios_base::failure ex) {
cout
}
return 0;
}