AES 128 CBC:如何计算正确的 IV?
<?php
$data = '
-What is the answer to the Ultimate Question of Life, the Universe, and Everything ?
-42
';
$method = 'AES-128-CBC';
$password = 'secret password';
$raw_output = $raw_input = true;
$iv_len = openssl_cipher_iv_length($method);
$iv = openssl_random_pseudo_bytes($iv_len);
$encrypted = openssl_encrypt($data, $method, $password, $raw_output, $iv);
var_dump($encrypted);
echo 'Decryption with known IV: OK';
$decrypted = openssl_decrypt($encrypted, $method, $password, $raw_input, $iv);
var_dump($decrypted);
echo 'Decryption with calculated IV: Fail<br><br>';
$iv = substr($encrypted, 0, $iv_len);
echo 'Without substring';
$decrypted = openssl_decrypt($encrypted, $method, $password, $raw_input, $iv);
var_dump($decrypted);
echo 'With substring';
$encrypted = substr($encrypted, $iv_len);
$decrypted = openssl_decrypt($encrypted, $method, $password, $raw_input, $iv);
var_dump($decrypted);
我做错了什么?
<?php
$data = '
-What is the answer to the Ultimate Question of Life, the Universe, and Everything ?
-42
';
$method = 'AES-128-CBC';
$password = 'secret password';
$raw_output = $raw_input = true;
$iv_len = openssl_cipher_iv_length($method);
$iv = openssl_random_pseudo_bytes($iv_len);
$encrypted = openssl_encrypt($data, $method, $password, $raw_output, $iv);
var_dump($encrypted);
echo 'Decryption with known IV: OK';
$decrypted = openssl_decrypt($encrypted, $method, $password, $raw_input, $iv);
var_dump($decrypted);
echo 'Decryption with calculated IV: Fail<br><br>';
$iv = substr($encrypted, 0, $iv_len);
echo 'Without substring';
$decrypted = openssl_decrypt($encrypted, $method, $password, $raw_input, $iv);
var_dump($decrypted);
echo 'With substring';
$encrypted = substr($encrypted, $iv_len);
$decrypted = openssl_decrypt($encrypted, $method, $password, $raw_input, $iv);
var_dump($decrypted);
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来你假设你的 IV 位于加密输出的开头,但你没有明确地将它放在那里。
尝试:
并尝试解密:
It seems like you are assuming that your IV is at the beginning of the encrypted output but your are not explicitly putting it there.
Try:
and try decrypting with: