当前位置:   article > 正文

AES128加密_aes128在线加密

aes128在线加密

调用

<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;
use think\Request;

class Index extends Frontend
{

    function decrypt(){

        $this->aesmodel = new \app\common\model\Aes;
        // $param = [
        //     'sign'=>'f2990213be172b44b75743297c3afdf5',
        //     'page'=>1
        // ];
        // $param = json_encode($param);

        // echo urlencode($this->aesmodel->encrypt($param,'iskajfisudoqkdug'));die;
   
        $request = Request::instance();

        $info = $this->aesmodel->decrypt_info($request->get());

        //赋值
        Request::instance()->get($info['data']);
    
        var_dump($info);
        var_dump($request->get());
        die;
    }

    function encrypt(){

        $this->aesmodel = new \app\common\model\Aes;
        
        $request = Request::instance();

        $data = [
            'sign'=>'f2990213be172b44b75743297c3afdf5',
            'page'=>1
        ];
        $data = json_encode($data);

        $info = $this->aesmodel->encrypt($data,$request->get('key'));   //key iskajfisudoqkdug

        echo $info.'<br />'.urlencode($info).'<br />';

    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

封装类

<?php

namespace app\common\model;

use think\Db;
use think\Model;

class Aes extends Model
{

    /**
    *
    * @param string $string 需要加密的字符串
    * @param string $key 密钥
    * @return string
    */
    public static function encrypt($string, $key)
    {
        // openssl_encrypt 加密不同Mcrypt,对秘钥长度要求,超出16加密结果不变
        $data = openssl_encrypt($string, 'AES-128-ECB', $key, OPENSSL_RAW_DATA);
        return base64_encode($data);
    }

    /**
    * @param string $string 需要解密的字符串
    * @param string $key 密钥
    * @return string
    */
    public static function decrypt($string, $key)
    {
        return openssl_decrypt(base64_decode($string), 'AES-128-ECB', $key, OPENSSL_RAW_DATA);
    }

    function decrypt_info($param){

        $return = ['code'=>0,'message'=>'','data'=>[]];

        //参数校验
        if(!isset($param['param'])){
            $return = ['code'=>2,'message'=>'参数不足','data'=>[]];
        }
        elseif(!isset($param['key'])){
            $return = ['code'=>2,'message'=>'令牌不能为空','data'=>[]];
        }
        else{
            if(strpos($param['param'], '%')!==false){
                $param['param'] = urldecode($param['param']);
            }
            
            //参数解密
            $decrypt = $this->decrypt($param['param'], $param['key']);

            if(!$decrypt){
                $return = ['code'=>2,'message'=>'令牌信息错误','data'=>$param];
            }else{
                $params = json_decode($decrypt,true);

                if(isset($params['sign'])){
                    //验证签名
                    $list=Db::table('v_video_keys')->where('key',$param['key'])->field('secrect')->find();

                    $sign = $list?md5($param['key'].'+'.$list['secrect']):'';

                    if($params['sign']==$sign){
                        $return = ['code'=>1,'message'=>'验证通过','data'=>$params];
                        unset($params,$param);
                    }
                }else{
                    $return = ['code'=>2,'message'=>'sign为空','data'=>$params];
                }
            }
            
        }

        return $return;
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/794801
推荐阅读
相关标签
  

闽ICP备14008679号