简体   繁体   English

C ++:从文件/ UserName和密码读取

[英]C++: Reading from file /UserName and Password

I am doing a program that will prompt a user to login in using their username and password. 我正在做一个程序,提示用户使用其用户名和密码登录。 I have a textfile(userandpassword.txt) that contains a list of username and password. 我有一个包含用户名和密码列表的文本文件(userandpassword.txt)。 so what happens is, the user input their username and password, and their input will be matched with the username and password in the textfile. 因此,发生的情况是,用户输入了用户名和密码,输入的内容将与文本文件中的用户名和密码匹配。 if it matches, they can login and if it doesn't match, it will print out "Invalid username or password". 如果匹配,则他们可以登录,如果不匹配,则将打印出“无效的用户名或密码”。 below is what I have. 以下是我所拥有的。

textfile(userandpassword.txt) 文本文件(userandpassword.txt)

john abcd
mary efgh
jane ijkl

my codes 我的密码

string line = " ";
ifstream readFile("userandpassword.txt");
string UserName;
string Password;
string _UserName;
string _Password;

cout << "Enter UserName: ";
cin >> UserName;

cout << "Enter Password: ";
cin >> Password;

while (getline(readFile,line)) {

    stringstream iss(line);
    iss >> _UserName >> _Password;

    if (UserName == _UserName && Password == _Password) {
        cout << "Login Successfully!"<< endl;
    }

    else {
        cout << "InValid UserName And Password"<< endl;
    }

}

The problem is if I key in username: john and password: abcd it will return me the output as shown below 问题是如果我输入用户名:john和密码:abcd,它将返回我如下所示的输出

Login Successfully!
InValid UserName And Password
InValid UserName And Password 

whereas if I key in username: mary and password: efgh it will return me the out as shown below 而如果我输入用户名:mary和密码:efgh,它将返回我,如下所示

InValid UserName And Password
Login Successfully!
InValid UserName And Password

and finally username: jane and password: ijkl 最后是用户名:jane和密码:ijkl

InValid UserName And Password
InValid UserName And Password
Login Successfully!

but this is not the result I wanted, what I wanted is it reads through the file line by line for the username and password and if matches with the user's input , It will just output "Login Successfully" and same goes for if it does not match , it will just print out "Invalid Username and password". 但这不是我想要的结果,我想要的是它逐行读取文件中的用户名和密码,如果与用户的输入匹配,它将只输出“成功登录”,如果没有,则输出相同的结果match,它将只打印出“无效的用户名和密码”。

eg 例如

if I key in username: mary and password: efgh, my expected output 如果我输入用户名:mary和密码:efgh,我的预期输出

Login Successfully!

and if I key in any invalid username and password , my expected output 如果我输入任何无效的用户名和密码,我的预期输出

Invalid Username and password.

Any ideas or suggestions on how should I achieve my expected output? 关于如何实现预期输出的任何想法或建议? Thanks 谢谢

Add a break after you found a correct line and output the invalid-message behind the while loop: 找到正确的行后添加一个break ,并在while循环后面输出无效消息:

bool found = false;
while (getline(readFile,line)) {

    stringstream iss(line);
    iss >> _UserName >> _Password;

    if (UserName == _UserName && Password == _Password) {
        cout << "Login Successfully!"<< endl;
        found = true;
        break;
    }
}

if (!found) {
    cout << "InValid UserName And Password"<< endl;
}

Edit: By the way. 编辑:顺便说一句。 It is not such a good idea to safe any password credentials in plain text files. 保护纯文本文件中的所有密码凭据不是一个好主意。 You should safe a hash-value (salted) of the password, hash the user input, too and compare the hashes. 您应该保护密码的哈希值(加盐),也对用户输入进行哈希处理,并比较哈希值。 This way, even if someone gets the file, he will have to break the hashing algorithm to find the actual password. 这样,即使有人获取了文件,他也必须破坏哈希算法才能找到实际的密码。

Edit2: A hash can be generated by one of the well-testes hash-functions available (sha-1, sha-3 eg). Edit2:哈希可以通过可用的经过良好测试的哈希函数之一生成(例如sha-1,sha-3)。 This choice depends on how secure it has to be and on the avaiability of code to calculate such a hash. 这种选择取决于它必须具有的安全性以及计算这种哈希的代码的可用性。 The general process would be (pseudo-code): 一般过程为(伪代码):

cin >> username;
for-each-row in file do:
    read username-in, salt, password-hash-in
    if (username-in == username && hash(salt, password) == password-hash-in):
        success!

The salt is a small piece of data that is appended or somehow integrated into the password and is randomly generated while the password is stored to the file. 盐是一小段数据,已附加或以某种方式集成到密码中,并且在将密码存储到文件时随机生成。 This way, the hashes will be different - even if two users use the same password. 这样,即使两个用户使用相同的密码,哈希也将不同。 The input-password has to be hashed with the same salt, this is why the salt has to be read from the file. 输入密码必须使用相同的盐进行哈希处理,这就是为什么必须从文件中读取盐的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM