PHP Crypt 无法运行 Ubuntu PHP 5.3.6

发布于 2025-01-04 15:06:26 字数 671 浏览 0 评论 0原文

为什么 crypt 值在 Ubuntu PHP 5.3.6 上不匹配?在其他系统上,它们匹配。

示例代码:

<?php

$password = '12345';

$saltString = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
$salt = '_';
while (strlen($salt) < 9)
    $salt .= substr($saltString, rand(0, strlen($saltString)-1), 1);
$cryptedPassword = crypt($password, $salt);

printf("Password: %s\n", $password);
printf("Crypted Password: %s\n", $cryptedPassword);

$cryptCompare = crypt($password, $cryptedPassword);

printf("Crypted Password Comparison: %s\n", $cryptCompare);

?>

Password: 12345
Crypted Password: _8OixMoOTyONAZDOiHbs
Crypted Password Comparison: _8IK4dGYmlkVo

Why do the crypt values not match on Ubuntu PHP 5.3.6? On other systems, they match.

Sample code:

<?php

$password = '12345';

$saltString = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
$salt = '_';
while (strlen($salt) < 9)
    $salt .= substr($saltString, rand(0, strlen($saltString)-1), 1);
$cryptedPassword = crypt($password, $salt);

printf("Password: %s\n", $password);
printf("Crypted Password: %s\n", $cryptedPassword);

$cryptCompare = crypt($password, $cryptedPassword);

printf("Crypted Password Comparison: %s\n", $cryptCompare);

?>

Password: 12345
Crypted Password: _8OixMoOTyONAZDOiHbs
Crypted Password Comparison: _8IK4dGYmlkVo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南烟 2025-01-11 15:06:26

我相信 crypt 应该返回添加到返回值前面的盐值。在某些实现中,它显然只有 2 个字节(您可以使用常量 CRYPT_SALT_LENGTH 检查它)。从OP中打印的输出来看,两个“加密”字符串的相似性仅限于前两个字节。也许实现有缺陷,使用了两个以上的字节作为盐,但只返回结果中盐的前两个字节。如果是这样,那就可以解释其中的差异。您可以通过简单地将盐长度设置为 2 来进行测试。

话虽如此,您可能需要考虑使用不同的哈希函数。我对 PHP 知之甚少,但谷歌搜索似乎表明 crypt 已经过时并且不太安全。例如,这是一篇这样的帖子

I believe that crypt is supposed to return the salt value prepended to the front of the return value. In some implementations it is apparently only 2 bytes (you can check it with the constant CRYPT_SALT_LENGTH). From looking at the output printed in the OP, the similarity in the two "encrypted" strings is limited to the first two bytes. Perhaps the implementation is flawed and uses more than two bytes for the salt but only returns the first two bytes of the salt in the result. If so, that would explain the difference. You could test that by simply setting the salt length at 2.

Having said that, you might want to consider using a different hashing function. I know very little about PHP, but a bit of googling seems to indicate that crypt is obsolete and not very secure. For example, this is one such post.

荒岛晴空 2025-01-11 15:06:26

也许您的系统不支持您当前的哈希类型。为什么不尝试不同的哈希类型呢?
http://php.net/manual/en/function.crypt.php

Perhaps your system doesn't support your current hash type. Why not try a different hash type?
http://php.net/manual/en/function.crypt.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文