AesEncrypt.php 992 Bytes
<?php


namespace App\Helper;

use phpseclib3\Crypt\AES;

/**
 * Class Aes
 * @package App\Helper
 */
class AesEncrypt
{
    private static $length = 256;

    /**
     * 加密
     * @param $str
     * @return string
     */
    public static function encrypt($str, $mode = 'ecb')
    {
        try {
            $cipher = new AES($mode);
            $cipher->setKeyLength(self::$length);
            $cipher->setKey(env('secrect_key'));
            return base64_encode($cipher->encrypt($str));
        } catch (\Exception $e){
            return null;
        }
    }

    /**
     * 解密
     * @param $str
     * @return string
     */
    public static function decrypt($str, $mode = 'ecb')
    {
        try {
            $cipher = new AES($mode);
            $cipher->setKeyLength(self::$length);
            $cipher->setKey(env('secrect_key'));
            return $cipher->decrypt(base64_decode($str));
        } catch (\Exception $e) {
            return null;
        }
    }
}