尝试了解 PHP 密码盐/加密
现在我正在开发一个允许在线注册的应用程序。对于开发来说,密码检查仅检查 MySQL 行以确保该值与输入字段中的值匹配。
此代码检查该行是否存在:
$res = mysql_query("SELECT * FROM `users` WHERE `username` = '".$username."' AND `password` = '".$password."'");
$num = mysql_num_rows($res);
//check if there was not a match
if($num == 0){
//if not display error message
echo "<center>The <b>Password</b> you supplied does not match the one for that username!</center>";
我对实现盐系统感到困惑。我将如何更改此脚本来检查加密的密码?我还没有找到详细解释这一点的优秀教程。
Right now I am developing an application that will allow online registration. For development, the password check just checks a MySQL row to make sure the value matches the value in the input field.
This code checks to see that the row exists:
$res = mysql_query("SELECT * FROM `users` WHERE `username` = '".$username."' AND `password` = '".$password."'");
$num = mysql_num_rows($res);
//check if there was not a match
if($num == 0){
//if not display error message
echo "<center>The <b>Password</b> you supplied does not match the one for that username!</center>";
I'm confused about implementing a salt system. How would I alter this script to check for the encrypted password? I haven't found a great tutorial that explains this in detail.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
盐是在加密和解密之前添加到密码开头或结尾的一组字符,以使其更难以运行暴力攻击。
您首先创建盐,它只是一系列随机的固定字符,然后将其添加到密码之前,然后对其进行哈希处理。您还应该在将数据放入查询之前对其进行转义,以防止 MySQL 注入攻击。
将通行证输入数据库时执行此操作
检查密码是否正确
将 $SALT 设置为一些大型随机静态字符串,例如 $SALT="WEHGFHAWEOIfjo;cewrxq#$%";
A salt is a set of characters added to the beginning or end of the password before encryption and unencryption to make it harder to run a brute force attack.
Your first create the salt which is just a random fixed series of characters and then prepend it to the password before hashing it. You should also escape your data before putting it into the query to prevent MySQL injection attacks.
Do this when entering the pass into the database
To check if the password is correct
Set $SALT to some large random static string such as $SALT="WEHGFHAWEOIfjo;cewrxq#$%";
如果用户注册并将其保存在数据库中,您将必须生成新的盐。
当某个用户想要登录时,您检查该密码是否是该用户的密码列。
注:
- encFunction 是你的加密函数
You would have to generate a new salt if the user registers and save it in your database.
and when some user wants to logint you check if this password is a the password column of this user.
Note:
- encFunction is your encryption function