侯体宗的博客
  • 首页
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

AES加解密在php接口请求过程中的应用示例

php  /  管理员 发布于 7年前   275

在php请求接口的时候,我们经常需要考虑的一个问题就是数据的安全性,因为数据传输过程中很有可能会被用fillder这样的抓包工具进行截获。一种比较好的解决方案就是在客户端请求发起之前先对要请求的数据进行加密,服务端api接收到请求数据后再对数据进行解密处理,返回结果给客户端的时候也对要返回的数据进行加密,客户端接收到返回数据的时候再解密。因此整个api请求过程中数据的安全性有了一定程度的提高。

今天结合一个简单的demo给大家分享一下AES加解密技术在php接口请求中的应用。

首先,准备一个AES加解密的基础类:

setKey($key);    }    if (null !== $iv) {      $this->setIv($iv);    }    if (null !== $options) {      if (isset($options['chipher'])) {        $this->setCipher($options['chipher']);      }      if (isset($options['PKCS7'])) {        $this->isPKCS7Padding($options['PKCS7']);      }      if (isset($options['mode'])) {        $this->setMode($options['mode']);      }    }  }  /**   * PKCS#7状态查看,传入Boolean值进行设置   * @param boolean $flag   * @return boolean   */  public function isPKCS7Padding($flag = null)  {    if (null === $flag) {      return $this->_PKCS7;    }    $this->_PKCS7 = (bool) $flag;  }  /**   * 开启加密算法   * @param string $algorithm_directory locate the encryption    * @param string $mode_directory   * @return Crypt_AES   */  public function _openMode($algorithm_directory = "" , $mode_directory = "")   {    $this->_descriptor = mcrypt_module_open($this->_cipher, $algorithm_directory, $this->_mode,$mode_directory);    return $this;  }  public function getDescriptor()  {    if (null === $this->_descriptor) {      $this->_openMode();    }    return $this->_descriptor;  }  protected function _genericInit()  {    return mcrypt_generic_init($this->getDescriptor(), $this->getKey(), $this->getIv());  }  protected function _genericDeinit()  {    return mcrypt_generic_deinit($this->getDescriptor());  }  public function getMode()  {    return $this->_mode;  }    public function setMode($mode)  {    $this->_mode = $mode;    return $this;  }  public function getCipher()  {    return $this->_cipher;  }    public function setCipher($cipher)  {    $this->_cipher = $cipher;    return $this;  }    /**   * 获得key   * @return string   */  public function getKey()  {    return $this->_key;  }    /**   * 设置可以   * @param string $key   */  public function setKey($key)  {    $this->_key = $key;    return $this;  }    /**   * 获得加密向量块,如果其为null时将追加当前Descriptor的IV大小长度   *   * @return string   */  public function getIv()  {    if (null === $this->_iv && in_array($this->_mode, array( "cbc", "cfb", "ofb", ))) {      $size = mcrypt_enc_get_iv_size($this->getDescriptor());      $this->_iv = str_pad("", 16, "\0");    }    return $this->_iv;  }  /**   * 获得向量块   *   * @param string $iv   * @return Crypt_AES $this   */  public function setIv($iv)  {    $this->_iv = $iv;    return $this;  }    /**   * 加密   * @param string $str 被加密文本   * @return string   */  public function encrypt($str){    $td = $this->getDescriptor();    $this->_genericInit();    $bin = mcrypt_generic($td, $this->padding($str));    $this->_genericDeinit();    return $bin;  }   public function padding($dat)  {    if ($this->isPKCS7Padding()) {      $block = mcrypt_enc_get_block_size($this->getDescriptor());         $len = strlen($dat);      $padding = $block - ($len % $block);      $dat .= str_repeat(chr($padding),$padding);          }    return $dat;  }  public function unpadding($str)  {    if ($this->isPKCS7Padding()) {      $pad = ord($str[($len = strlen($str)) - 1]);      $str = substr($str, 0, strlen($str) - $pad);    }    return $str;  }  /**   * 解密   * @param string $str    * @return string   */  public function decrypt($str){    $td = $this->getDescriptor();    $this->_genericInit();    $text = mdecrypt_generic($td, $str);    $this->_genericDeinit();    return $this->unpadding($text);  }    /**   * 16进制转成2进制数据   * @param string $hexdata 16进制字符串   * @return string   */  public static function hex2bin($hexdata)   {    return pack("H*" , $hexdata);  }  /**   * 字符串转十六进制   * @param string $hexdata 16进制字符串   * @return string   */  public static function strToHex($string)  {    $hex='';    for($i=0;$i

客户端请求部分:

true, 'mode'=>'cbc'));// var_dump($aes);$data['name'] = 'idoubi';$data['sex']= 'male';$data['age'] = 23;$data['signature'] = '白天我是一个程序员,晚上我就是一个有梦想的演员。';$content = base64_encode($aes->encrypt(json_encode($data)));$content = urlencode($content);$sign = md5($content.$md5Key);$url = 'http://localhost/aesdemo/api.php';$params = "version=1.0&sign=$sign&content=$content";// 请求接口post($url, $params);/** * 接口请求函数 */function post($url, $params) {  $curlPost= $params;  $ch = curl_init();   //初始化curl  curl_setopt($ch, CURLOPT_URL, $url);  //提交到指定网页  curl_setopt($ch, CURLOPT_HEADER, 0);  //设置header  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  //要求结果为字符串且输出到屏幕上  curl_setopt($ch, CURLOPT_POST, 1);  //post提交方式  curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);  $result = curl_exec($ch);//运行curl  curl_close($ch);  var_dump(json_decode($result, true));}

接口处理逻辑:

true, 'mode'=>'cbc'));  $decrypt = $aes->decrypt(base64_decode($content));  if (!$decrypt) {   // 解密失败    echo json_encode('can not decrypt the data');  } else {    echo json_encode($decrypt);   // 解密成功  }} else{  echo json_encode('data is not integrity');    // 数据校验失败}

上述接口请求过程中定义了三个加解密需要用到的参数:$aesKey、$aesIV、$md5key,在实际应用过程中,只要与客户端用户约定好这三个参数,客户端程序员利用这几个参数对要请求的数据进行加密后再请求接口,服务端程序员在接收到数据后利用同样的加解密参数对数据进行解密,整个api请求过程中的数据就很安全了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

您可能感兴趣的文章:

  • php写的AES加密解密类分享
  • php中AES加密解密的例子小结


  • 上一条:
    php抛出异常与捕捉特定类型的异常详解
    下一条:
    PHP上传Excel文件导入数据到MySQL数据库示例
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 用Time Warden监控PHP中的代码处理时间(0个评论)
    • 在PHP中使用array_pop + yield实现读取超大型目录功能示例(0个评论)
    • Property Hooks RFC在PHP 8.4中越来越接近现实(0个评论)
    • 近期文章
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(95个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-11
    • 2017-12
    • 2018-01
    • 2018-02
    • 2018-03
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-09
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    • 2024-09
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客