C ++加密文本文件,允许通过ifstream使用解密 [英] C++ Encrypt a text file, allow use of decrypt via ifstream

查看:140
本文介绍了C ++加密文本文件,允许通过ifstream使用解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种非常简单的方法来使用密码对文本文件(在我的示例中为.data文件)进行加密并保存加密后的文件.

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

检索数据后,我应该能够对数据进行某种处理,并在ifstream中使用它,这就是我现在用来解析文本数据文件的方式,如下所示:

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();

在C ++中快速执行此操作的最佳和最简单的方法是什么?无需任何外部库就能完成吗?最好可以使用Windows Visual Express C ++中提供的标准C ++工具来完成某些事情,而无需链接任何新库.但是,如果您确实知道需要第三方代码的代码,则可以轻松快速地将其发布.

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.

推荐答案

一种非常简单(非常不安全)的加密方法是将密码短语与文本数据进行异或.解密是另一个具有相同密码短语的XOR.您将需要重复密码短语,直到文本数据结束.

A very simple (and very unsafe) method for encryption 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.

这篇关于C ++加密文本文件,允许通过ifstream使用解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆