资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

微信开发之数据解密的示例分析

这篇文章主要为大家展示了“微信开发之数据解密的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“微信开发之数据解密的示例分析”这篇文章吧。

我们一直强调成都做网站、成都网站建设、成都外贸网站建设对于企业的重要性,如果您也觉得重要,那么就需要我们慎重对待,选择一个安全靠谱的网站建设公司,企业网站我们建议是要么不做,要么就做好,让网站能真正成为企业发展过程中的有力推手。专业网站建设公司不一定是大公司,创新互联建站作为专业的网络公司选择我们就是放心。

最近在用thinkphp框架写微信小程序的服务端,可能真的是处女座的缘故,从官方下载了一个php的微信解密demo,明明能整合成一个类也没多少代码的,非要分几个类来写,考虑到thinkphp 5.0的框架对于扩展的类引用路劲看着太蛋疼,所以就整合成了一个类,方便调用,有需要的朋友可以download。

微信开发之数据解密的示例分析

百度盘下载地址:
pan.baidu.com/s/1kURMQ2b

sessionKey = $sessionKey;
        $this->appid = $appid;

        if (strlen($this->sessionKey) != 24) {
            return $this->IllegalAesKey;
        }
        $aesKey=base64_decode($this->sessionKey);

        if (strlen($iv) != 24) {
            return $this->IllegalIv;
        }
        $aesIV=base64_decode($iv);

        $aesCipher=base64_decode($encryptedData);

        $result = $this->decrypt($aesKey,$aesCipher,$aesIV);

        if ($result[0] != 0) {
            return $result[0];
        }

        $dataObj=json_decode( $result[1] );

        if( $dataObj  == NULL )
        {
            return $this->IllegalBuffer;
        }
        if( $dataObj->watermark->appid != $this->appid )
        {
            return $this->IllegalBuffer;
        }
        $data = $result[1];
        return $this->OKs;
    }

    /**
     * 对密文进行解密
     * @param string $aesCipher 需要解密的密文
     * @param string $aesIV 解密的初始向量
     * @return string 解密得到的明文
     */
    private function decrypt($key, $aesCipher, $aesIV )
    {
        try {

            $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');

            mcrypt_generic_init($module, $key, $aesIV);

            //解密
            $decrypted = mdecrypt_generic($module, $aesCipher);
            mcrypt_generic_deinit($module);
            mcrypt_module_close($module);
        } catch (Exception $e) {
            return array($this->IllegalBuffer, null);
        }


        try {
            //去除补位字符
            $result = $this->decode($decrypted);

        } catch (Exception $e) {
            //print $e;
            return array($this->IllegalBuffer, null);
        }
        return array(0, $result);
    }

    /**
     * 对需要加密的明文进行填充补位
     * @param $text 需要进行填充补位操作的明文
     * @return 补齐明文字符串
     */
    private function encode( $text )
    {
        $block_size = $this->blockSize;
        $text_length = strlen( $text );
        //计算需要填充的位数
        $amount_to_pad = $this->blockSize - ( $text_length % $this->blockSize );
        if ( $amount_to_pad == 0 ) {
            $amount_to_pad = $this->blockSize;
        }
        //获得补位所用的字符
        $pad_chr = chr( $amount_to_pad );
        $tmp = "";
        for ( $index = 0; $index < $amount_to_pad; $index++ ) {
            $tmp .= $pad_chr;
        }
        return $text . $tmp;
    }

    /**
     * 对解密后的明文进行补位删除
     * @param decrypted 解密后的明文
     * @return 删除填充补位后的明文
     */
    private function decode($text)
    {

        $pad = ord(substr($text, -1));
        if ($pad < 1 || $pad > 32) {
            $pad = 0;
        }
        return substr($text, 0, (strlen($text) - $pad));
    }

}

以上是“微信开发之数据解密的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


新闻名称:微信开发之数据解密的示例分析
本文地址:http://cdkjz.cn/article/piejog.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220