解决问题,填充无效,无法被移除 var rgbKey = Convert.FromBase64String(AesKey); var rgbIV = Convert.FromBase64String(AesIV); RijndaelManaged rijndael = new RijndaelManaged(); ICryptoTransform transform = rijndael.CreateDecryptor(rgbKey, rgbIV); byte[] bCipherText = Convert.FromBase64String(inputdata);//这里要用这个函数来正确转换Base64字符串成Byte数组 MemoryStream ms = new MemoryStream(bCipherText); CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Read); byte[] bPlainText = new byte[bCipherText.Length]; cs.Read(bPlainText, 0, bPlainText.Length); return Encoding.UTF8.GetString(bPlainText);
微信小程序解密偶发性的 "填充无效,无法被移除"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=
2020-06-04