Okay, so I'm having a little bit of a problem here.
What I'm doing is converting a binary file (in this example I used a .exe file) to a Base64 string and then converting this one back to binary data to write it to the disk.
So far so good, this code works:
std::string str = base64_decode(base64str); // base64str is the base64 string made of the actual .exe file
std::ofstream strm("file.exe", std::ios::binary);
strm << str;
strm.close();
The file "file.exe" is being created as expected and I can run it.
Now my problem is that I need the decrypted file as char* instead of std::string, but whenever I call this code
str.c_str();
to either convert it to const char* or char* the contents suddenly no longer equal the binary data contained in str, but rather this:
MZP
So, for instance the following code
std::string str = base64_decode(base64str);
std::ofstream strm("file.exe", std::ios::binary);
char* cstr = new char[str.length()-1];
strcpy(cstr, str.c_str());
strm << cstr;
strm.close();
would create file.exe, but this time it would contain "MZP" instead of the actual binary data
I have no clue on how to fix this. Of course the char* is mandatory.
Can any of you help?
strcpy(cstr, str.c_str());will stop copying after hitting the first null byte, of which there are probably hundreds in your binary file.strm << cstrit will stop at the first null. Usestrm.write().