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: 3888489 • 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

Given below is the code for the crack_file() function. Hope the answer helps. If it did, please don't forget to rate the answer. Thank you.

#include <fstream>
#include <iostream>
using namespace std;
string crack_file(string originalFilename, string encryptedFilename);
int main()
{
string orignalFname, encryptedFname;
cout << "Enter original filename: ";
cin >> orignalFname;
  
cout << "Enter the encrypted filename: ";
cin >> encryptedFname;
  
string password = crack_file(orignalFname, encryptedFname);
cout << "The password is " << password << endl;
}

string crack_file(string originalFilename, string encrptedFilename)
{
ifstream orig(originalFilename.c_str(), ios::binary);
ifstream encryp(encrptedFilename.c_str(), ios::binary);
  
if(orig.fail() || encryp.fail())
{
cout << "Failed opening the files." << endl;
return "";
}
char ch1, ch2, ch3;
string decrypted = "";
while(!orig.fail())
{
orig.read(&ch1, sizeof(char));
encryp.read(&ch2, sizeof(char));
ch3 = ch1 ^ ch2;
//cout << ch3;
decrypted += ch3;
}
orig.close();
encryp.close();
  
  
int sz = decrypted.size();
  
for(int i = 1; i < sz; i++ )
{
if(decrypted[i] == decrypted[0])
{
if(2*i <= sz) //if all hte characters till i can be searched, i.e full repetition of password
{
string s1 = decrypted.substr(0, i); //get th string starting from 0 to i-1
if(decrypted.find(s1, i) != string::npos) //look s1 starting at i, if its found, then s1 is the password
{
return s1;
}
}
else //if all i charactesr from index 0 can not be found at i, then try to match the small string starting at i to beginning of string, i.e only part of password is repeated 2nd time
{
string s1 = decrypted.substr(i); //get substring starting at i
if(decrypted.find(s1, 0) == 0) //if the decrypted message starts with the substring,
return decrypted.substr(0, i); //all characters from 0 to i form the password
}
}
}
return decrypted; //no repetions found, return the whole string
  
}

ouput

Enter original filename: input
Enter the encrypted filename: output
The password is cse250

============================

In order to test the program, I used anohter program to generated the original file called "input" and the encrypted file called "output". This program creates an original file with 16 bytes of and the encrypted file using cse250 as password. The files generated from this program are given as input to above program

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char ch = '';
string pwd = "cse250";
  
ofstream infile ("input", ios::binary);
ofstream outfile ("output", ios::binary);
for(int i=0, k=0; i <16; i++)
{
infile.write(&ch, 1);
char ch2 =ch ^ pwd[k++];
if(k == pwd.length())
k=0;
outfile.write(&ch2, 1);
  
}
  
infile.close();
outfile.close();
}