收藏
回答

PHP7怎么加解密AES

微信小程序开发过程中,开启了消息推送,并且消息是完全模式,需要自己加解密,服务器语言使用PHP,但是问题来了,官方给的代码中的消息加解密方法在PHP7中并不能使用,mcrypt_module_open

等相关方法不支持了,请问在PHP7中如何加解密消息内容。下面是微信官方给的PHP5的加密方法,在php7中出现问题,求大佬解答


回答关注问题邀请回答
收藏

3 个回答

  • Haoliyou
    Haoliyou
    2019-10-11

    你好,使用你的这个,这里报错,from_appid是空的呢

    2019-10-11
    有用 2
    回复 1
    • Banana
      Banana
      2021-12-01
      怎么解决的
      2021-12-01
      回复
  • 飞鸿先森          ༽
    飞鸿先森 ༽
    2018-07-18

    折腾半天,终于解决,将微信官方提供的pkcs7Encoder.php中的两个方法修改如下即可

    /**
         * 对明文进行加密
         * @param string $text 需要加密的明文
         * @return string 加密后的密文
         */
        public function encrypt($text, $appid)
        {
     
            try {
                //获得16位随机字符串,填充到明文之前
                $random = $this->getRandomStr();//"aaaabbbbccccdddd";
                $text = $random . pack("N", strlen($text)) . $text . $appid;
                $iv = substr($this->key, 0, 16);
                $pkc_encoder = new Pkcsencoder();
                $text = $pkc_encoder->encode($text);
                $encrypted = openssl_encrypt($text,'AES-256-CBC',substr($this->key, 0, 32),OPENSSL_ZERO_PADDING,$iv);
                return array(ErrorCode::$OK, $encrypted);
            } catch (Exception $e) {
                //print $e;
                return array(Errorcode::$EncryptAESError, null);
            }
        }
     
        /**
         * 对密文进行解密
         * @param string $encrypted 需要解密的密文
         * @return string 解密得到的明文
         */
        public function decrypt($encrypted, $appid)
        {
     
            try {
                $iv = substr($this->key, 0, 16);
                $decrypted = openssl_decrypt($encrypted,'AES-256-CBC',substr($this->key, 0, 32),OPENSSL_ZERO_PADDING,$iv);
            } catch (Exception $e) {
                return array(Errorcode::$DecryptAESError, null);
            }
            try {
                //去除补位字符
                $pkc_encoder = new Pkcsencoder();
                $result = $pkc_encoder->decode($decrypted);
                //去除16位随机字符串,网络字节序和AppId
                if (strlen($result) < 16)
                    return array('出错啦');
                $content = substr($result, 16, strlen($result));
                $len_list = unpack("N", substr($content, 0, 4));
                $xml_len = $len_list[1];
                $xml_content = substr($content, 4, $xml_len);
                $from_appid = substr($content, $xml_len + 4);
            } catch (Exception $e) {
                //print $e;
                return array(Errorcode::$IllegalBuffer, null);
            }
            if ($from_appid != $appid)
                return array(Errorcode::$ValidateAppidError, null);
            return array(0, $xml_content, $from_appid);
     
        }


    2018-07-18
    有用 2
    回复 5
    • 郭郭哒
      郭郭哒
      2018-08-09

      你好,请问Pkcsencoder这个类方法是什么样子的呢

      2018-08-09
      回复
    • 飞鸿先森          ༽
      飞鸿先森 ༽
      2018-08-11回复郭郭哒
      <?php
       
      namespace app\wxapp\controller;
      include_once "Errorcode.php";
       
      class Pkcsencoder
      {
          public static $block_size = 32;
       
          /**
           * 对需要加密的明文进行填充补位
           * @param $text 需要进行填充补位操作的明文
           * @return 补齐明文字符串
           */
          function encode($text)
          {
              $block_size = Pkcsencoder::$block_size;
              $text_length = strlen($text);
              //计算需要填充的位数
              $amount_to_pad = Pkcsencoder::$block_size - ($text_length % Pkcsencoder::$block_size);
              if ($amount_to_pad == 0) {
                  $amount_to_pad = Pkcsencoder::block_size;
              }
              //获得补位所用的字符
              $pad_chr = chr($amount_to_pad);
              $tmp = "";
              for ($index = 0; $index < $amount_to_pad; $index++) {
                  $tmp .= $pad_chr;
              }
              return $text . $tmp;
          }
       
          /**
           * 对解密后的明文进行补位删除
           * @param decrypted 解密后的明文
           * @return 删除填充补位后的明文
           */
          function decode($text)
          {
       
              $pad = ord(substr($text, -1));
              if ($pad < 1 || $pad > 32) {
                  $pad = 0;
              }
              return substr($text, 0, (strlen($text) - $pad));
          }
       
      }

      Pkcsencoder类的所有代码都在这了

      2018-08-11
      1
      回复
    • 郭郭哒
      郭郭哒
      2018-08-11回复飞鸿先森 ༽

      嗯嗯好的,多谢多谢

      2018-08-11
      回复
    • 二狗
      二狗
      2020-10-15
      万分感谢
      2020-10-15
      回复
    • 落拓
      落拓
      2021-03-31
      帮了大忙!感谢
      2021-03-31
      回复
  • 武歌🐭
    武歌🐭
    2018-07-19

    请问官方提供的demo在哪儿下载? 我在这里下载的没有php语言的,只有c++的demohttps://developers.weixin.qq.com/miniprogram/dev/api/signature.html#wxchecksessionobject

    2018-07-19
    有用 1
    回复 1
登录 后发表内容