PHP 的 Crypt() 中的精度损失?相同的盐,不同的密码=相同的加密结果

Precision loss in PHP's Crypt()? Same salt, different passwords = same encrypted result

我在 PHP 中使用 Crypt() 来加密密码。 假设盐是 "bg"、
密码是:"gg456456gg"

加密结果给出:"bgvQk9C2Pv27o"
但是如果我使用 password: "gg456456" - 没有最后两个字符,它会给出相同的结果。

因此,用户无需输入 100% 准确的密码即可登录。

发生什么事了?我的意思是gg456456和gg456456gg是两个不同的密码,为什么加密结果一样?

Php.net on function crypt()

The standard DES-based crypt() returns the salt as the first two characters of the output. It also only uses the first eight characters of str, so longer strings that start with the same eight characters will generate the same result (when the same salt is used).

所以使用不同的加密方法。 例如河豚或 sha-512。这些将接受更长的字符串

例如SHA-512:

$encpassword = crypt($password,"$".$salt);

使用上述方法(和相同的盐):

gg456456 -> $6$631080661$L2o7HNKfYrqB4H19vYe7fRWWLenQj2EcWqriNG9rX6ki1QKO2YytkylrYmZ8mhIr6XE19Ms4RW2of5Z/dsYRA/

gg456456gg -> $6$631080661$maGxQ2d7ZIPIdXDFN1sJJsIjTFEwD9dL/uljSXdKXeJU4E5miCzh1ZCao57sGDm9PrDhdPYPLGUvoy0HzTfqI.

为你的盐使用一个好的随机数生成器,瞧,你有一个加密良好的密码

原来Unix系统的crypt函数只使用密码的前8个字符。最终我们认为这是不安全的,并已切换到更安全的密码哈希。

PHP crypt 函数根据您提供的 salt 选择要使用的算法,并且像您使用的两个字符的字母数字 salt 会触发原始 crypt 算法。

有关算法和相应盐的列表,请参阅 http://php.net/manual/en/function.crypt.php