【问题标题】:PHP AES-128-CBC encode with hexadecimal keyPHP AES-128-CBC 使用十六进制密钥进行编码
【发布时间】:2019-11-13 02:41:04
【问题描述】:

我必须使用 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 是正确的。

谁能告诉我我做错了什么,好吗?非常感谢。

【问题讨论】:

    标签: php aes


    【解决方案1】:

    您似乎有一个不同的$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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-13
      • 1970-01-01
      • 2013-08-11
      • 2021-05-15
      • 1970-01-01
      • 2017-05-07
      • 1970-01-01
      • 2021-02-28
      相关资源
      最近更新 更多