Loading

PHP AES加密封装类

简介

PHP AES 加密解密常用封装类

使用方式

$key = 123;
$aes = new Aes($key);
$data = ['a' => 1];
$aes->decrypt($aes->encrypt($data));

源码

/**
 * Aes 加密 解密
 * @package Pay\Lib
 */
class Aes
{

    private $key;

    /**
     * 构造方法
     * @param $key
     */
    public function __construct($key)
    {
        $this->key = $key;
    }

    /**
     * 加密
     * @param $input
     * @return string
     */
    public function encrypt($input)
    {
        $data = openssl_encrypt($input, 'AES-128-ECB', $this->key, OPENSSL_RAW_DATA);
        return base64_encode($data);
    }

    /**
     * 解密
     * @param $input
     * @return false|string
     */
    public function decrypt($input)
    {
        return openssl_decrypt(base64_decode($input), 'AES-128-ECB', $this->key, OPENSSL_RAW_DATA);
    }
}
posted @ 2020-11-03 11:40  nicesome  阅读(138)  评论(0)    收藏  举报