搜尋
首頁後端開發php教程小程式解碼時 php 7.0以上 mcrypt拓展無法使用 舊版的解密解決方案

本篇文章給大家分享的內容是小程式解碼時php 7.0以上mcrypt拓展無法使用舊版本的解密解決方案 ,有著一定的參考價值,有需要的朋友可以參考一下

<br/>
WXBizDataCrypt.php
class	WXBizDataCrypt	{

				private	$appid;
				private	$sessionKey;

				/**
					* 构造函数
					* @param $sessionKey string 用户在小程序登录后获取的会话密钥
					* @param $appid string 小程序的appid
					*/
				public	function	__construct($appid,	$sessionKey)	{
						$this->sessionKey	=	$sessionKey;
						$this->appid	=	$appid;
				}

				/**
					* 检验数据的真实性,并且获取解密后的明文.
					* @param $encryptedData string 加密的用户数据
					* @param $iv string 与用户数据一同返回的初始向量
					* @param $data string 解密后的原文
					*
					* @return int 成功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);

						$pc	=	new	Prpcrypt($aesKey);
						$result	=	$pc->decrypt($aesCipher,	$aesIV);

						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;
				}
			// 	// 注释这一段是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.&#39;--&#39;;
			//         }
			//         if( $dataObj->watermark->appid != $this->appid )
			//         {
			//             return ErrorCode::$IllegalBuffer.&#39;;;&#39;;
			//         }
			//         $data = $result[1];
			//         return ErrorCode::$OK;
			//     }

		}



#PKCS7Encoder.php


#
		class	PKCS7Encoder	{

				public	static	$block_size	=	16;

				/**
					* 对需要加密的明文进行填充补位
					* @param $text 需要进行填充补位操作的明文
					* @return 补齐明文字符串
					*/
				function	encode($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 删除填充补位后的明文
					*/
				function	decode($text)	{

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

		}

		/**
			* Prpcrypt class
			*
			* 
			*/
		class	Prpcrypt	{

				public	$key;

				public	function	__construct($k)	{
						$this->key	=	$k;
				}

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

						try	{

								$module	=	mcrypt_module_open(MCRYPT_RIJNDAEL_128,	&#39;&#39;,	MCRYPT_MODE_CBC,	&#39;&#39;);

								mcrypt_generic_init($module,	$this->key,	$aesIV);

								//解密
								$decrypted	=	mdecrypt_generic($module,	$aesCipher);
								mcrypt_generic_deinit($module);
								mcrypt_module_close($module);
						}	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);
				}

                                  //php 7.0版本
				 // public function decrypt( $aesCipher, $aesIV )
				 //    {
				 //        try {
				            
				 //            // $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, &#39;&#39;, MCRYPT_MODE_CBC, &#39;&#39;);
				            
				 //            // mcrypt_generic_init($module, $this->key, $aesIV);
				 //            // //解密
				 //            // $decrypted = mdecrypt_generic($module, $aesCipher);
				 //            // mcrypt_generic_deinit($module);
				 //            // mcrypt_module_close($module);
				 //            $decrypted = openssl_decrypt($aesCipher,&#39;AES-128-CBC&#39;,$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);
				 //    }

		}




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

以上是小程式解碼時 php 7.0以上 mcrypt拓展無法使用 舊版的解密解決方案的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
絕對會話超時有什麼區別?絕對會話超時有什麼區別?May 03, 2025 am 12:21 AM

絕對會話超時從會話創建時開始計時,閒置會話超時則從用戶無操作時開始計時。絕對會話超時適用於需要嚴格控制會話生命週期的場景,如金融應用;閒置會話超時適合希望用戶長時間保持會話活躍的應用,如社交媒體。

如果會話在服務器上不起作用,您將採取什麼步驟?如果會話在服務器上不起作用,您將採取什麼步驟?May 03, 2025 am 12:19 AM

服務器會話失效可以通過以下步驟解決:1.檢查服務器配置,確保會話設置正確。 2.驗證客戶端cookies,確認瀏覽器支持並正確發送。 3.檢查會話存儲服務,如Redis,確保其正常運行。 4.審查應用代碼,確保會話邏輯正確。通過這些步驟,可以有效診斷和修復會話問題,提升用戶體驗。

session_start()函數的意義是什麼?session_start()函數的意義是什麼?May 03, 2025 am 12:18 AM

session_start()iscucialinphpformanagingusersessions.1)ItInitiateSanewsessionifnoneexists,2)resumesanexistingsessions,and3)setsasesessionCookieforContinuityActinuityAccontinuityAcconActInityAcconActInityAcconAccRequests,EnablingApplicationsApplicationsLikeUseAppericationLikeUseAthenticationalticationaltication and PersersonalizedContentent。

為會話cookie設置httponly標誌的重要性是什麼?為會話cookie設置httponly標誌的重要性是什麼?May 03, 2025 am 12:10 AM

設置httponly標誌對會話cookie至關重要,因為它能有效防止XSS攻擊,保護用戶會話信息。具體來說,1)httponly標誌阻止JavaScript訪問cookie,2)在PHP和Flask中可以通過setcookie和make_response設置該標誌,3)儘管不能防範所有攻擊,但應作為整體安全策略的一部分。

PHP會議在網絡開發中解決了什麼問題?PHP會議在網絡開發中解決了什麼問題?May 03, 2025 am 12:02 AM

phpsessions solvathepromblymaintainingStateAcrossMultipleHttpRequestsbyStoringDataTaNthEserVerAndAssociatingItwithaIniquesestionId.1)他們儲存了AtoredAtaserver side,通常是Infilesordatabases,InseasessessionIdStoreDistordStoredStoredStoredStoredStoredStoredStoreDoreToreTeReTrestaa.2)

可以在PHP會話中存儲哪些數據?可以在PHP會話中存儲哪些數據?May 02, 2025 am 12:17 AM

phpsessionscanStorestrings,數字,數組和原始物。

您如何開始PHP會話?您如何開始PHP會話?May 02, 2025 am 12:16 AM

tostartaphpsession,usesesses_start()attheScript'Sbeginning.1)placeitbeforeanyOutputtosetThesessionCookie.2)useSessionsforuserDatalikeloginstatusorshoppingcarts.3)regenerateSessiveIdStopreventFentfixationAttacks.s.4)考慮使用AttActAcks.s.s.4)

什麼是會話再生,如何提高安全性?什麼是會話再生,如何提高安全性?May 02, 2025 am 12:15 AM

會話再生是指在用戶進行敏感操作時生成新會話ID並使舊ID失效,以防會話固定攻擊。實現步驟包括:1.檢測敏感操作,2.生成新會話ID,3.銷毀舊會話ID,4.更新用戶端會話信息。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器