Hi,
Why can't we xor text with binary file?
When I xor it back half of them are not the same, garbage characters.
Code:
#include<fstream>
#include<iostream>
#include<string>
using namespace std;
string XOR(string value,string key);
void OpenKeyFile();
string value ("The quick brown fox jumps over the lazy dog");
const char * key_file = "C:\\Windows\\System32\\appinfo.dll";
int length_of_command = 200;
char secret_key[520] = "";
int main()
{
OpenKeyFile();
string key(secret_key);
cout<<"Plain text: "<<value<<"\n\n";
value=XOR(value,key);
cout<<"Cipher text: "<<value<<"\n\n";
value=XOR(value,key);
cout<<"Decrypted text: \n"<<value<<endl;
return 0;
}
void OpenKeyFile()
{
fstream ifs(key_file, fstream::in | fstream::binary);
char a;
if(!ifs.is_open())
{
cout << "Couldnt open input file "<<key_file<<endl;
return;
}
//
ifs.seekp(0);
// write to ifs
ifs.read(secret_key, length_of_command);
// cout << vIn << endl;
ifs.close();
}
string XOR(string value,string key)
{
string retval(value);
short unsigned int klen=key.length();
short unsigned int vlen=value.length();
short unsigned int k=0;
short unsigned int v=0;
for(v;v<vlen;v++)
{
cout << value[v] << " - " << key[k] << "\n";
retval[v]=value[v]^key[k];
k++;
}
return retval;
}