<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2022/11/4
 * Time: 11:48
 */

namespace app\lib\utils;


class Aes{


    /**
     * 数据AES加密
     */
    private static $method='AES-128-CBC';
    private static $iv="1234567890abcdef"; //初始向量
    private static $key="1234567890abcdef";//密钥

    /**
     * 加密后返回字段
     * @param $data
     * @return bool|string
     */
    public static function encrypt($strData){
        $strEncrypt=openssl_encrypt($strData,self::$method,self::$key,OPENSSL_RAW_DATA,self::$iv);
        if(!$strEncrypt){
            $setErrorMessage[]=openssl_error_string();
            return false;
        }
        return base64_encode($strEncrypt);
    }

    /**
     * 解密
     * @param $data
     * @return bool|string
     */
    public static function decrypt($data){
        $data=base64_decode($data);
        $strDecrypt=openssl_decrypt($data,self::$method,self::$key,OPENSSL_RAW_DATA,self::$iv);
        if(!$strDecrypt){
            $setErrorMessage[]=openssl_error_string();
            return false;
        }
        return $strDecrypt;
    }
}