ds11111111111111111 2011-10-06 20:19
浏览 66
已采纳

试图围绕PHP密码salt /加密

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.

  • 写回答

2条回答 默认 最新

  • duanqiu3800 2011-10-06 20:31
    关注

    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

    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['pass']);
    $pass_hash = md5($SALT.$password);
    mysql_query(*query to insert $username and $pass_hash into db*)
    

    To check if the password is correct

    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['pass']);
    $res = mysql_query(*query to extract $pass_hash from db where username==$username)
    //get the password from the $res and put it in a var
    if(md5($SALT.$pass_hash_from_db) == $password){*correct pass*} else {*invalid login*}
    

    Set $SALT to some large random static string such as $SALT="WEHGFHAWEOIfjo;cewrxq#$%";

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题