| 主機參考:VPS測評參考推薦/專注分享VPS服務器優惠信息!若您是商家可以在本站進行投稿,查看詳情!此外我們還提供軟文收錄、PayPal代付、廣告贊助等服務,查看詳情! |
| 我們發布的部分優惠活動文章可能存在時效性,購買時建議在本站搜索商家名稱可查看相關文章充分了解該商家!若非中文頁面可使用Edge瀏覽器同步翻譯!PayPal代付/收錄合作 |
最近在用thinkphp框架寫一個服務器端的微信小程序。可能真的是處女座的原因。我從官方下載了一個php微信解密演示。顯然,它可以集成到一個類中而無需太多代碼,因此我必須在幾個類中編寫它。考慮到thinkphp 5.0的框架引用擴展類太痛苦,我把它整合成一個類,方便調用,有需要的朋友可以下載。

百度磁盤下載地址:pan.baidu.com/s/1kURMQ2b
& lt?Php/** *微信小程序用戶加密數據解密示例代碼。* * @ copyright版權所有(c)1998-2014騰訊公司*/classwxbizdatacrypt { private $ appid;private $ sessionKeyprivate $ blockSize = 16private $ OKs = 0;private $ IllegalAesKey = -41001;private $ IllegalIv = -41002;private $ illegal buffer = -41003;private $ decode base 64 error = -41004;/* * *檢查數據的真實性,得到解密后的明文。* @param $encryptedData string加密的用戶數據* @param $iv string和用戶數據返回的初始向量* @param $data string解密原始文本* * @return int成功0,對應的錯誤代碼*/public function解密數據($ appid,$ sessionkey,$encryptedData,$iv,&;$ data){ $ this -》;sessionKey = $ sessionKey$ this -》;appid = $ appidif(strlen($ this -》;會話密鑰)!= 24){ return $ this -》;IllegalAesKey} $ AES key = base64 _ decode($ this -》;session key);if(strlen(iv)!= 24){ return $ this -》;非法的;} $ aesIV = base64 _ decode($ iv);$ AES cipher = base64 _ decode($ encrypted data);$ result = $ this -& gt;decrypt($ AES key,$aesCipher,$ aesIV);if($ result【0】!= 0){ return $ result【0】;} $ data obj = JSON _ decode($ result【1】);if($ data obj = = NULL){ return $ this -& gt;非法緩沖區;} if($ data obj -& gt;水印-》;appid!= $ this -& gt;appid){ return $ this -& gt;非法緩沖區;} $ data = $ result【1】;return $ this -& gt;OKs}/* * *解密密文* @ param string $要用AES cipher解密的密文* @ param string $ AES IV解密的初始向量* @return string解密明文*/私有函數Decrypt($ key,$aesCipher,$ aesIV){ try { $ module = MCRYPT _ module _ open(MCRYPT _ RIJNDAEL _ 128,& amp#39;& amp#39;,MCRYPT_MODE_CBC。#39;& amp#39;);mcrypt _ generic _ init($ module,$key,$ aesIV);//decrypt $ decrypted = mdecrypt _ generic($ module,$ AES cipher);mcrypt _ generic _ deinit($ module);mcrypt _ module _ close($ module);} catch(Exception $ e){ return array($ this -& gt;IllegalBuffer,null);}嘗試{//刪除補碼字符$ result = $ this -& gt;decode(已解密);} catch(Exception $ e){//print $ e;返回數組($ this -》;IllegalBuffer,null);}返回數組(0,$ result);}/* * *填充需要加密的明文* @param $text需要填充和填充的明文* @return Fill明文字符串*/私有函數encode($ text){ $ block _ size = $ this -》block size;$ text _ length = strlen($ text);//計算要填充的位數$ amount _ to _ pad = $ this -& gt;block size -($ text _ length % $ this -& gt;block size);if($ amount _ to _ pad = = 0){ $ amount _ to _ pad = $ this -& gt;blockSize}//用于獲取補碼的字符$ pad _ chr = chr($ amount _ to _ pad);$ tmp = & quot";for($ index = 0;$ index & lt$ amount _ to _ pad$ index++){ $ tmp。= $ pad _ chr} return $text。$ tmp}/* * *補充并刪除解密后的明文* @param解密后的明文* @return刪除用補充符填充的明文*/private function decode($ text){ $ pad = ord(substr($ text,-1);if($ pad & lt;1 | | $ pad & gt32){ $ pad = 0;} return substr(text,0,(strlen(text)-$ pad));}}如果您自己需要,ps:如果您對微信登錄授權流程感到困惑,可以留言咨詢。
【相關建議】
1.下載微信微信官方賬號平臺源代碼。
2.PigCMS(PigCMS)微電商系統運營版(獨立微店商城+三級分銷系統)
3.微信網王v3.4.5高級商業版微信魔方源代碼
這幾篇文章你可能也喜歡:
本文由主機參考刊發,轉載請注明:分享一個微信開發的數據解密示例教程(微信數據解密不正確) http://www.k2270.com/114151.html
主機參考

















評論前必須登錄!
注冊