使用crypt()加密

时间:2011-11-19 17:04:33

标签: php passwords hash

我目前正在做一个非常安全的登录系统,但我是crypt()函数的新手,需要一些快速帮助。

我在注册期间使用crypt()加密密码字符串并将其保存到数据库中。但是,如何在登录期间解密密钥?或者我该怎么办呢?或者是否可能使用提交的密码字符串进行一些魔术以将其与数据库中的加密密钥进行比较?

5 个答案:

答案 0 :(得分:2)

crypt()不加密密码,哈希密码。根本区别在于,您无法获得哈希密码(想想哈希棕色 - 如果您有哈希棕色,则无法获取土豆)。

因此,您将相同的函数应用于输入,并将其结果与存储在数据库中的值进行比较:

$stored_pw = get_hashed_password_from_db($_POST['username']);
crypt($_POST['password'], $stored_pw) == $stored_pw

阅读documentation on crypt()以了解上述代码背后的“魔力”。

答案 1 :(得分:1)

crypt()登录时提供的密码。将输出与先前的crypt()输出进行比较。如果匹配,则密码匹配。

这是单向散列函数的基本操作理论。

答案 2 :(得分:1)

不要加密密码。相反,请使用哈希值存储它。

热门SO帖子:How should I ethically approach user password storage for later plaintext retrieval?

答案 3 :(得分:0)

这也是你应该这样做的。请注意,这段代码是我如何做的,你可能想要改变一些事情。而且你必须定义自己独特的盐,无论是在配置文件还是elsehwere。它必须a)在我发布的全局范围内,或者您可以更改它以使其在函数中定义。你也没有加密,实际上是哈希。加密是两种方式,哈希是以一种方式加密。意思是你无法解密哈希。你只能粗暴地猜测原始纯文本。

/*
*   Copyright (c) 2012, Macarthur Inbody
*   The following code was posted on http://stackoverflow.com/questions/8195689/encryption-using-crypt
*   The license is simply CC-by https://creativecommons.org/licenses/by/3.0/
*
*
*
*/


/*
 *
 * This is used to hash their password.
 *
 * @param   $password       string      the users supplied password
 * @param   $username       string      the users supplied username
 * @param   $rand_salt      int         the secondary salt -2^31-1 to 2^31-1 Must be defined previously.
 * @return  string the hashed password
 */
function hash_pass($username,$password,$rand_salt){

    global $unique_salt;
    $main_salt=base64_encode(hash('sha512',$username.$password.$config_salt);
    $main_salt=str_replace('+', '.', $salt);
    $main_salt=str_replace('=','/',$salt);
    $main_salt='$2$06'.$main_salt; //change this here to the cost factor that you want
    $hashed=crypt($unique_salt.$username.$password.$rand_salt,$main_salt);
    return $hashed;
}

function gen_rand_salt(){
    return rand();
}

function rand_str($length,$additional_entropy){
    $max_length=ceil($length/28);
    if(!is_defined($additional_entropy)){
        $additional_entropy='';
    }
    $str='';
    for($i=0;$i<=$max_length;++$i){
        $str.=base64_encode(sha1($i.''.microtime().$additional_entropy,true));
    }
    $str=substr($str,0,$length);
    return $str;
}

/*
*
* Generate A temp password/token
*
* This function generates a temporary password and also gives you
* the hashed password too. It is an array, arr[0]=password, arr[1]=
* hashed password. If it fails it'll return -1;
*
* @param    $username   the username
* @param    $rand_salt  the random salt value, must be given.
*
* @return   array       if it is successful array, if it fails it's a number of -1
*/ 
function generate_temp_password($username,$rand_salt){
    global $unique_salt;
    if(!is_defined($rand_salt)){
    return -1;
    }
    $pass_len=12; // change this to what you want for password recovery
    $pass_arr=Array();
    $password=rand_str($pass_len,$unique_salt.rand().$rand_salt);
    $password=substr(base64_encode(sha1($rand_str.$rand_salt,true)),0,$pass_len);
    $hashed_password=hash_pass($username,$password,$rand_salt);
    $pass_arr[0]=$password;
    $pass_arr[1]=$hashed_password;
    return $pass_arr;
}

正如代码中所述,许可证是CC-By,因为我觉得它对大多数事情来说已经足够了。另外,请保持块与此页面的链接相同,因为这是我对所有自己的源代码所做的。此外,我意识到“随机”字符串并非真正随机,但它足够随机,可以被你用于它的目的。

编辑2:还要确保转义用户的用户名。我没有忘记密码,因为我正在对它进行哈希处理,因此它没有必要转义,因为它已经被缓解并且只会浪费周期。但是只有如果您正在做这样的事情。请务必使用mysql_real_escape_string转义用户名。如果您使用的是php5 +,您应该查看mysqli(如果您使用的是mysql)。如果你正在使用另一个系统,那么你必须自己查找它,因为我只知道mysql。我要离开几天,所以我真的希望这适合你。我会不时检查一下,但我可能会忘记......所以是的。我希望这对你有所帮助,因为它安全,可靠,并且应该对你有用。

编辑3:更改了随机字符串功能,使其有点更强,因为我忘了这将用于生成临时密码。这应该使它足够随机以用于此目的,因为否则生成的密码可能能够被知道准确时间的人知道(使用当前的微时间)虽然极不可能,这仍然使它更强一点并且应该使它免受这些攻击。它不应该是完全生产就绪,应该对您的系统是安全的。只需确保在全局范围内设置$ unique_salt变量somwhere,或在每次在每个函数中使用它时设置它。

答案 4 :(得分:0)

http://php.net/manual/en/function.password-hash.php

http://php.net/manual/en/function.password-verify.php

如果您需要安全的随机值,也不要使用rand()。它是PHP中随机值的最差来源。

在PHP 7中,你应该使用

http://php.net/manual/en/function.random-bytes.php

代替。对于早期版本,请参阅

http://php.net/manual/en/function.openssl-random-pseudo-bytes.php