AES CFB加解密,不用填充,OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING相当于OPENSSL_NO_PADDING
root@ubuntu:~# cat a.php
<?php
$iv = random_bytes(16);
$encryption_key = hash('sha256', 'key', true);
echo 'key:'. bin2hex($encryption_key)."\n";
echo 'iv:'. bin2hex($iv)."\n";
$data = 'data';
$data = openssl_encrypt($data, 'aes-256-cfb', $encryption_key, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, $iv);
echo 'encrypt data:'. bin2hex($data) . "\n";
$data = openssl_decrypt($data, 'aes-256-cfb', $encryption_key, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, $iv);
echo 'decrypt data:'. $data . "\r\n";
AES CBC加解密,自动使用PKCS#7填充,iv可公开,最好使用唯一的随机字符:
<?php
$iv = random_bytes(16);
$encryption_key = hash('sha256', 'key', true);
$data = 'data';
$data = openssl_encrypt($data, 'aes-256-cbc', $encryption_key, OPENSSL_RAW_DATA, $iv);
echo bin2hex($data) . "\r\n";
$data = openssl_decrypt($data, 'aes-256-cbc', $encryption_key, OPENSSL_RAW_DATA, $iv);
echo $data . "\r\n";
?>
标签:none
总结的很棒