星火微课系统客户端
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

AESHelper.cs 3.5KB

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