PHP AES加解密系列-PHP AES加密

时间:2024-01-26 21:32:35

使用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;
?>