PHP AES-128-CBC 十六进制密钥编码

PHP AES-128-CBC encode with hexadecimal key

我必须使用 AES-128-CBC 对字符串 ($text) 进行编码,其中的密钥 ($key) 为十六进制(IV 为 0):

$text = "042878e2d35380";
$key = "\x66\x21\x10\x98\x3a\x2f\xd8\x7a\x4b\xb8\xb8\xc7\x54\xe6\xb9\x56";

我想要一个加密的字节数组,表示为一个字符串(不是 base64,只是一个带有十六进制值的普通字符串)。

这是我的失败尝试:

$ciphertext_raw = openssl_encrypt($text , "AES-128-CBC", $key , OPENSSL_RAW_DATA, "0000000000000000");
$hex = bin2hex($ciphertext_raw);
echo "cripted hex = ".($hex);

我得到的结果与我从 criptii (https://cryptii.com/pipes/aes-encryption) 得到的结果不同。使用它我得到: d5 70 1d 97 ed d7 64 0e 44 b7 76 90 2c ef 03 9e 是正确的。

有人可以告诉我我做错了什么吗?非常感谢。

您的 $iv 参数似乎有所不同。

$text = "042878e2d35380";
$key = "\x66\x21\x10\x98\x3a\x2f\xd8\x7a\x4b\xb8\xb8\xc7\x54\xe6\xb9\x56";

$iv = hex2bin('00000000000000000000000000000000');
$ciphertext_raw = openssl_encrypt($text , "AES-128-CBC", $key , OPENSSL_RAW_DATA, $iv);
$hex = bin2hex($ciphertext_raw);
echo "cripted hex = ".($hex);
// Outputs d5 70 1d 97 ed d7 64 0e 44 b7 76 90 2c ef 03 9e

// On the site is using these values:
$iv2 = hex2bin('000102030405060708090a0b0c0d0e0f');
$ciphertext_raw = openssl_encrypt($text , "AES-128-CBC", $key , OPENSSL_RAW_DATA, $iv2);
$hex = bin2hex($ciphertext_raw);
echo "cripted hex = ".($hex);
// Outputs 9a 0d c6 6e fc 6e 94 58 15 15 9a a2 be 35 4e 72