EncryptHelper.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using System.Collections.Generic;
  3. using Library;
  4. using System.Text;
  5. using System.Linq;
  6. namespace MySystem
  7. {
  8. public class EncryptHelper
  9. {
  10. public static string Encrypt1(SortedList<string, string> obj, string key)
  11. {
  12. SortedList<string, string> objnew = new SortedList<string, string>();
  13. foreach(string field in obj.Keys)
  14. {
  15. if(!string.IsNullOrEmpty(obj[field])) objnew.Add(field, obj[field]);
  16. }
  17. string signstr = function.BuildQueryString(objnew) + key;
  18. string sign = function.MD532(signstr).ToUpper();
  19. obj.Add("sign", sign);
  20. return AesEncrypt(Newtonsoft.Json.JsonConvert.SerializeObject(obj), key);
  21. }
  22. public static string AesEncrypt(string str, string key)
  23. {
  24. if (string.IsNullOrEmpty(str)) return null;
  25. Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);
  26. System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
  27. {
  28. Key = Encoding.UTF8.GetBytes(key),
  29. Mode = System.Security.Cryptography.CipherMode.ECB,
  30. Padding = System.Security.Cryptography.PaddingMode.PKCS7
  31. };
  32. System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor();
  33. Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
  34. return Convert.ToBase64String(resultArray, 0, resultArray.Length);
  35. }
  36. }
  37. }