请随身携带,因为我现在只用了几天的C++。阅读加密的用户名/密码
我正在开发一个充当日记的控制台应用程序。在这个阶段,我正在开发登录身份验证,并且打了一下墙!因为我将处理我的登录和日记存储的文本文件,所以我想从这些窥探中加密这些文本文件。
现在,问题是我不知道怎么去解密>>检查用户& & pass >>再次加密。
难道是沿着这些路线?:
程序加载
解密password.txt的
如果在任何时候程序关闭,encryptFile()是跑了。
验证用户输入
加密password.txt的
如果我沿着正确的线我怎么去实施呢?我搜索了使用C++的文本文件的加密教程,并且他们不是很有帮助。
这是我刚刚开始使用的初学password.txt代码,我应该从哪里出发?如果有加密教程/文章,建议我错过了,请发布!
void checkPasswordFile() {
string username;
string password;
string passwordAgain;
string userIn;
string passIn;
string line;
ifstream passwordFile("passwords.txt");
istringstream instream;
if (passwordFile.good()) {
cout << "\t==================================" << endl;
cout << "\t----------------------------------" << endl;
cout << "\tYou are a returning user, please fill in your details" << endl;
while(!passwordFile.eof()) {
getline(passwordFile, line);
instream.clear();
instream.str(line);
username = line;
getline(passwordFile, line);
instream.clear();
instream.str(line);
password = line;
}
do {
cout << "Username: " << endl;
cin >> userIn;
cout << "Password: " << endl;
cin >> passIn;
if (userIn == username && passIn == password) {
displayMenu();
} else {
cout << "Username and Password Do Not Match, Try Again" << endl;
}
} while(userIn != username && passIn != password);
} else {
cout << "file no exist";
ofstream passwordFile;
passwordFile.open ("passwords.txt", ios:: in | ios::app);
cout << "\t==================================" << endl;
cout << "\t----------------------------------" << endl;
cout << "\tThis is your first run, please enter a username and password" << endl;
cout << "\tUsername: " << endl;
cin >> username;
cout << "\tPassword: " << endl;
cin >> password;
/*
Do Loop:
Prompts Re-Entry if PasswordAgain is not equal to Password
*/
do {
cout << "Re-Type Password: ";
cin >> passwordAgain;
if(password != passwordAgain) {
cout << "Passwords Do Not Match! Try Again" << endl;
}
} while(password != passwordAgain);
passwordFile << username << "\n";
passwordFile << password;
}
}
非常感谢您的宝贵时间。
PS为我的生活,我不能找出如何做的:
用户名:CIN >>用户名]在同一控制台线路,对不起增加了一倍,但认为没有一个足够大问题为自己的职位!谢谢。
编辑:
创建并存储在文本文件时,我已成功能够解密的用户名和传递。然后,当用户回来时,他们输入的内容被加密并与文件进行比较。
问题是这只适用于简短的话,用户通过工程,但用户名和密码不......任何想法为什么?这里是我的加密代码:
char encryptKey = 'h';
cout << "\tUsername: ";
cin >> userIn;
cout << "\tPassword: ";
cin >> passIn;
for (int i = 0; i < userIn.size(); i++) {
userIn[i] ^= encryptKey;
}
for (int x = 0; x < passIn.size(); x++) {
passIn[x] ^= encryptKey;
}
if (userIn == username && passIn == password) {
displayMenu();
} else {
cout << "\tUsername and Password Do Not Match, Try Again" << endl;
}
作为一项建议,如果您从未将解密文件写入磁盘将会更好。 – Wug 2012-07-12 23:22:39
假设你的二进制文件需要包含加密密钥,所以你的应用程序对于任何知道如何使用调试器并阅读asm的人来说都不会真的是安全的。 – ildjarn 2012-07-12 23:25:56
我只是在我的第一个星期的C++,这是一个个人项目,以提高知识,所以我很好,我只需要加密.txt文件,以便用户不能只浸入程序以外的文件。 – speak 2012-07-12 23:29:31