簡體   English   中英

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM