Search code examples
c++fileencryptiontext

C++ Encrypt a text file, allow use of decrypt via ifstream


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.


Solution

  • 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.