PHP7.1中AES加密解密方法 mcrypt_module_open()替换方案

mcrypt 扩展已通过时了大约10年,而且用起来很复杂。所以它被废弃而且被 OpenSSL 所取代。 从PHP 7.2起它将被从核心代码中移除而且移到PECL中。html

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;
    }
}

可据需求,自行改编。code

原文 http://www.tech1024.cn/origin...htm

相关文章
相关标签/搜索