赞
踩
调用
<?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 />'; } }
封装类
<?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; } }
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。