小程序getPhoneNumber 解密手机号时,偶尔会出现填充无效,无法被移除
客户端 getPhoneNumber: function (e) { let that=this; wx.login({ success: function (res) { if (e.detail.errMsg == 'getPhoneNumber:fail user deny') { wx.navigateTo({ url: '../../pages/userLogin/userLogin', }); } else { if (e.detail.errMsg == 'getPhoneNumber:fail:cancel to confirm login') { wx.navigateTo({ url: '../../pages/userLogin/userLogin?url='+that.data.url }); } else { //console.log(res.code); wx.request({ url: api.AESdecrypt, data: { 'encryptedDataStr': e.detail.encryptedData, 'code': res.code, 'iv': e.detail.iv }, method: 'post', header: { 'content-type': 'application/x-www-form-urlencoded' }, success: function (data) { console.log(data); app.globalData.userInfo = data.data.data; setTimeout(function(){ wx.navigateBack({ changed: true });//返回上一页 },500); }, error: function () { tip.showToast('error'); }, fail: function (err) { tip.showToast(err); } }) } } } }); }, 服务端 try { string encryptedDataStr = System.Web.HttpContext.Current.Request["encryptedDataStr"], code = System.Web.HttpContext.Current.Request["code"], iv = System.Web.HttpContext.Current.Request["iv"]; ReturnData resultMsg = new ReturnData(); resultMsg.state = (int)StatusCodeEnum.Success; resultMsg.message = StatusCodeEnum.Success.GetEnumText(); string strresult = ""; string Appid = "Appid"; string Secret = "Secret "; string grant_type = "authorization_code"; GetUsersHelper GetUsersHelper = new Controllers.GetUsersHelper(); //向微信服务端 使用登录凭证 code 获取 session_key 和 openid string url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + Appid + "&secret=" + Secret + "&js_code=" + code + "&grant_type=" + grant_type; Stream s_re = WebRequest.Create(url).GetResponse().GetResponseStream(); StreamReader sr = new StreamReader(s_re, Encoding.UTF8); string strLine = sr.ReadToEnd(); sr.Close(); //将字符串转换为json格式 JObject jo = (JObject)JsonConvert.DeserializeObject(strLine); result res = new result(); try { //微信服务器验证成功 res.openid = jo["openid"].ToString(); res.session_key = jo["session_key"].ToString(); } catch (Exception) { //微信服务器验证失败 res.errcode = jo["errcode"].ToString(); res.errmsg = jo["errmsg"].ToString(); WriteInfo(res.errmsg); } if (!string.IsNullOrEmpty(res.openid)) { //用户数据解密 strresult = GetUsersHelper.AESDecrypt(encryptedDataStr, res.session_key,iv);//.AES_decrypt(encryptedDataStr,res.session_key,iv);// JObject jos = (JObject)JsonConvert.DeserializeObject(strresult); ResultInfo ress = new ResultInfo(); ress.phoneNumber = jos["phoneNumber"].ToString(); var model = Ioc.Get<IUserRepository>().FindMobile(ress.phoneNumber); resultMsg.data = model; } else { strresult = null; resultMsg.data = strresult; } return resultMsg; } catch (Exception ex) { WriteInfo(ex.Message); return new ReturnData { state=200, message="error", data=null }; } 解密.net public string AESDecrypt(string inputdata, string AesKey, string AesIV) { try { RijndaelManaged rijalg = new RijndaelManaged(); //----------------- //设置 cipher 格式 AES-128-CBC rijalg.KeySize = 128; rijalg.Padding = PaddingMode.PKCS7; rijalg.Mode = CipherMode.CBC; rijalg.Key = Convert.FromBase64String(AesKey); rijalg.IV = Convert.FromBase64String(AesIV); byte[] encryptedData = Convert.FromBase64String(inputdata); //解密 ICryptoTransform decryptor = rijalg.CreateDecryptor(rijalg.Key, rijalg.IV); string result; using (MemoryStream msDecrypt = new MemoryStream(encryptedData)) { using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { using (StreamReader srDecrypt = new StreamReader(csDecrypt)) { result = srDecrypt.ReadToEnd(); } } } return result; } catch (Exception ex) { ApiUserController apis = new ApiUserController(); apis.WriteInfo("jiemi"+ex.Message); return null; } }