Data structure in C++ help needed!
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 0
Expert Answer
// istream::get example
#include <iostream> // std::cin, std::cout
#include <fstream> // std::ifstream
#include<cstring>
void encrypt_file(const std::string& filename,const std::string& password)
{
char str[filename.size()];
int i;
for(i=0;i<filename.size();i++)
{
str[i]=filename[i]; //storing file name to char array str..
}
std::ifstream is(str); // open file
char c;
int k=0,n = password.size();
while (is.get(c)) // loop getting single characters
{
if(k==n)
k=0;
c = c ^ password[k];//repeatedly performing xor
//printing encrypted string..
std::cout<<c;
k++;
}
std::cout<<“n”;
is.close(); // close file
}
int main () {
return 0;
}