星火微课系统客户端
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.

AESHelper.cs 3.5KB

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