这是微信提供的Demo解码代码,我对它进行了一部分的修改,目前只需要引入这一个文件即可使用
class WxBizDataCrypt
{
private $app_id;
private $session_key;
public static $OK = 0;
public static $IllegalAesKey = -41001;
public static $IllegalIv = -41002;
public static $IllegalBuffer = -41003;
public static $DecodeBase64Error = -41004;
public function __construct( $app_id, $session_key)
{
$this->session_key = $session_key;
$this->app_id = $app_id;
}
public function decryptData( $encryptedData, $iv, &$data )
{
if (strlen($this->session_key) != 24) {
return self::$IllegalAesKey;
}
$aesKey=base64_decode($this->session_key);
if (strlen($iv) != 24) {
return self::$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 self::$IllegalBuffer;
}
if( $dataObj->watermark->appid != $this->app_id )
{
return self::$IllegalBuffer;
}
$data = $result;
return self::$OK;
}
}
这里输入iv向量和encryptedData敏感数据在内的完整用户信息的加密数据 可以解码出以下数据:
{
"openId": "OPENID",
"nickName": "NICKNAME",
"gender": GENDER,
"city": "CITY",
"province": "PROVINCE",
"country": "COUNTRY",
"avatarUrl": "AVATARURL",
"unionId": "UNIONID",
"watermark": {
"appid":"APPID",
"timestamp":TIMESTAMP
}
}
参考文章:
微信小程序官方文档
服务端获取开放数据