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.

146 lines
4.8 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. using Microsoft.EntityFrameworkCore.Metadata.Internal;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Security.Cryptography;
  8. using System.IO;
  9. using Org.BouncyCastle.Crypto.Parameters;
  10. using Org.BouncyCastle.Crypto;
  11. using Org.BouncyCastle.Security;
  12. using Org.BouncyCastle.Utilities;
  13. namespace Shentun.Peis
  14. {
  15. public class AesHelper
  16. {
  17. private static readonly byte[] Key = Encoding.UTF8.GetBytes("1234567812345678"); // 16字节密钥
  18. private static readonly byte[] Iv = Encoding.UTF8.GetBytes("1234567812345678"); // 16字节初始化向量
  19. /// <summary>
  20. /// MD5加密
  21. /// </summary>
  22. /// <param name="input"></param>
  23. /// <returns></returns>
  24. public static string GetMD5Hash(string input)
  25. {
  26. using (MD5 md5 = MD5.Create())
  27. {
  28. byte[] inputBytes = Encoding.UTF8.GetBytes(input);
  29. byte[] hashBytes = md5.ComputeHash(inputBytes);
  30. // Convert byte array to a hexadecimal string
  31. StringBuilder sb = new StringBuilder();
  32. for (int i = 0; i < hashBytes.Length; i++)
  33. {
  34. sb.Append(hashBytes[i].ToString("x2"));
  35. }
  36. return sb.ToString();
  37. }
  38. }
  39. /// <summary>
  40. /// MD5加密
  41. /// </summary>
  42. /// <param name="input"></param>
  43. /// <returns></returns>
  44. public static string GetMD5Hash2(byte[] input)
  45. {
  46. using (MD5 md5 = MD5.Create())
  47. {
  48. //byte[] inputBytes = Encoding.UTF8.GetBytes(input);
  49. byte[] hashBytes = md5.ComputeHash(input);
  50. // Convert byte array to a hexadecimal string
  51. StringBuilder sb = new StringBuilder();
  52. for (int i = 0; i < hashBytes.Length; i++)
  53. {
  54. sb.Append(hashBytes[i].ToString("X2"));
  55. }
  56. return sb.ToString();
  57. }
  58. }
  59. /// <summary>
  60. /// 解密
  61. /// </summary>
  62. /// <param name="strinput"></param>
  63. /// <param name="strkey"></param>
  64. /// <returns></returns>
  65. public static string AESDecrypt(string strinput, string strkey)
  66. {
  67. try
  68. {
  69. //string dummyData = strinput.Trim().Replace("%", "").Replace(",", "").Replace(" ", "+").Replace("_", "/").Replace("-", "+");
  70. //if (dummyData.Length % 4 > 0)
  71. //{
  72. // dummyData = dummyData.PadRight(dummyData.Length + 4 - dummyData.Length % 4, '=');
  73. //}
  74. byte[] inputArray = Convert.FromBase64String(strinput);
  75. byte[] key = Encoding.UTF8.GetBytes(strkey);
  76. IBufferedCipher cipher = CipherUtilities.GetCipher("AES/ECB/PKCS5Padding");
  77. cipher.Init(false, AesKey(strkey));
  78. // cipher.Init(false, new ParametersWithIV(new DesParameters(key), key)); 带IV向量的算法可以用到。
  79. byte[] rv = new byte[cipher.GetOutputSize(inputArray.Length)];
  80. int tam = cipher.ProcessBytes(inputArray, 0, inputArray.Length, rv, 0);
  81. cipher.DoFinal(rv, tam);
  82. return Encoding.UTF8.GetString(rv);
  83. }
  84. catch (Exception ex)
  85. {
  86. return "";
  87. }
  88. }
  89. /// <summary>
  90. /// 加密
  91. /// </summary>
  92. /// <param name="inputArray"></param>
  93. /// <param name="strkey"></param>
  94. /// <returns></returns>
  95. public static byte[] AESEncrypt(byte[] inputArray, string strkey)
  96. {
  97. try
  98. {
  99. //byte[] inputArray = Encoding.UTF8.GetBytes(strinput);
  100. byte[] key = Encoding.UTF8.GetBytes(strkey);
  101. IBufferedCipher cipher = CipherUtilities.GetCipher("AES/ECB/PKCS5Padding");
  102. //cipher.Init(true, new ParametersWithIV(new DesParameters(key), key));
  103. cipher.Init(true, AesKey(strkey));
  104. byte[] rv = new byte[cipher.GetOutputSize(inputArray.Length)];
  105. int tam = cipher.ProcessBytes(inputArray, 0, inputArray.Length, rv, 0);
  106. cipher.DoFinal(rv, tam);
  107. //return Convert.ToBase64String(rv);
  108. return rv;
  109. }
  110. catch (Exception ex)
  111. {
  112. return null;
  113. }
  114. }
  115. public static KeyParameter AesKey(String key)
  116. {
  117. byte[] bs = Encoding.Default.GetBytes(key);
  118. if (bs.Length != 16)
  119. {
  120. bs = Arrays.CopyOf(bs, 16);// 处理数组长度为16(不足16位,不空;超过16位取前16位)
  121. }
  122. return new KeyParameter(bs);
  123. }
  124. }
  125. }