Please edit your code on the following code I provided PLEASE ONLY USE THESE THR
ID: 3889399 • Letter: P
Question
Please edit your code on the following code I provided
PLEASE ONLY USE THESE THREE HEADERS I PROVIDED!!
I DO RATE THE ANSWER, PLEASE ANSWER IT AS ACCURATE AS POSSIBLE.
EXAMPLE: Consider a 16-byte file composed of all Os stored in a character array named data and a password string "cse250" data (hex representation): 00000000000000000000000000000000 data (as chars): e1ele1e1elele1e1elele1e1e elele password (as chars): cse250 Then we can encrypt by computing an XOR between the password and the data. Fortunately, computing 0 ^ x = x, so taking the XOR of our password with data that is all 0 will simply create repeated copies of our password. XOR: C se250 cs e 2 5 0 cse 2Explanation / Answer
Your headers I am using these minimal three headers
#include <iostream> //for cout
#include <fstream> // for file handling
#include <string> // for strings
void encrypt_file(const std::string& filename, const std::string& password) {
//open input file
std::ifstream input_file(filename.c_str(), std::ifstream::binary);
//if error in opening input file
if(!input_file)
{
std::cout << "Error opening input file [" << filename << "] ";
return;
}
//name of output file
std::string new_filename = filename+"-encrypted";
//open outf=put file in binary mode
std::ofstream output_file(new_filename.c_str(), std::ofstream::binary);
//if error in opening output file
if(!input_file)
{
std::cout << "Error opening output file [" << filename << "-encrypted ";
return;
}
//since data will be all zeros we can directly assign all zeros to data
//but we will get that from file
char data[16];
//loop to get data from input file and performing xor operation on password
//simultaneously and writting the result directly to output file
//Note that while outputting data is casted to char and then wriiten if not
//data will be stored in int because xor returns an integer result
for (int i = 0; i < 16; ++i)
{
//read data from input file
input_file >> data[i];
//modulus of password size is taken so it takes value from password only
// like if password array is of size 4 only it will produce index as
// 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3
output_file << char(data[i]^password[i % password.size()]);
}
}
int main(int argc, char const *argv[])
{
encrypt_file("file", "ciao");
return 0;
}