AESHelper.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace IoTIntegrationPlatform.Common
  9. {
  10. /// <summary>
  11. /// AES加解密
  12. /// </summary>
  13. public static class AESHelper
  14. {
  15. /// <summary>
  16. /// AES加密
  17. /// </summary>
  18. /// <param name="encryptStr">明文</param>
  19. /// <param name="key">密钥</param>
  20. /// <returns></returns>
  21. public static byte[] Encrypt(string encryptStr, string key)
  22. {
  23. //byte[] keyArray =Encoding.Default.GetBytes(key);
  24. //byte[] toEncryptArray = Encoding.Default.GetBytes(encryptStr);
  25. //RijndaelManaged rDel = new RijndaelManaged();
  26. //rDel.Key = keyArray;
  27. ////rDel.KeySize = 128;
  28. //rDel.Mode = CipherMode.ECB;
  29. //rDel.Padding = PaddingMode.PKCS7;
  30. //ICryptoTransform cTransform = rDel.CreateEncryptor();
  31. //byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
  32. //return Encoding.Default.GetString(resultArray);
  33. AesCryptoServiceProvider aes = new AesCryptoServiceProvider
  34. {
  35. Key = Encoding.Default.GetBytes(key),
  36. Mode = CipherMode.ECB,
  37. Padding = PaddingMode.PKCS7
  38. };
  39. using (MemoryStream ms = new MemoryStream())
  40. {
  41. byte[] bytes = Encoding.Default.GetBytes(encryptStr);
  42. using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(aes.Key, aes.IV), CryptoStreamMode.Write))
  43. {
  44. cs.Write(bytes, 0, bytes.Count());
  45. cs.FlushFinalBlock();
  46. }
  47. return ms.ToArray();
  48. }
  49. }
  50. /// <summary>
  51. /// AES解密
  52. /// </summary>
  53. /// <param name="decryptStr">密文</param>
  54. /// <param name="key">密钥</param>
  55. /// <returns></returns>
  56. public static byte[] Decrypt(byte[] bytes, string key)
  57. {
  58. //byte[] keyArray = Encoding.Default.GetBytes(key);
  59. //byte[] toEncryptArray = Encoding.Default.GetBytes(decryptStr);
  60. //RijndaelManaged rDel = new RijndaelManaged();
  61. //rDel.Key = keyArray;
  62. //// rDel.KeySize = 128;
  63. //rDel.Mode = CipherMode.ECB;
  64. //rDel.Padding = PaddingMode.PKCS7;
  65. //ICryptoTransform cTransform = rDel.CreateDecryptor();
  66. //byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
  67. //return Encoding.Default.GetString(resultArray);
  68. AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
  69. aes.Key = Encoding.Default.GetBytes(key);
  70. aes.Mode = CipherMode.ECB;
  71. aes.Padding = PaddingMode.PKCS7;
  72. using (MemoryStream ms = new MemoryStream())
  73. {
  74. //byte[] bytes = Convert.FromBase64String(decryptStr);
  75. using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(aes.Key, aes.IV), CryptoStreamMode.Write))
  76. {
  77. cs.Write(bytes, 0, bytes.Count());
  78. cs.FlushFinalBlock();
  79. }
  80. return ms.ToArray();
  81. }
  82. }
  83. /// <summary>
  84. /// 2进制转16进制
  85. /// </summary>
  86. public static String Hex_2To16(Byte[] bytes)
  87. {
  88. String hexString = String.Empty;
  89. Int32 iLength = 65535;
  90. if (bytes != null)
  91. {
  92. StringBuilder strB = new StringBuilder();
  93. if (bytes.Length < iLength)
  94. {
  95. iLength = bytes.Length;
  96. }
  97. for (int i = 0; i < iLength; i++)
  98. {
  99. strB.Append(bytes[i].ToString("X2"));
  100. }
  101. hexString = strB.ToString();
  102. }
  103. return hexString;
  104. }
  105. /// <summary>
  106. /// 16进制转2进制
  107. /// </summary>
  108. public static Byte[] Hex_16To2(String hexString)
  109. {
  110. if ((hexString.Length % 2) != 0)
  111. {
  112. hexString += " ";
  113. }
  114. Byte[] returnBytes = new Byte[hexString.Length / 2];
  115. for (Int32 i = 0; i < returnBytes.Length; i++)
  116. {
  117. returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
  118. }
  119. return returnBytes;
  120. }
  121. }
  122. }