微信公众号 PHP/tp5.1使用消息接口加密解密时报错(建议看到最后)

10 篇文章 0 订阅
2 篇文章 0 订阅

由于业务需要,公众号要是用消息接口将用户发消息与项目逻辑结合
在这里插入图片描述
在使用获取用户消息再回复与之对应的消息时,使用明文方式处理成功
但是在安全模式(消息接收与发送均加密)下
在这里插入图片描述
后来折腾半天找到问题所在
微信官方给出的文档里pkcs7Encoder.php这个文件中在加密解密方法里用到了一个叫mcrypt_module_open的函数,但是由于此函数在PHP7.*版本被抛弃了,正好我的环境就是7.2所以需要修改加密解密方法(这就是个坑,微信官方文档也不更新,也不提示)几经波折,找到如下解决办法

/**
     * 对明文进行加密
     * @param string $text 需要加密的明文
     * @return string 加密后的密文
     */
    public function encrypt($text, $appid)
    {

        try {
            if(version_compare(PHP_VERSION, '7','>=')) {
                //获得16位随机字符串,填充到明文之前
                $random = $this->getRandomStr();
                $text = $random . pack("N", strlen($text)) . $text . $appid;
                $iv = substr($this->key, 0, 16);
                $pkc_encoder = new PKCS7Encoder;
                $text = $pkc_encoder->encode($text);
                $encrypted = openssl_encrypt($text,'AES-256-CBC',substr($this->key, 0, 32),OPENSSL_ZERO_PADDING,$iv);
            } else {
                //获得16位随机字符串,填充到明文之前
                $random = $this->getRandomStr();
                $text = $random . pack("N", strlen($text)) . $text . $appid;
                // 网络字节序
                $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
                $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
                $iv = substr($this->key, 0, 16);
                //使用自定义的填充方式对明文进行补位填充
                $pkc_encoder = new PKCS7Encoder;
                $text = $pkc_encoder->encode($text);
                mcrypt_generic_init($module, $this->key, $iv);
                //加密
                $encrypted = mcrypt_generic($module, $text);
                mcrypt_generic_deinit($module);
                mcrypt_module_close($module);
                $encrypted = base64_encode($encrypted);
            }

            //print($encrypted);
            //使用BASE64对加密后的字符串进行编码
            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 {
            if(version_compare(PHP_VERSION, '7','>=')) {
                $iv = substr($this->key, 0, 16);
                $decrypted = openssl_decrypt($encrypted,'AES-256-CBC',substr($this->key, 0, 32),OPENSSL_ZERO_PADDING,$iv);
            } else {
                //使用BASE64对需要解密的字符串进行解码
                $ciphertext_dec = base64_decode($encrypted);
                $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
                $iv = substr($this->key, 0, 16);
                mcrypt_generic_init($module, $this->key, $iv);

                //解密
                $decrypted = mdecrypt_generic($module, $ciphertext_dec);
                mcrypt_generic_deinit($module);
                mcrypt_module_close($module);
            }

        } catch (Exception $e) {
            return array(ErrorCode::$DecryptAESError, null);
        }


        try {
            //去除补位字符
            $pkc_encoder = new PKCS7Encoder;
            $result = $pkc_encoder->decode($decrypted);
            //去除16位随机字符串,网络字节序和AppId
            if (strlen($result) < 16)
                return "";
            $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);
            if(version_compare(PHP_VERSION, '7','>=')) {
                if (!$appid) {
                    //如果传入的appid是空的,则认为是订阅号,使用数据中提取出来的appid
                    $appid = $from_appid;
                }
            }
        } catch (Exception $e) {
            //print $e;
            return array(ErrorCode::$IllegalBuffer, null);
        }
        if ($from_appid != $appid)
            return array(ErrorCode::$ValidateAppidError, null);
        //不注释上边两行,避免传入appid是错误的情况
        if(version_compare(PHP_VERSION, '7','>=')) {
            //增加appid,为了解决后面加密回复消息的时候没有appid的订阅号会无法回复
            return array(0, $xml_content, $from_appid);
        } else {
            return array(0, $xml_content);
        }

    }

这两个方法只把try里面的修改就行了。
这里吐槽一万字,就不写在这里,各位都懂的,反正最后是弄出来了
还有一点如果把接口放在tp框架中要注意一点
pkcs7Encoder.php,将构造函数改为

class Prpcrypt
{
	public $key;
 
	function __construct($k)
	{
		$this->key = base64_decode($k . "=");
	}

还有wxBizMsgCrypt.php

class WXBizMsgCrypt
{
    private $token;
    private $encodingAesKey;
    private $appId;
 
    /**
     * 构造函数
     * @param $token string 公众平台上,开发者设置的token
     * @param $encodingAesKey string 公众平台上,开发者设置的EncodingAESKey
     * @param $appId string 公众平台的appId
     */
    function __construct($token, $encodingAesKey, $appId)
    {
     $this->token = $token;
     $this->encodingAesKey = $encodingAesKey;
     $this->appId = $appId;
    }

另外其他的文件要将文件名改成类名一样的大小写,还有文件内文件之间的引用,和类的实例化大小写都更改,不然坑的你找不着北

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值