星火微课系统客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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, string key = "")
  20. {
  21. if (string.IsNullOrWhiteSpace(key))
  22. {
  23. key = QrcodeLoginKey.PadRight(16, '0');
  24. }
  25. // 256-AES key
  26. byte[] keyArray = UTF8Encoding.ASCII.GetBytes(key);
  27. byte[] toEncryptArray = UTF8Encoding.ASCII.GetBytes(Data);
  28. RijndaelManaged rDel = new RijndaelManaged
  29. {
  30. Key = keyArray,
  31. Mode = CipherMode.ECB,
  32. Padding = PaddingMode.PKCS7
  33. };
  34. ICryptoTransform cTransform = rDel.CreateEncryptor();
  35. byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0,
  36. toEncryptArray.Length);
  37. return BytesToHexString(resultArray);
  38. }
  39. /// <summary>
  40. /// AES 解密
  41. /// </summary>
  42. /// <param name="hexString">Encrypted 16 hex string</param>
  43. /// <returns>Decrypted string</returns>
  44. public static string AESDecrypt(string hexString, string key = "")
  45. {
  46. if (string.IsNullOrWhiteSpace(key))
  47. {
  48. key = QrcodeLoginKey.PadRight(16, '0');
  49. }
  50. // 256-AES key
  51. byte[] keyArray = UTF8Encoding.ASCII.GetBytes(key);
  52. byte[] toEncryptArray = HexStringToBytes(hexString);
  53. RijndaelManaged rDel = new RijndaelManaged
  54. {
  55. Key = keyArray,
  56. Mode = CipherMode.ECB,
  57. Padding = PaddingMode.PKCS7
  58. };
  59. ICryptoTransform cTransform = rDel.CreateDecryptor();
  60. byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0,
  61. toEncryptArray.Length);
  62. return UTF8Encoding.ASCII.GetString(resultArray);
  63. }
  64. /// <summary>
  65. /// Byte array to convert 16 hex string
  66. /// </summary>
  67. /// <param name="bytes">byte array</param>
  68. /// <returns>16 hex string</returns>
  69. public static string BytesToHexString(byte[] bytes)
  70. {
  71. StringBuilder returnStr = new StringBuilder();
  72. if (bytes != null || bytes.Length == 0)
  73. {
  74. for (int i = 0; i < bytes.Length; i++)
  75. {
  76. returnStr.Append(bytes[i].ToString("X2"));
  77. }
  78. }
  79. return returnStr.ToString();
  80. }
  81. /// <summary>
  82. /// 16 hex string converted to byte array
  83. /// </summary>
  84. /// <param name="hexString">16 hex string</param>
  85. /// <returns>byte array</returns>
  86. public static byte[] HexStringToBytes(string hexString)
  87. {
  88. if (hexString == null || hexString.Equals(""))
  89. {
  90. return null;
  91. }
  92. int length = hexString.Length / 2;
  93. if (hexString.Length % 2 != 0)
  94. {
  95. return null;
  96. }
  97. byte[] d = new byte[length];
  98. for (int i = 0; i < length; i++)
  99. {
  100. d[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
  101. }
  102. return d;
  103. }
  104. }
  105. }