Php openssl_decrypt返回空白
我尝试了以下代码Php openssl_decrypt返回空白,php,openssl,Php,Openssl,我尝试了以下代码 <?PHP $encrypt_method = "AES-128-CBC"; $secret_key = 'iuiuiui'; $secret_iv = '1234567891234567'; $string="keeri"; // hash $key = hash('sha256', $secret_key);
<?PHP
$encrypt_method = "AES-128-CBC";
$secret_key = 'iuiuiui';
$secret_iv = '1234567891234567';
$string="keeri";
// hash
$key = hash('sha256', $secret_key);
// iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
$iv = substr(hash('sha256', $secret_iv), 0, 16);
$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
echo "encrypt----)";
echo $output ;
echo "decrypt---";
$output1 = openssl_decrypt($string, $encrypt_method, $key, 0, $iv);
echo $output1;
?>
解密字符串($output1)为空。您的代码运行良好,但解密功能的输入数据错误 您正在向openssl_decrypt函数提供(原始)明文,而不是由openssl_encrypt加密的数据(“输出”) 关于代码安全性的友好信息:当您在“CBC”模式下使用AES算法时,必须使用随机生成的初始化向量(“iv”),否则您的完整加密将变得易受攻击 这将是加密/解密的输出:
encrypt----)3BR54C8qvhHG3e4Lgry4uw==decrypt---keeri
完整代码:
<?php
$encrypt_method = "AES-128-CBC";
$secret_key = 'iuiuiui';
$secret_iv = '1234567891234567';
$string="keeri";
// hash
$key = hash('sha256', $secret_key);
// iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
$iv = substr(hash('sha256', $secret_iv), 0, 16);
$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
echo "encrypt----)";
echo $output ;
echo "decrypt---";
//$output1 = openssl_decrypt($string, $encrypt_method, $key, 0, $iv);
$output1 = openssl_decrypt($output, $encrypt_method, $key, 0, $iv);
echo $output1;
?>
<?php
$encrypt_method = "AES-128-CBC";
$secret_key = 'iuiuiui';
$secret_iv = '1234567891234567';
$string="keeri";
// hash
$key = hash('sha256', $secret_key);
// iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
$iv = substr(hash('sha256', $secret_iv), 0, 16);
$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
echo "encrypt----)";
echo $output ;
echo "decrypt---";
//$output1 = openssl_decrypt($string, $encrypt_method, $key, 0, $iv);
$output1 = openssl_decrypt($output, $encrypt_method, $key, 0, $iv);
echo $output1;
?>