首頁  >  文章  >  微信小程式  >  分享一篇微信開發之資料解密的實例教程

分享一篇微信開發之資料解密的實例教程

零下一度
零下一度原創
2017-05-20 16:19:522233瀏覽

最近在用thinkphp框架寫微信小程式的服務端,可能真的是處女座的緣故,從官方下載了一個php的微信解密demo,明明能整合成一個類也沒多少代碼的,非要分幾個類來寫,考慮到thinkphp 5.0的框架對於擴展的類引用路勁看著太蛋疼,所以就整合成了一個類,方便調用,有需要的朋友可以download。

分享一篇微信開發之資料解密的實例教程


百度磁碟下載位址:
pan.baidu.com/s/1kURMQ2b

<?php
/**
 * 对微信小程序用户加密数据的解密示例代码.
 *
 * @copyright Copyright (c) 1998-2014 Tencent Inc.
 */

class WXBizDataCrypt
{
    private $appid;
    private $sessionKey;
    private $blockSize = 16;

    private $OKs = 0;
    private $IllegalAesKey = -41001;
    private $IllegalIv = -41002;
    private $IllegalBuffer = -41003;
    private $DecodeBase64Error = -41004;

    /**
     * 检验数据的真实性,并且获取解密后的明文.
     * @param $encryptedData string 加密的用户数据
     * @param $iv string 与用户数据一同返回的初始向量
     * @param $data string 解密后的原文
     *
     * @return int 成功0,失败返回对应的错误码
     */
    public function decryptData($appid,$sessionKey,$encryptedData, $iv, &$data )
    {
        $this->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, &#39;&#39;, MCRYPT_MODE_CBC, &#39;&#39;);

            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));
    }

}

有需要的自己下,ps:對微信登入授權流程有迷惑的可以留言諮詢。

【相關推薦】

1. 微信公眾號平台源碼下載

#2. 小豬cms(PigCms)微電商系統營運版(獨立微店商城+三級分銷系統)

3. 微信人脈王v3.4.5高階商業版微信魔術方塊原始碼

############################################

以上是分享一篇微信開發之資料解密的實例教程的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn