微信小程序登录时偶尔报解密后得到的buffer非法问题
使用 laravel9 + php8.0。
用户点击登录,一般情况下都可以正常登录,仅少数情况下会出现以下问题:
代码如下:
private function decryptData($appid, $sessionKey, $encryptedData, $iv){
if(strlen($sessionKey) != 24){
throwBusinessException('encodingAesKey 非法');
}
$aesKey = base64_decode($sessionKey);
if(strlen($iv) != 24){
throwBusinessException('aes 解密失败');
}
$aesIV = base64_decode($iv);
$aesCipher = base64_decode($encryptedData);
$result = openssl_decrypt($aesCipher, "aes-128-cbc", $aesKey, OPENSSL_RAW_DATA, $aesIV);
$dataObj = json_decode($result);
if($dataObj == NULL){
throwBusinessException('解密后得到的buffer非法');
}
if($dataObj->watermark->appid != $appid){
throwBusinessException('base64解密失败');
}
$data = json_decode($result, true);
return $data;
}
控制器接收传参使用的是laravel中的 $request->input('code', '')
方法。
我个人猜测是框架或者哪里传参时把参数的某个特殊字符给转义了,但是没有查到。哪位大佬知道是哪里有问题吗
小程序端代码:
<view class="content">
<button class="login" openType="getUserInfo" lang="zh_CN" bindgetuserinfo="authorLogin">
<image src="{{utils.staticUrl}}/login/weixin.png"></image>
<text>微信一键登录</text>
</button>
</view>
authorLogin: function(e) {
let _this = this;
wx.login({
success: function(res) {
// 发送用户信息
App._post_form('api/login', {
code: res.code,
encryptedData: e.detail.encryptedData,
iv: e.detail.iv,
}, function(result) {
console.log(result)
}, false, function() {
wx.hideLoading();
});
}
});
}
为了省篇幅,上面的代码删除了一些跟问题无关的判断逻辑等代码
aes在js端的补码方式看看有没有用,现在看起来像补码方式会有点问题