Reputation: 305
I'm looking for a very simple method of encrypting a text file (in my example is a .data file) using a password and saving the encrypted file.
The file that was saved will then be loaded into my application and decrypted using the same password. I then need a way to somehow get a handle to the decrypted data without the need to saveit to a file (I don't want users to see the decrypted data, only the program will have it and process it and then it will be discarded on program end).
After the data is retrieved, I should be able to get some sort of handle to the data and use it in an ifstream which is what I use now to parse my text data files as seen here:
string line;
string filename = "hello.data";
ifstream myfile(filename); // <-- instead of providing filename directly,
//^^I'd need a handle to the decrypted data.
while ( getline (myfile,line) ){
parse_line(line);
}
myfile.close();
What is the best and easiest method to do this quickly in C++? Can it be done without any external libraries? Just something that can be done preferably using standard C++ tools available in Windows Visual Express C++ without the need to link any new libraries. However if you do know of one that requires third party code, please post it if you feel it's easy to learn quickly.
Upvotes: 1
Views: 3920
Reputation: 395
A very simple (and very unsafe) method for encryption obfuscation would be to just XOR the password phrase with your text data. The decryption is another XOR with the same passphrase. You will need to repeat the passphrase until the end of the text data.
Note that this can be easily cracked but it provides a rudamentary layer of encryption obfuscation.
Upvotes: 1
Reputation: 8484
Something safer than XORing password with data would be going for RSA encryption using POCO libraries.
The RSA keys can be generated this way:
//generate private key:
openssl genrsa -des3 -out private_rsa.pem 1024
// generate public key:
openssl rsa -in private_rsa.pem -pubout -out public_rsa.pem
Public key would be used to encrypt the data and then encrypted data stored in the file:
void encrypt_file()
{
Poco::Crypto::CipherFactory &factory = Poco::Crypto::CipherFactory::defaultFactory();
Poco::Crypto::Cipher* pCipher = factory.createCipher(Poco::Crypto::RSAKey("./public_rsa.pem"));
Poco::Crypto::CryptoTransform *pEncryptor = NULL;
pEncryptor = pCipher->createEncryptor();
Poco::FileOutputStream sink("./encrypted.bin");
Poco::Crypto::CryptoOutputStream encryptor(sink, pEncryptor);
Poco::FileInputStream source("input.txt");
Poco::StreamCopier::copyStream(source, encryptor);
// Always close output streams to flush all internal buffers
encryptor.close();
sink.close();
}
Private key could be read from file or hard-coded:
void decrypt_file()
{
std::istringstream is("-----BEGIN RSA PRIVATE KEY-----\n"
"Proc-Type: 4,ENCRYPTED\n"
"DEK-Info: DES-EDE3-CBC,0F9006C4519B55C8\n"
"\n"
"rRLQeGPaa8iqc4ke+fxDmCvgfdsgfdsgf55343ggynqDpmhGd29iBed4N1Xovdiw\n"
"G87l0Uco+ZhsriLPjWBdTmr14HrBxJEJybXucjx1h4WLqMd1ro0QY2QlojJ337Sq\n"
"LFqcLc1nSW3levjkFIDSpFjnPbaDk/t/1xQEh3VHWOGHa+IVSDKTkw2uyiKO7bh+\n"
"W6MCbXnaJIS0/6ouoJgnK7COrS/0Hqo5z0wLY9ZCarLeVOYMK+YamhXrSz5sLElI\n"
"2ysC5kLxhWBZOTiGOc1aPh6svWmFg0I1Eil+PVTR3XR6L/b8LY/BQMh0OJ6uwdvp\n"
"YfgzdvxqDVbCjw1dNJjgfvegfdgdDDlzQfFsXGf1p9OY0jElL/egVTGP1YLHgMb6\n"
"zJDUZmgC2PJBOB/KWJF09k0vDfdr/t32OXE9vMPAJeJ2TwecnmvYiLbA5uu93bvi\n"
"DEo9V+F7ltMS2XQld9kal4dHPE1NdCMBx5oY8Bi+Qf9rXUdO/0JxZIY0j+0pWGZa\n"
"7iZWriyme4zxGFQJXD8hV4AW7NNUUff3bCkkmYyYOyV11ybWJGGOBk6IJCcuzFoq\n"
"S94LfFMBtcmXQmUXcQwacIDzAEivmk0Uxz1bMmcu+wNEIquLx9wEZWlll6P88JPv\n"
"E58HIXKB9AVEJZ5gfdgfdfgregd4345grgdgfs0qbR3v4qcNCKWlihd36MT+0QD+\n"
"HCoakmTbbPXdUC8HcppB7D9nhjmbuXcneu8sf/zUrDHcwBbHR7T0U63LE2gdzfOc\n"
"vg6XAhXMRApLYydfsf44sgg4w4wsAcXMJpxNcz+JXG14QcBGJ1Ot4dGbTCVoww==\n"
"-----END RSA PRIVATE KEY-----\n"
"\n");
Poco::Crypto::CipherFactory &factory = Poco::Crypto::CipherFactory::defaultFactory();
Poco::Crypto::Cipher* pCipher = factory.createCipher(Poco::Crypto::RSAKey(NULL, &is, "my password"));
// uncomment to read private key from the file
//Poco::Crypto::Cipher* pCipher = factory.createCipher(Poco::Crypto::RSAKey("", "./private_rsa.pem", "my password"));
std::ifstream in("./encrypted.bin", std::ios::binary|std::ios::in);
if(!in.is_open())
return;
std::string data;
data.append(std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>());
in.close();
const std::string decrypted_string(pCipher->decryptString(data));
std::cout << "decrypted string: " << decrypted_string << std::endl;
}
Upvotes: 1