本篇文章給大家分享的內容是小程式解碼時 php 7.0以上 mcrypt拓展無法使用 舊版本的解密解決方案 ,有著一定的參考價值,有需要的朋友可以參考一下
WXBizDataCrypt.php
classWXBizDataCrypt{private$appid;private$sessionKey;/*** 建構函式* @param $sessionKey string 使用者在小程式登入後擷取的工作階段金鑰* @param $appid string 小程式的appid*/publicfunction__construct($appid,$sessionKey){$this->sessionKey=$sessionKey;$this->appid=$appid;}/*** 檢驗資料的真實性,並且擷取解密後的明文.* @param $encryptedData string 加密的使用者資料* @param $iv string 與使用者資料一同返回的初始向量* @param $data string 解密後的原文** @return int 成功0,失敗返回對應的錯誤碼*/publicfunctiondecryptData($encryptedData,$iv,&$data){if(strlen($this->sessionKey)!=24){returnErrorCode::$IllegalAesKey;}$aesKey=base64_decode($this->sessionKey);if(strlen($iv)!=24){returnErrorCode::$IllegalIv;}$aesIV=base64_decode($iv);$aesCipher=base64_decode($encryptedData);$pc=newPrpcrypt($aesKey);$result=$pc->decrypt($aesCipher,$aesIV);if($result[0]!=0){return$result[0];}$dataObj=json_decode($result[1]);if($dataObj==NULL){returnErrorCode::$IllegalBuffer;}if($dataObj->watermark->appid!=$this->appid){returnErrorCode::$IllegalBuffer;}$data=$result[1];returnErrorCode::$OK;}// // 注釋這一段是7.0以上版本// public function decryptData( $encryptedData, $iv, &$data )// {// if (strlen($this->sessionKey) != 24) {// return ErrorCode::$IllegalAesKey;// }// $aesKey=base64_decode($this->sessionKey); // if (strlen($iv) != 24) {// return ErrorCode::$IllegalIv;// }// $aesIV=base64_decode($iv);// // $aesCipher=base64_decode($encryptedData);// $aesCipher=$encryptedData;// $pc = new Prpcrypt($aesKey);// $result = $pc->decrypt($aesCipher,$aesIV);// // var_dump($result);// if ($result[0] != 0) {// return $result[0];// } // $dataObj=json_decode( $result[1] );// if( $dataObj == NULL )// {// return ErrorCode::$IllegalBuffer.'--';// }// if( $dataObj->watermark->appid != $this->appid )// {// return ErrorCode::$IllegalBuffer.';;';// }// $data = $result[1];// return ErrorCode::$OK;// }}
PKCS7Encoder.php
classPKCS7Encoder{publicstatic$block_size=16;/*** 對需要加密的明文進行填充補位* @param $text 需要進行填充補位操作的明文* @return 補齊明文字串*/functionencode($text){$block_size=PKCS7Encoder::$block_size;$text_length=strlen($text);//計算需要填充的位元$amount_to_pad=PKCS7Encoder::$block_size-($text_length%PKCS7Encoder::$block_size);if($amount_to_pad==0){$amount_to_pad=PKCS7Encoder::block_size;}//獲得補位所用的字元$pad_chr=chr($amount_to_pad);$tmp="";for($index=0;$index<$amount_to_pad;$index++){$tmp.=$pad_chr;}return$text.$tmp;}/*** 對解密後的明文進行補位刪除* @param decrypted 解密後的明文* @return 刪除填充補位後的明文*/functiondecode($text){$pad=ord(substr($text,-1));if($pad<1||$pad>32){$pad=0;}returnsubstr($text,0,(strlen($text)-$pad));}}/*** Prpcrypt class** */classPrpcrypt{public$key;publicfunction__construct($k){$this->key=$k;}/*** 對密文進行解密* @param string $aesCipher 需要解密的密文* @param string $aesIV 解密的初始向量* @return string 解密得到的明文*/publicfunctiondecrypt($aesCipher,$aesIV){try{$module=mcrypt_module_open(MCRYPT_RIJNDAEL_128,'',MCRYPT_MODE_CBC,'');mcrypt_generic_init($module,$this->key,$aesIV);//解密$decrypted=mdecrypt_generic($module,$aesCipher);mcrypt_generic_deinit($module);mcrypt_module_close($module);}catch(Exception$e){returnarray(ErrorCode::$IllegalBuffer,null);}try{//去除補位字元$pkc_encoder=newPKCS7Encoder;$result=$pkc_encoder->decode($decrypted);}catch(Exception$e){//print $e;returnarray(ErrorCode::$IllegalBuffer,null);}returnarray(0,$result);} //php 7.0版本 // public function decrypt( $aesCipher, $aesIV ) // { // try { // // $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); // // mcrypt_generic_init($module, $this->key, $aesIV); // // //解密 // // $decrypted = mdecrypt_generic($module, $aesCipher); // // mcrypt_generic_deinit($module); // // mcrypt_module_close($module); // $decrypted = openssl_decrypt($aesCipher,'AES-128-CBC',$this->key,OPENSSL_ZERO_PADDING,$aesIV); // // var_dump($decrypted); // } catch (Exception $e) { // return array(ErrorCode::$IllegalBuffer, null); // } // try { // //去除補位字元 // $pkc_encoder = new PKCS7Encoder; // $result = $pkc_encoder->decode($decrypted); // } catch (Exception $e) { // //print $e; // return array(ErrorCode::$IllegalBuffer, null); // } // return array(0, $result); // }}