Php 使用crypt()进行加密

Php 使用crypt()进行加密,php,passwords,hash,Php,Passwords,Hash,我目前正在做一个非常安全的登录系统,但我对crypt()函数不熟悉,需要一些快速帮助 我在注册期间使用crypt()加密密码字符串,并将其保存到数据库中。但是,如何在登录期间解密密钥?或者我该怎么做呢?或者可能对提交的密码字符串进行一些处理,将其与数据库中的加密密钥进行比较 crypt()登录时提供的密码。将输出与以前的crypt()的输出进行比较。如果它们匹配,则密码匹配 这是单向散列函数操作的基本理论。crypt()不加密密码,而是对密码进行散列。根本的区别是,你无法取回散列密码(想想散列布

我目前正在做一个非常安全的登录系统,但我对crypt()函数不熟悉,需要一些快速帮助

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

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

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

crypt()
不加密密码,而是对密码进行散列。根本的区别是,你无法取回散列密码(想想散列布朗——如果你有散列布朗,你就无法取回土豆)

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

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

阅读密码,了解上面代码背后的“魔力”。

不要加密密码。相反,用散列存储它


流行的SO线程:

这也是你应该做的事情。请注意,这段代码就是我要做的,您可能需要更改其中一些内容。您必须定义自己的唯一salt,无论是在配置文件中还是在ElseWare中。它必须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;
}
/*
*版权所有(c)2012,麦克阿瑟Inbody
*以下代码发布在http://stackoverflow.com/questions/8195689/encryption-using-crypt
*该许可证只需抄送即可https://creativecommons.org/licenses/by/3.0/
*
*
*
*/
/*
*
*这是用来散列他们的密码。
*
*@param$password字符串用户提供的密码
*@param$username字符串用户提供的用户名
*@param$rand_salt必须在前面定义次盐-2^31-1到2^31-1。
*@return字符串哈希密码
*/
函数hash\u pass($username、$password、$rand\u salt){
全球$unique_盐;
$main_salt=base64_encode(散列('sha512',$username.$password.$config_salt));
$main_salt=str_replace(“+”、“.”、$salt);
$main_salt=str_replace(“=”、“/”、$salt);
$main_salt='$2$06'$main_salt;//在此处将其更改为所需的成本系数
$hashed=crypt($unique_salt.$username.$password.$rand_salt,$main_salt);
返回$hash;
}
函数gen_rand_salt(){
返回rand();
}
函数rand_str($length,$additional_entropy){
$max_length=ceil($length/28);
如果(!已定义($additional_entropy)){
$附加熵='';
}
$str='';
对于($i=0;$i请参见

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

在PHP7中,您应该使用

相反。有关早期版本,请参阅


不知何故,我怀疑您是否编写了一个“非常安全的登录系统”…但无论如何,祝您好运。
crypt()
不加密密码。非常感谢您的好意和帮助。我不确定为什么我不能让它与crypt()一起工作,因为我像你们大多数人说的那样做了。但现在我创建了一个自己的函数,它使用sha1对密码字符串进行散列,然后将其与salt组合,然后它实际开始工作。另外,我要为说“加密”而道歉:)祝您晚上愉快!@user973763
crypt
可能有点违反直觉,但是如果您阅读文档,您应该能够理解它。演示: