使用 PHP 的 DES ECB 加密

DES ECB encryption with PHP

https://www.tools4noobs.com/online_tools/encrypt/ 给出 "a67a318c98a0307502ba81caade2f3a9" 作为密钥“1234567890abcdef”和有效载荷 "encrypt this".

的 DES ECB 结果

PHP代码

echo bin2hex(mcrypt_encrypt(
    MCRYPT_DES,
    hex2bin("1234567890abcdef"),
    "encrypt this",
    MCRYPT_MODE_ECB)) . "\n";

打印出“1a29ee87f2ad67644ff28450c676a664”。

代码有什么问题?

noobs4tools 网站删除了 hex2bin 函数并将密钥长度截断为 8 个字符(如 Yoshi 在评论中所述)。

使用 12345678 的密钥大小,网站和 PHP 代码的输出是一致的。

DES 密钥大小在手册中规定为 56 位。请阅读以下有关 DES 特定密钥大小的一些有用背景知识。

How should I create my DES key? Why is an 7-character string not enough?

noobs4tools 网站使用的密钥:

"12345678"

您的代码使用的密钥:

 hex2bin("1234567890abcdef"); // 4Vx����

这种差异会给你不同的输出。

因此网站不会将 key 翻译成任何其他数字或数据形式。它希望您在页面脚本中提供格式正确的值。