重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
這篇文章主要為大家展示了“微信開發之數據解密的示例分析”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“微信開發之數據解密的示例分析”這篇文章吧。
我們一直強調成都做網站、成都網站建設、成都外貿網站建設對于企業的重要性,如果您也覺得重要,那么就需要我們慎重對待,選擇一個安全靠譜的網站建設公司,企業網站我們建議是要么不做,要么就做好,讓網站能真正成為企業發展過程中的有力推手。專業網站建設公司不一定是大公司,創新互聯建站作為專業的網絡公司選擇我們就是放心。
最近在用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)); } }
以上是“微信開發之數據解密的示例分析”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注創新互聯行業資訊頻道!