已经找到根源了,详情去看我博客吧 微信小程序加密解密 C# 以及 填充无效,无法被移除错误的解决方案 Padding is invalid and cannot be removed
c# 解密手机{"填充无效,无法被移除。"} 怎么搞?网上各种方法均试过,·同样问题
2019-11-10已经找到根源了,详情去看我博客吧 微信小程序加密解密 C# 以及 填充无效,无法被移除错误的解决方案 Padding is invalid and cannot be removed
填充无效无法被移除[图片] 有时可以,有时不可以,直接报填充无效无法被移除, 加密数据:FJOD8oQ8l/4awbPuNMfdb+oQjMFjYlgDyx8qOkbViMG5CCs8OlSFs66Y2oYgRFg40oA98lansvmsOFQ3h4+dW19mNo0c+dCWpLPf2XT6UzZ16BL+QPIN+29pqmrDbR3BEd2dlaOsiZmeAVQFov2TPfANZyLMBZNk9w9cA8IGoivQ/0vZoy3teFfeHykTdsjtZgLt4uBH9VBsoHzXyZCCQzcprmIabNjgZ2zEeGcrBJD5OhlDSJftTint9tPYNdRZo1GgZpkdf7rupzc9pQ0aOXNL/LdkLWl/GutI6OOpb1Vs3prBn2dnMn7zv/SshlR3wk91RwMhpL7gEy4QmEJVzPQgwBjFGyE68e5NYwxXdHf4cGLeckyChsaeIvSqi1RR1C4CpKcsxNI/ZrUvKmmOEADm6vR4xiXShUT8iYQLyPSNSetOoNEIv4Lqutu9Vh2yhHszW47vS/N0zZJ0hz5bp/m1v2i3qzOfOV/6Lh1YEG8= 密钥:H186u5/0NMFynRLcEv0CKQ== IV:bH0I4SMZEc33MlSoezDwSA== 请问是不是密钥或者IV出错了
2019-11-10已经找到根源了,详情去看我博客吧 微信小程序加密解密 C# 以及 填充无效,无法被移除错误的解决方案 Padding is invalid and cannot be removed
小程序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; } }
2019-11-10已经找到根源了,详情去看我博客吧 微信小程序加密解密 C# 以及 填充无效,无法被移除错误的解决方案 Padding is invalid and cannot be removed
微信小程序解密偶发性的 "填充无效,无法被移除"C#基础代码片段: private static byte[] AES_decrypt(string input, string iv, string key) { RijndaelManaged aes = new RijndaelManaged(); aes.KeySize = 256; aes.BlockSize = 128; aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.PKCS7; aes.Key = Convert.FromBase64String(key); aes.IV = Convert.FromBase64String(iv); var decrypt = aes.CreateDecryptor(aes.Key, aes.IV); byte[] xBuff = null; using (var ms = new MemoryStream()) { using (var cs = new CryptoStream(ms, decrypt, CryptoStreamMode.Write)) { byte[] xXml = Convert.FromBase64String(input); byte[] msg = new byte[xXml.Length + 32 - xXml.Length % 32]; Array.Copy(xXml, msg, xXml.Length); cs.Write(xXml, 0, xXml.Length); } xBuff = decode2(ms.ToArray()); } return xBuff; } private static byte[] decode2(byte[] decrypted) { int pad = (int)decrypted[decrypted.Length - 1]; if (pad < 1 || pad > 32) { pad = 0; } byte[] res = new byte[decrypted.Length - pad]; Array.Copy(decrypted, 0, res, 0, decrypted.Length - pad); return res; } 错误日志: ExecptionMessage:Padding is invalid and cannot be removed. mscorlib at System.Security.Cryptography.RijndaelManagedTransform.DecryptData(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[]& outputBuffer, Int32 outputOffset, PaddingMode paddingMode, Boolean fLast) at System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) at System.Security.Cryptography.CryptoStream.FlushFinalBlock() at System.Security.Cryptography.CryptoStream.Dispose(Boolean disposing) at System.IO.Stream.Close() at System.IO.Stream.Dispose() at Senparc.Weixin.WxOpen.Helpers.EncryptHelper.AES_Decrypt(String Input, Byte[] Iv, Byte[] Key) at Senparc.Weixin.WxOpen.Helpers.EncryptHelper.DecodeEncryptedData(String sessionKey, String encryptedData, String iv) 加密数据: sessionKey:98195476102492321891401391061935624977242;IV:116115241129461711788323441202601974169239; encryptedData:deWfUALVVTxrux2cp0qeqLWotTHTIpRmIrpcuWoh3ngyr7vjCDYq1wh2Q0CE6Zj9P/V2ZVqtjkVAiGdBuBR8fSs9qpWhb9ieO5FoumuvgoM6HP5+7Eul6lm8njXJlbTZr+pODAIeMoBIwpQUPpCLwYtpSuKlQGKvsrmoVU5j5xgoKm4dyKmNwq3qcqE5Q+HUOV0r/c7GusFWZD0haaccduMjmKAyupCpbwdDu6kiVfEo1pVZdp5j4C5ihrZdE7gzeS9vOAFDaB+NXPB6Lz+H8js6BH8gVJ7tZ1KUAwqt+FIqHHBKsREKoyjePwREkRc1Sr/N+QR1vps2cFGpqp16NAoTyT/JFi2jNs8PgrrEYZkjVvyMUYFlDnq5BWNyyh5RX34JEq7EN62sc+wfAMB2Nrm/QEcBCtYLycP3xcQnCLasU2SQbpIr5GOUz7aiIu5rwMXMUDDg7jxCOA4+ORfSHUgS6OczRjY+QqrcfKmlA84=
2019-11-10同样的应用场景,怎么解决的,用getCurrentPages?
wx.navigateBack 没有上一页时不会跳转首页wx.navigateBack({ delta: 1 }); 没有上一级页面时 ,页面无任何反映
2019-09-21