新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
php解密小程序sessionKey的方法
小程序獲取用戶信息,需要先拿到code 然后再獲取用戶信息,再用戶同意獲取信息后,小程序會返回session_key 等相關(guān)信息,需要注意的是session_key 是需要解密才能獲取到用戶的相關(guān)信息的,小程序代碼我就不放出來了,今天主要介紹一下php是如何解密的

微信小程序解密
/**
* 檢驗數(shù)據(jù)的真實性,并且獲取解密后的明文.
* @param string $appid 小程序id
* @param string $sessionKey 小程序密鑰
* @param string $encryptedData 在小程序中獲取的encryptedData
* @param string $iv 在小程序中獲取的iv
* @return array 成功0,失敗返回對應(yīng)的錯誤碼
*/
function decryptData($encryptedData, $iv, $appid , $sessionKey)
{
$OK = 0;
$IllegalAesKey = -41001;
$IllegalIv = -41002;
$IllegalBuffer = -41003;
$DecodeBase64Error = -41004;
if (strlen($sessionKey) != 24) {
return $IllegalAesKey;
}
$aesKey = base64_decode($sessionKey);
if (strlen($iv) != 24) {
return $IllegalIv;
}
$aesIV = base64_decode($iv);
$aesCipher = base64_decode($encryptedData);
$result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
$dataObj = json_decode($result);
if ($dataObj == NULL) {
return $IllegalBuffer;
}
if ($dataObj->watermark->appid != $appid) {
return $IllegalBuffer;
}
return $result;
}
百度小程序
/**
* 數(shù)據(jù)解密:低版本使用mcrypt庫(PHP < 5.3.0),高版本使用openssl庫(PHP >= 5.3.0)。
*
* @param string $ciphertext 待解密數(shù)據(jù),返回的內(nèi)容中的data字段
* @param string $iv 加密向量,返回的內(nèi)容中的iv字段
* @param string $app_key 創(chuàng)建小程序時生成的app_key
* @param string $session_key 登錄的code換得的
* @return string | false
*/
function decrypt($ciphertext, $iv, $app_key, $session_key)
{
$session_key = base64_decode($session_key);
$iv = base64_decode($iv);
$ciphertext = base64_decode($ciphertext);
$plaintext = false;
if (function_exists("openssl_decrypt")) {
$plaintext = openssl_decrypt($ciphertext, "AES-192-CBC", $session_key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);
} else {
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, null, MCRYPT_MODE_CBC, null);
mcrypt_generic_init($td, $session_key, $iv);
$plaintext = mdecrypt_generic($td, $ciphertext);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
}
if ($plaintext == false) {
return false;
}
// trim pkcs#7 padding
$pad = ord(substr($plaintext, -1));
$pad = ($pad < 1 || $pad > 32) ? 0 : $pad;
$plaintext = substr($plaintext, 0, strlen($plaintext) - $pad);
// trim header
$plaintext = substr($plaintext, 16);
// get content length
$unpack = unpack("Nlen/", substr($plaintext, 0, 4));
// get content
$content = substr($plaintext, 4, $unpack['len']);
// get app_key
$app_key_decode = substr($plaintext, $unpack['len'] + 4);
return $app_key == $app_key_decode ? $content : false;
}
以上這篇php解密小程序sessionKey 的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持創(chuàng)新互聯(lián)。
網(wǎng)站欄目:php解密小程序sessionKey的方法
鏈接地址:http://fisionsoft.com.cn/article/dhjsoci.html


咨詢
建站咨詢
