PHP AES加解密示例
一、概述
本示例演示了如何在PHP中使用AES算法进行加解密操作。
二、环境准备
PHP 7.4
OpenSSL扩展
三、示例代码
- 加密函数
function encrypt($plaintext, $key, $iv)
{
$method = 'aes-256-cbc';
$options = OPENSSL_RAW_DATA;
$ciphertext = openssl_encrypt($plaintext, $method, $key, $options, $iv);
return base64_encode($ciphertext);
}
Use code with caution.
- 解密函数
function decrypt($ciphertext, $key, $iv)
{
$method = 'aes-256-cbc';
$options = OPENSSL_RAW_DATA;
$plaintext = openssl_decrypt(base64_decode($ciphertext), $method, $key, $options, $iv);
return $plaintext;
}
Use code with caution.
- 使用示例