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

Please work on C++ You can ONLY use given #include code you are working on Resta

ID: 3889619 • Letter: P

Question

Please work on C++

You can ONLY use given #include

code you are working on

Restart your subscription to keep accessing solutions. Your Chegg Study subscription will expire on September 28, 2017. CONTINUE MY SUBSCRIPTION

home / study / engineering / computer science / computer science questions and answers / please work on c++ you can only use given #include code you are working on #include #include ...

Your question has been posted.

We'll notify you when a Chegg Expert has answered. Post another question.

Question: Please work on C++ You can ONLY use given #include code you are working on #include <fstream&g...

Please work on C++

You can ONLY use given #include

code you are working on

#include <fstream>
#include <string>
#include <algorithm>

void encrypt_file(const std::string& filename, const std::string& password) {
  
}

Test code(cannot change):

#include <iostream>

int main() {
std::string testFilename;

// Tests your encrypt_file function on test01 through test09
for (int i = 1; i <= 9; ++i) {
testFilename = std::string("test0") + (char) (i + 48);
encrypt_file(testFilename + "-unzipped", "password");
if (cmp_files(testFilename + "-unzipped-encrypted", testFilename + "-unzipped-encrypted-expected")) {
std::cout << "Encryption was correctly performed for " << testFilename << "!" << std::endl;
} else {
std::cout << "Something went wrong encrypting " << testFilename << " :(" << std::endl;
return -1;
}
}

4) 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 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/) 00lo 101 110 A "bitwise" operator takes two variables and evaluates a logical operator on respective bits of the variables. Consider the following code CSE 250 Fall 2017 char xz'; / char code is 122 -> binary is 01111010 char y '!'. // char code is 33-> binary is 00100001 char z = x ^ y. I/ result of bitwise XOR: 01011011 x: 01111010 char w z ^ x; // the result is the value of y: 00100001 Observe above that the bitwise operator works by computing the logical operator between each pair of bits. This will also be true for larger types (int, double, etc.). Also observe that for any x and y we have x^y#x = y (and x^x^y = y and y^x^x = x). This property allows us a nice way to encrypt our data and then later recover the original data Then our algorithm to encrypt is quite basic. First load the file into a byte (char) array. Then, starting from the first byte of the file and the first character in the password, compute the XOR of the two bytes and move to the next byte. When you run out of symbols in the password, cycle back to the first character of the password. Repeat this process until each byte in the original file data has been XORed with its respective character from the password

Explanation / Answer

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <vector>

using namespace std; // specifying the namespace to be used

void encrypt_file(const std::string& filename, const std::string& password) {

ifstream ifs(filename.c_str(), ios::binary|ios::ate); /* opening the binary file and keeping the current position to the end */
ifstream::pos_type pos = ifs.tellg(); /* taking the end position in the variable pos. This will help in knowing the size of the file */
int length = pos; // length is the variable to hold the size of the file
std::vector<char> result(pos); // a vector to hold the byte wise file data

ifs.seekg(0, ios::beg);
ifs.read(&result[0], pos); // reading data from the file into the byte vector


char encryptedData[length+1]; // byte array to hold the final result after XORing the data

int passwordLength = password.size(); // a variable to hold length of the password


for(int i = 0;i<length; i++)
{
encryptedData[i] = result[i]^password[i%passwordLength]; /* handling the cyclic nature of password as mentioned in the question */
}

encryptedData[length] = '';
ifs.close();

// Now, we will write the data to the output file

ofstream fout;
string outputfilename = filename + "-encrypted";
fout.open(outputfilename.c_str(), ios::binary | ios::out);
fout.write((char*) &encryptedData, sizeof(encryptedData));
fout.close();

}