WxDecryptHelper.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using Org.BouncyCastle.Crypto.Parameters;
  2. using Org.BouncyCastle.Security;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace ShwasherSys
  9. {
  10. public class WxDecryptHelper
  11. {
  12. public static string Decrypt(string encryptedData, string sessionKey, string iv)
  13. {
  14. try
  15. {
  16. // Base64解码
  17. var encryptedDataBytes = Convert.FromBase64String(encryptedData);
  18. var keyBytes = Convert.FromBase64String(sessionKey);
  19. var ivBytes = Convert.FromBase64String(iv);
  20. // 创建AES解密器
  21. var cipher = CipherUtilities.GetCipher("AES/CBC/PKCS7Padding");
  22. var keyParam = new KeyParameter(keyBytes);
  23. var keyParameters = new ParametersWithIV(keyParam, ivBytes);
  24. cipher.Init(false, keyParameters);
  25. var resultBytes = cipher.DoFinal(encryptedDataBytes);
  26. // 返回解密后的JSON字符串
  27. return Encoding.UTF8.GetString(resultBytes);
  28. }
  29. catch (Exception ex)
  30. {
  31. throw new Exception("解密失败", ex);
  32. }
  33. }
  34. }
  35. }