星火批注
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4. namespace Common
  5. {
  6. /// <summary>
  7. /// AES加密
  8. /// 创建人:赵耀
  9. /// 创建时间:2020年9月7日
  10. /// </summary>
  11. public class AESHelper
  12. {
  13. public static string QrcodeLoginKey = "zyyxhlywkdatakey";
  14. /// <summary>
  15. /// AES 加密
  16. /// </summary>
  17. /// <param name="content">Need encrypted string</param>
  18. /// <returns>Encrypted 16 hex string</returns>
  19. public static string AESEncrypt(string Data)
  20. {
  21. string key = QrcodeLoginKey.PadRight(16, '0');
  22. // 256-AES key
  23. byte[] keyArray = UTF8Encoding.ASCII.GetBytes(key);
  24. byte[] toEncryptArray = UTF8Encoding.ASCII.GetBytes(Data);
  25. RijndaelManaged rDel = new RijndaelManaged
  26. {
  27. Key = keyArray,
  28. Mode = CipherMode.ECB,
  29. Padding = PaddingMode.PKCS7
  30. };
  31. ICryptoTransform cTransform = rDel.CreateEncryptor();
  32. byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0,
  33. toEncryptArray.Length);
  34. return BytesToHexString(resultArray);
  35. }
  36. /// <summary>
  37. /// AES 解密
  38. /// </summary>
  39. /// <param name="hexString">Encrypted 16 hex string</param>
  40. /// <returns>Decrypted string</returns>
  41. public static string AESDecrypt(string hexString)
  42. {
  43. string key = QrcodeLoginKey.PadRight(16, '0');
  44. // 256-AES key
  45. byte[] keyArray = UTF8Encoding.ASCII.GetBytes(key);
  46. byte[] toEncryptArray = HexStringToBytes(hexString);
  47. RijndaelManaged rDel = new RijndaelManaged
  48. {
  49. Key = keyArray,
  50. Mode = CipherMode.ECB,
  51. Padding = PaddingMode.PKCS7
  52. };
  53. ICryptoTransform cTransform = rDel.CreateDecryptor();
  54. byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0,
  55. toEncryptArray.Length);
  56. return UTF8Encoding.ASCII.GetString(resultArray);
  57. }
  58. /// <summary>
  59. /// Byte array to convert 16 hex string
  60. /// </summary>
  61. /// <param name="bytes">byte array</param>
  62. /// <returns>16 hex string</returns>
  63. public static string BytesToHexString(byte[] bytes)
  64. {
  65. StringBuilder returnStr = new StringBuilder();
  66. if (bytes != null || bytes.Length == 0)
  67. {
  68. for (int i = 0; i < bytes.Length; i++)
  69. {
  70. returnStr.Append(bytes[i].ToString("X2"));
  71. }
  72. }
  73. return returnStr.ToString();
  74. }
  75. /// <summary>
  76. /// 16 hex string converted to byte array
  77. /// </summary>
  78. /// <param name="hexString">16 hex string</param>
  79. /// <returns>byte array</returns>
  80. public static byte[] HexStringToBytes(string hexString)
  81. {
  82. if (hexString == null || hexString.Equals(""))
  83. {
  84. return null;
  85. }
  86. int length = hexString.Length / 2;
  87. if (hexString.Length % 2 != 0)
  88. {
  89. return null;
  90. }
  91. byte[] d = new byte[length];
  92. for (int i = 0; i < length; i++)
  93. {
  94. d[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
  95. }
  96. return d;
  97. }
  98. }
  99. }