AES

<?php 
namespace AES; 

use think\Cache; 

class mongoAES { 
    /**
    * var string $method 加解密方法,可通过openssl_get_cipher_methods()获得 
    */ 
    protected static $method; 
    
    /**
    * var string $secret_key 加解密的密钥 
    */ 
    protected static $secret_key; 
    
    /**
    * var string $iv 加解密的向量,有些方法需要设置比如CBC 
    */ 
    protected static $iv; 
    
    /**
    * var string $options 
    */ 
    protected static $options; 
    
    /**
    * 构造函数 
    * @param string $key 密钥 
    * @param string $method 加密方式 
    * @param string $iv iv向量 
    * @param mixed $options 
    * 'AES-128-ECB' //iv可以是0位  
    * 'AES-128-CBC' //iv必须是16位 
    */ 
    public function __construct() { 
        $method = 'AES-128-CBC'; 
        $ivlen = openssl_cipher_iv_length($method); //获取本加密方法中需要的向量字节数 
        $iv = openssl_random_pseudo_bytes($ivlen); //生成一个伪随机字节串 
        //获取AES KEY 
        $key = model('conf')->where(['sysmark'=>'SYS_AES_KEY'])->value('sysvalue'); 
        //unlink(EDIT_AES_KEY.'2019_03_11.txt'); 
        if (!Cache::get('EDIT_AES_KEY_CACHE') || Cache::get('EDIT_AES_KEY_CACHE') != $key) { 
            $msg = '管理员在' . date('Y-m-d H:i:s') . '将原来的AesKey('.Cache::get('EDIT_AES_KEY_CACHE') . ')更改为'.$key.'请注意相应时间段的信息加密key'; 
            if (!file_exists(EDIT_AES_KEY)) mkdir (EDIT_AES_KEY,0777,true); 
            $tag = '----------------------------------------------------------------------'; 
            file_put_contents(EDIT_AES_KEY . date('Y_m_d') . '.txt',"$msg\r\n$tag\r\n",FILE_APPEND); 
            model('historyAeskey')->insert(['hy_key'=>Cache::get('EDIT_AES_KEY_CACHE'),'addtime'=>time()]); //存历史key记录表 
            Cache::rm('EDIT_AES_KEY_CACHE'); //删除更改前的缓存 
            Cache::set('EDIT_AES_KEY_CACHE',$key); //写入新缓存 
        } 
        $this::$secret_key = $key; 
        $this::$method = $method;  
        $this::$iv = $iv; 
        $this::$options = OPENSSL_RAW_DATA; 
    } 
    
    /**
    * 加密方法,对数据进行加密,返回加密后的数据 
    * @param string $data 要加密的数据 
    * @return string 
    */ 
    public function encrypt($data) { 
        $ciphertext_raw = openssl_encrypt($data, $this::$method, $this::$secret_key, $this::$options, $this::$iv); 
        //as_binary 为true返回二进制格式 false 为16进制 
        $hmac = hash_hmac('sha256', $ciphertext_raw, $this::$secret_key,$as_binary = true); 
        return base64_encode($this::$iv . $hmac . $ciphertext_raw); 
    } 
    
    /**
    * 解密方法,对数据进行解密,返回解密后的数据 
    * @param string $data 要解密的数据 
    * @return string 
    */ 
    public function decrypt($data) { 
        $c = base64_decode($data); 
        $ivlen = openssl_cipher_iv_length($this::$method); 
        $iv = substr($c, 0, $ivlen); 
        $hmac = substr($c, $ivlen, $sha2len = 32); 
        $ciphertext_raw = substr($c, $ivlen + $sha2len); 
        $calcmac = hash_hmac('sha256',$ciphertext_raw,$this::$secret_key,$as_binary = true); 
        //PHP 5.6+ 定时攻击安全性比较 
        if (hash_equals($hmac, $calcmac)) { 
            $original_plaintext = openssl_decrypt($ciphertext_raw,$this::$method,$this::$secret_key,$this::$options,$iv); 
            return $original_plaintext; 
        } 
        return '解密失败'; 
    } 
} 
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值