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

C++ Cracking an Encrypted File (You can only use the headers #include <fstream>

ID: 3890359 • Letter: C

Question

C++ Cracking an Encrypted File (You can only use the headers

#include <fstream>

#include <string>

#include <algorithm> )

5) Required function: std::string crack file(const std::string& originalFilename, const std:string& encryptedFilename) Your friend was using their email password to encrypt a file using your software and then later forgot their password. Fortunately, they still have the original file as well as the encrypted file created with that password. You have an idea to save the day. If you XOR each bit, pairwise, of the two files you would be able to recover the string containing (repeated copies) of the password they used to encrypt the file The function crack file should take two files and then determine the password used to encrypt the file. To complete the functionality Load each file in binary mode Compute the XOR between the two files by computing the XOR between the respective bytes of each file Determine the password used to encrypt the file . Return the string containing the password (exactly one copy of the password). You will have to think about how to process the string to find the password Assumptions you can make for crack file(... The files referred to by originalFilename and encryptedFilename exist. The files referred to by originalFilename and encryptedFilename are not empty The files referred to by originalFilename and encryptedFilename are of the same length. The password is the first repeated text that you find (so if you see "passwordpassword" the password would simply be "password" and not the repeat. If there is no repeated text, then take the entire string as the password (even if you know the password was longer). For example, suppose a file of 4 bytes was encrypted with the password "password" using our method. When you go to recover the password, you will only be able to recover "pass" (4 bytes) since not all bytes from the original password could be used You have read permission on the directory

Explanation / Answer

#include<iostream>

#include<fstream>

#include<string>

#include<algorithm>

using namespace std;

string crack_file(const string originalFilename, const string encryptedFilename);

int main()

{

cout << "Password cracked = " << crack_file("OriginalFile.bin", "EncryptedFile.bin") << endl;

}

string crack_file(const string originalFilename, const string encryptedFilename)

{

char data[16];

string password;

char str[17];

ifstream in;

in.open(originalFilename, ios::in | ios::binary);

if (!in)

{

cout << "Error while reading input fil,pls check" << endl;

return "";

}

if (!in.eof())

{

in.read(data, 16);

}

//now read password from ecncrypted file

ifstream in1;

in1.open(encryptedFilename, ios::in | ios::binary);

if (!in1)

{

cout << "Error while reading input fil,pls check" << endl;

return "";

}

int i = 0,j=0;

char str2[17];

while (!in1.eof())

{

in1 >> str[i];

if (i != 0 && str[i] == str[0])

{

//password repeated ,, so dont read remaining chars

str2[0] = str[i];

j = 1;

while (!in1.eof())

{

in1 >> str2[j];

if ( str2[j-1] == str2[0])

{

str2[j] = '';

str[i] = '';

if (strcmp(str, str2) == 0)

{

break;

}

else

strcat(str, str2);

}

j++;

}

str[i] = '';

break;

}

i++;

}

//data is 16 char long ..so put end of string char '' at 16th char

str[16] = '';

int CharAfterXor;

for (int j = 0; j < strlen(str); j++)

{

//subtract 48 as 0 is stored as ascii value 48

CharAfterXor = str[j] ^ (data[0]-48) ;

password.push_back(CharAfterXor);

}

//cout << "Password= " << password << endl;

return password;

}

---------------------------------------------------------------------------------------------------------------

//output

Password cracked = cse250

//password not repeated

Password cracked = cse250l67hgnjknq