mcrypt 擴展已經過時了大約10年,并且用起來很復雜。因此它被廢棄并且被 OpenSSL 所取代。 從PHP 7.2起它將被從核心代碼中移除并且移到PECL中。
PHP手冊在7.1遷移頁面給出了替代方案,就是用OpenSSL取代MCrypt.
/**
* [AesSecurity aes加密,支持PHP7.1]
*/
class AesSecurity
{
/**
* [encrypt aes加密]
* @param [type] $input [要加密的數據]
* @param [type] $key [加密key]
* @return [type] [加密后的數據]
*/
public static function encrypt($input, $key)
{
$data = openssl_encrypt($input, 'AES-128-ECB', $key, OPENSSL_RAW_DATA);
$data = base64_encode($data);
return $data;
}
/**
* [decrypt aes解密]
* @param [type] $sStr [要解密的數據]
* @param [type] $sKey [加密key]
* @return [type] [解密后的數據]
*/
public static function decrypt($sStr, $sKey)
{
$decrypted = openssl_decrypt(base64_decode($sStr), 'AES-128-ECB', $sKey, OPENSSL_RAW_DATA);
return $decrypted;
}
}