using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Security; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ShwasherSys { public class WxDecryptHelper { public static string Decrypt(string encryptedData, string sessionKey, string iv) { try { // Base64解码 var encryptedDataBytes = Convert.FromBase64String(encryptedData); var keyBytes = Convert.FromBase64String(sessionKey); var ivBytes = Convert.FromBase64String(iv); // 创建AES解密器 var cipher = CipherUtilities.GetCipher("AES/CBC/PKCS7Padding"); var keyParam = new KeyParameter(keyBytes); var keyParameters = new ParametersWithIV(keyParam, ivBytes); cipher.Init(false, keyParameters); var resultBytes = cipher.DoFinal(encryptedDataBytes); // 返回解密后的JSON字符串 return Encoding.UTF8.GetString(resultBytes); } catch (Exception ex) { throw new Exception("解密失败", ex); } } } }