EncryptHelper.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using System.Collections.Generic;
  3. using Library;
  4. using System.Text;
  5. namespace MySystem
  6. {
  7. public class EncryptHelper
  8. {
  9. public static string Encrypt1(SortedList<string, string> obj, string key)
  10. {
  11. string signstr = function.BuildQueryString(obj) + key;
  12. string sign = function.MD532(signstr).ToUpper();
  13. obj.Add("sign", sign);
  14. return AesEncrypt(Newtonsoft.Json.JsonConvert.SerializeObject(obj), key);
  15. }
  16. public static string[] Encrypt1(SortedList<string, object> obj, string key)
  17. {
  18. string signstr = "";
  19. foreach(string sub in obj.Keys)
  20. {
  21. if(sub == "pos_no" || sub == "merch_no")
  22. {
  23. signstr += sub + "=" + Newtonsoft.Json.JsonConvert.SerializeObject(obj[sub]).Replace("\"", "") + "&";
  24. }
  25. else
  26. {
  27. signstr += sub + "=" + obj[sub].ToString() + "&";
  28. }
  29. }
  30. signstr = signstr.TrimEnd('&');
  31. signstr += key;
  32. string sign = function.MD532(signstr).ToUpper();
  33. obj.Add("sign", sign);
  34. return new string[] {AesEncrypt(Newtonsoft.Json.JsonConvert.SerializeObject(obj), key), sign};
  35. }
  36. public static string AesEncrypt(string str, string key)
  37. {
  38. if (string.IsNullOrEmpty(str)) return null;
  39. Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);
  40. System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
  41. {
  42. Key = Encoding.UTF8.GetBytes(key),
  43. Mode = System.Security.Cryptography.CipherMode.ECB,
  44. Padding = System.Security.Cryptography.PaddingMode.PKCS7
  45. };
  46. System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor();
  47. Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
  48. return Convert.ToBase64String(resultArray, 0, resultArray.Length);
  49. }
  50. public static string AesDecrypt(string str, string key)
  51. {
  52. if (string.IsNullOrEmpty(str)) return null;
  53. byte[] toEncryptArray = Convert.FromBase64String(str);
  54. System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
  55. {
  56. Key = Encoding.UTF8.GetBytes(key),
  57. Mode = System.Security.Cryptography.CipherMode.ECB,
  58. Padding = System.Security.Cryptography.PaddingMode.PKCS7,
  59. };
  60. System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateDecryptor();
  61. byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
  62. return Encoding.UTF8.GetString(resultArray);
  63. }
  64. }
  65. }