终端一体化运控平台
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

86 lines
3.0 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace BPASmartClient.Helper
  8. {
  9. public static class Aes128Helper
  10. {
  11. /// <summary>
  12. /// AES 加密
  13. /// </summary>
  14. /// <param name="str">明文(待加密)</param>
  15. /// <param name="aesSecretKey">密钥</param>
  16. /// <returns></returns>
  17. //public static string AESEncrypt(this string str, string aesSecretKey = "bpa20210418bpa20210418bpa*******")
  18. //{
  19. // try
  20. // {
  21. // if (string.IsNullOrEmpty(str)) return null;
  22. // byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);
  23. // byte[] keyArray = UTF8Encoding.UTF8.GetBytes(aesSecretKey);
  24. // RijndaelManaged rm = new RijndaelManaged
  25. // {
  26. // Key = keyArray,
  27. // Mode = CipherMode.ECB,
  28. // Padding = PaddingMode.PKCS7
  29. // };
  30. // ICryptoTransform cTransform = rm.CreateEncryptor();
  31. // Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
  32. // string strs = "";
  33. // for (var x = 0; x < resultArray.Length; x++)
  34. // {
  35. // var i = Convert.ToString(resultArray[x], 16);
  36. // strs += i.PadLeft(2, '0');
  37. // }
  38. // return strs;
  39. // }
  40. // catch (Exception ex)
  41. // {
  42. // return "";
  43. // }
  44. //}
  45. /// <summary>
  46. /// AES 解密
  47. /// </summary>
  48. /// <param name="str">密文(待解密)</param>
  49. /// <param name="aesSecretKey">密钥</param>
  50. /// <returns></returns>
  51. //public static string AESDecrypt(this string str, string aesSecretKey = "bpa20210418bpa20210418bpa*******")
  52. //{
  53. // try
  54. // {
  55. // if (string.IsNullOrEmpty(str)) return null;
  56. // byte[] keyArray = UTF8Encoding.UTF8.GetBytes(aesSecretKey);
  57. // byte[] toEncryptArray = new byte[str.Length / 2];
  58. // for (var x = 0; x < toEncryptArray.Length; x++)
  59. // {
  60. // var i = Convert.ToInt32(str.Substring(x * 2, 2), 16);
  61. // toEncryptArray[x] = (byte)i;
  62. // }
  63. // RijndaelManaged rDel = new RijndaelManaged();
  64. // rDel.Key = keyArray;
  65. // rDel.Mode = CipherMode.ECB;
  66. // rDel.Padding = PaddingMode.PKCS7;
  67. // ICryptoTransform cTransform = rDel.CreateDecryptor();
  68. // byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
  69. // return UTF8Encoding.UTF8.GetString(resultArray);
  70. // }
  71. // catch (Exception ex)
  72. // {
  73. // return "";
  74. // }
  75. //}
  76. }
  77. }