<?php

/**
 * AES类
 * Class Aes
 */
class Aes {
    
    public $resultArr   = [
        'status'        => true,
        'code'          => 200,
        'info'          => '',
    ];
    
    public function __construct() {
    
    }
    
    /**
     * 加密
     * @param $string
     * @param $key
     * @param $cipherMethod
     * @return array
     */
    public function encrypt($string, $key, $cipherMethod) {
        
        try {
            // 待加密的明文信息数据
            $string         = trim($string);
            // 秘钥
            $key            = substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true), 0, 16);
            // 密码学方式
            $cipherMethod   = trim($cipherMethod);
            
            if ('' === $string) {
                $errorLogDataArr    = [];
                $code               = SysConst::ERR_EMPTY;
                $message            = '加密内容不能为空';
                throw new Exception($message, $code, new Exception(json_encode($errorLogDataArr), $code));
            }
            
            if (!in_array($cipherMethod, openssl_get_cipher_methods())) {
                $errorLogDataArr    = [];
                $code               = SysConst::ERR_DATA_INVALID;
                $message            = '密码学方式不正确';
                throw new Exception($message, $code, new Exception(json_encode($errorLogDataArr), $code));
            }
            
            $cipherText             = openssl_encrypt($string, $cipherMethod, $key, OPENSSL_RAW_DATA);
            
            if (false === $cipherText) {
                $errorLogDataArr    = [];
                $code               = SysConst::ERR_FAILED;
                $message            = 'AES加密失败';
                throw new Exception($message, $code, new Exception(json_encode($errorLogDataArr), $code));
            }
            
            $cipherText                 = strtolower(bin2hex($cipherText));
            $this->resultArr['info']    = $cipherText;
        }
        catch (Exception $e) {
            $this->setErrorInfo($e);
        }
        
        return $this->resultArr;
        
    }
    
    /**
     * 解密
     * @param $cipherText
     * @param $key
     * @param $cipherMethod
     * @return array
     */
    public function decrypt($cipherText, $key, $cipherMethod) {
        
        try {
            // 待解密的信息数据
            $cipherText     = hex2bin($cipherText);
            // 秘钥
            $key            = substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true), 0, 16);
            // 密码学方式
            $cipherMethod   = trim($cipherMethod);
            
            if ('' === $cipherText) {
                $errorLogDataArr    = [];
                $code               = SysConst::ERR_EMPTY;
                $message            = '解密内容不能为空';
                throw new Exception($message, $code, new Exception(json_encode($errorLogDataArr), $code));
            }
            
            if (!in_array($cipherMethod, openssl_get_cipher_methods())) {
                $errorLogDataArr    = [];
                $code               = SysConst::ERR_DATA_INVALID;
                $message            = '密码学方式不正确';
                throw new Exception($message, $code, new Exception(json_encode($errorLogDataArr), $code));
            }
            
            $decryptText            = openssl_decrypt($cipherText, $cipherMethod, $key, OPENSSL_RAW_DATA);
            
            if (false === $decryptText) {
                $errorLogDataArr    = [];
                $code               = SysConst::ERR_FAILED;
                $message            = 'AES解密失败';
                throw new Exception($message, $code, new Exception(json_encode($errorLogDataArr), $code));
            }
            
            $this->resultArr['info'] = $decryptText;
        }
        catch (Exception $e) {
            $this->setErrorInfo($e);
        }
        
        return $this->resultArr;
        
    }
    
    protected function setErrorInfo(Exception $e) {
        
        $this->resultArr['status']      = false;
        $this->resultArr['code']        = $e->getCode();
        $this->resultArr['info']        = $e->getMessage();
        
    }
    
}

  

版权声明:本文为一个渺小的人原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/wjs2019/p/16573461.html