使用PHP内置的mcrypt扩展库可以轻松地实现AES加密。
<?php
function aes_encrypt($data, $key, $iv) {
$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
mcrypt_generic_init($cipher, $key, $iv);
$encrypted = mcrypt_generic($cipher, $data);
mcrypt_generic_deinit($cipher);
mcrypt_module_close($cipher);
return base64_encode($encrypted);
}
$key = '1234567890abcdef';
$iv = 'fedcba0987654321';
$data = 'Hello World!';
$encrypted = aes_encrypt($data, $key, $iv);
echo $encrypted;
?>