PHP AES加解密类

class Aes
{
    protected $method;      //密码学方式  openssl_get_cipher_methods() 共201种
    protected $secret_key;  //秘钥
    protected $iv;          //非 NULL 的初始化向量
    protected $options;     //options 是以下标记的按位或: OPENSSL_RAW_DATA 、 OPENSSL_ZERO_PADDING

    public function __construct($method = 'AES-128-ECB', $iv = '', $options = 0)
    {
        $this->secret_key = config('app.aeskey'); //秘钥
        $this->method     = $method;
        $this->iv         = $iv;
        $this->options    = $options;
    }

    /**
     * 加密
     * @desc
     * @author    [Anly,]
     * @since     2018/05/
     * @modify
     *
     * @param string $data 加密的数据
     *
     * @return string
     */
    public function encrypt($data)
    {
        return openssl_encrypt($data, $this->method, $this->secret_key, $this->options, $this->iv);
    }

    /**
     * 解密
     * @desc
     * @author    [Anly,]
     * @since     2018/05/
     * @modify
     *
     * @param string $data 解密的数据
     *
     * @return string
     */
    public function decrypt($data)
    {
        return openssl_decrypt($data, $this->method, $this->secret_key, $this->options, $this->iv);
    }
}

  

猜你喜欢

转载自www.cnblogs.com/anly95/p/9236096.html