dbconn.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Data;
  3. using System.Text;
  4. using System.Security.Cryptography;
  5. using Microsoft.Extensions.Caching.Memory;
  6. using System.IO;
  7. namespace Common
  8. {
  9. public class Dbconn
  10. {
  11. public static void InsertCache(string key, object val)
  12. {
  13. new MemoryCache(new MemoryCacheOptions()).Set(key, val, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMinutes(10)));
  14. }
  15. public static void InsertCache(string key, object val, int minutes)
  16. {
  17. new MemoryCache(new MemoryCacheOptions()).Set(key, val, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMinutes(minutes)));
  18. }
  19. public static void InsertCache(string key, object val, DateTime endtime)
  20. {
  21. new MemoryCache(new MemoryCacheOptions()).Set(key, val);
  22. }
  23. /// <summary>
  24. /// des解密
  25. /// </summary>
  26. /// <param name="content"></param>
  27. /// <param name="key"></param>
  28. /// <returns></returns>
  29. public static string DesDecrypt(string content, string key = "yun1mu23")
  30. {
  31. try
  32. {
  33. content = content.Replace(" ", "+");
  34. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  35. byte[] inputByteArray = Convert.FromBase64String(content);
  36. des.Key = ASCIIEncoding.ASCII.GetBytes(key);
  37. des.IV = ASCIIEncoding.ASCII.GetBytes(key);
  38. MemoryStream ms = new MemoryStream();
  39. CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
  40. cs.Write(inputByteArray, 0, inputByteArray.Length);
  41. cs.FlushFinalBlock();
  42. return System.Text.Encoding.Default.GetString(ms.ToArray());
  43. }
  44. catch (Exception ex)
  45. {
  46. Function.WriteLog(DateTime.Now.ToString() + "\r\n" + content + "\r\n" + ex.ToString(), "des解密异常");
  47. return "{}";
  48. }
  49. }
  50. public static string DesEncrypt(string content, string key = "yun1mu23")
  51. {
  52. try
  53. {
  54. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  55. byte[] inputByteArray = System.Text.Encoding.Default.GetBytes(content);
  56. des.Key = ASCIIEncoding.ASCII.GetBytes(key);
  57. des.IV = ASCIIEncoding.ASCII.GetBytes(key);
  58. MemoryStream ms = new MemoryStream();
  59. CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
  60. cs.Write(inputByteArray, 0, inputByteArray.Length);
  61. cs.FlushFinalBlock();
  62. return Convert.ToBase64String(ms.ToArray());
  63. }
  64. catch (Exception ex)
  65. {
  66. Function.WriteLog(DateTime.Now.ToString() + "\r\n" + content + "\r\n" + ex.ToString(), "des加密异常");
  67. return "{}";
  68. }
  69. }
  70. public static string Encrypt3DES(string a_strString, string Key3des = "yun1mu23")
  71. {
  72. DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
  73. DES.Key = Encoding.UTF8.GetBytes(Key3des);
  74. DES.Mode = CipherMode.ECB;
  75. DES.Padding = PaddingMode.Zeros;
  76. ICryptoTransform DESEncrypt = DES.CreateEncryptor();
  77. byte[] Buffer = Encoding.UTF8.GetBytes(a_strString);
  78. return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
  79. }
  80. public static string Decrypt3DES(string a_strString, string Key3des = "yun1mu23")
  81. {
  82. DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
  83. DES.Key = Encoding.UTF8.GetBytes(Key3des);
  84. DES.Mode = CipherMode.ECB;
  85. DES.Padding = PaddingMode.Zeros;
  86. ICryptoTransform DESDecrypt = DES.CreateDecryptor();
  87. byte[] Buffer = Convert.FromBase64String(a_strString);
  88. return UTF8Encoding.UTF8.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
  89. }
  90. public static string AesEncrypt(string str, string key, string iv)
  91. {
  92. if (string.IsNullOrEmpty(str)) return null;
  93. Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);
  94. System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
  95. {
  96. Key = Encoding.UTF8.GetBytes(key),
  97. IV = Encoding.UTF8.GetBytes(iv),
  98. Mode = System.Security.Cryptography.CipherMode.CBC,
  99. Padding = System.Security.Cryptography.PaddingMode.PKCS7
  100. };
  101. System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor();
  102. Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
  103. return Convert.ToBase64String(resultArray, 0, resultArray.Length);
  104. }
  105. public static string AesDecrypt(string str, string key, string iv)
  106. {
  107. if (string.IsNullOrEmpty(str)) return null;
  108. Byte[] toEncryptArray = Convert.FromBase64String(str);
  109. System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
  110. {
  111. Key = Encoding.UTF8.GetBytes(key),
  112. IV = Encoding.UTF8.GetBytes(iv),
  113. Mode = System.Security.Cryptography.CipherMode.CBC,
  114. Padding = System.Security.Cryptography.PaddingMode.PKCS7
  115. };
  116. System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateDecryptor();
  117. Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
  118. return Encoding.UTF8.GetString(resultArray);
  119. }
  120. }
  121. }