gpt4 book ai didi

c++ - 读取加密的用户名/密码

转载 作者:太空宇宙 更新时间:2023-11-04 12:07:23 26 4
gpt4 key购买 nike

我正在开发一个充当日记的控制台应用程序。在这个阶段,我正在开发登录身份验证,但遇到了一些困难!由于我将处理用于登录和日记存储的文本文件,因此我想对这些文本文件进行加密以防窥探。

现在,问题是我不知道如何进行解密 >> 检查用户&&密码 >> 再次加密。

它会沿着这些路线吗?:

  • 程序加载

  • 解密passwords.txt

  • 如果程序在任何时候关闭,就会运行 encryptFile()。

  • 验证用户输入

  • 加密passwords.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;
}

}

非常感谢您的宝贵时间。

p.s 对于我的一生,我不知道该怎么做:

Username:[cin>>username] 在同一个控制台行上,很抱歉加倍了,但不认为这是一个足够大的问题来发表自己的帖子!谢谢。

编辑:

我已成功解密用户名并在创建并存储在文本文件中时通过。然后当用户回来时,他们输入的内容将被加密并与文件进行比较。

问题是这只适用于短词,用户密码有效,但用户名和密码无效......有什么想法为什么?这是我的加密代码:

 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;
}

最佳答案

正确的做法是不要加密密码文件 - 问题是文件的加密 key 需要存储在程序可以访问的某个地方,这将使它相对容易找到和滥用。

相反,您应该使用密码散列(使用 SHA1 等强散列算法)。哈希算法是一种确定性地将一段文本映射到一个大数字(称为其哈希)的算法,并且被设计为无需付出很大努力就不可逆。基本概念是您获取密码,使用它来计算其散列值,然后存储该散列值。稍后,当用户输入密码登录时,您会再次计算其哈希值并将生成的哈希值与存储的哈希值进行比较。即使有人获得了哈希值的访问权,他们也无法获得密码,这很重要,因为人们经常在应用程序之间共享密码。不要实现您自己的 SHA1 哈希 - 请参阅“What is the best encryption library in C/C++?”以获取库列表。

您还必须使用 saltingkey stretching抵御常见的暴力攻击。

关于c++ - 读取加密的用户名/密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11462129/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com