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

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