[md]
<?php
/**
* 消息内容加解密
*/
class MsgAes
{
// 密码学方式,不可更改
private static $method = \'AES-128-CBC\';
// 加密
public static function encrypt($aesKey, $data)
{
$ivLen = openssl_cipher_iv_length(self::$method);
$iv = openssl_random_pseudo_bytes($ivLen);
$encryptedStr = openssl_encrypt($data, self::$method, $aesKey, OPENSSL_RAW_DATA, $iv);
return base64_encode($iv . $encryptedStr);
}
// 解密
public static function decrypt($aesKey, $encryptedData)
{
$encryptedData = base64_decode($encryptedData);
$ivLen = openssl_cipher_iv_length(self::$method);
$iv = substr($encryptedData, 0, $ivLen);
$encryptedStr = substr($encryptedData, $ivLen);
$decryptedStr = openssl_decrypt($encryptedStr, self::$method, $aesKey, OPENSSL_RAW_DATA, $iv);
return $decryptedStr;
}
}