I need help on this question, please answer it as accurate as possible since I d
ID: 3885111 • Letter: I
Question
I need help on this question, please answer it as accurate as possible since I do rate the answer.
Required function: void encrypt file(const std:string& filename, const std::string& password) You realized that someone else might see through your idea to "unzip" a file and determine it is not secure enough to transmit these files, so you thought encryption might be a better way to secure your pictures. To do some basic encryption, you have decided to take a password as an encryption key and then repeatedly XOR the file data with the password and write out this data as a new file. To accomplish this, we have to understand first how XOR works. The bitwise operation in C++ is computed using the A operator works on all primitive types. The XOR operation behaves as follows: Table 1: Truth table for XOR (source: http://www.cplusplus.com/doc/boolean/) 011 101 1 1 0Explanation / Answer
//Below is the code which encrypts the file using xor operation with password . I have printed the encryted output to the //console as well as to a file encrypt.txt. If you don't want to transfer to separate file you can comment that code
// Below is the solution
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void encrypt_file(const string filename, const string password){
ifstream inFile;
ofstream outFile;
inFile.open(filename.c_str(), ios::in | ios::binary);
string str((istreambuf_iterator<char>(inFile)), istreambuf_iterator<char>()); // Reads a text file into a single string.
inFile.close();
cout << "The file has been read into memory as follows:" << endl;
cout << str << endl;
// system("pause");
// Encryption
cout << "The file has been encrypted as follows:" << endl;
for (unsigned x = 0; x < str.size(); x++) // Steps through the characters of the string.
str[x] ^= password[x % password.size()]; // Cycles through a multi-character encryption key, and encrypts the character using an XOR bitwise encryption.
cout << str << endl;
// Write Encrypted File
outFile.open("encrypt.txt", ios::out | ios::binary);
outFile.write(str.c_str(), str.size()); // Writes the string to the binary file by first converting it to a C-String using the .c_str member function.
}
int main()
{
encrypt_file("E:\hobbies.txt", "password");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.