纽威
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.

113 lines
3.7 KiB

3 years ago
  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 NFine.Code
  9. {
  10. public class DBConnection
  11. {
  12. public static bool Encrypt { get; set; }
  13. public DBConnection(bool encrypt)
  14. {
  15. Encrypt = encrypt;
  16. }
  17. public static string connectionString
  18. {
  19. get
  20. {
  21. string connection = FromMd5( System.Configuration.ConfigurationManager.ConnectionStrings["connstr"].ConnectionString);
  22. if (Encrypt == true)
  23. {
  24. return DESEncrypt.Decrypt(connection);
  25. }
  26. else
  27. {
  28. return connection;
  29. }
  30. }
  31. }
  32. #region 字符串加解密
  33. /// <summary>
  34. /// MD5加密
  35. /// </summary>
  36. /// <param name="str"></param>
  37. /// <returns></returns>
  38. public static string ToMd5(string str)
  39. {
  40. return Encrypts(str, "&%#@?,:*_");
  41. }
  42. /// <summary>
  43. /// MD5解密
  44. /// </summary>
  45. /// <param name="str"></param>
  46. /// <returns></returns>
  47. public static string FromMd5(string str)
  48. {
  49. return str;
  50. //return Decrypt(str, "&%#@?,:*_");
  51. }
  52. /// <summary>
  53. /// 加密
  54. /// </summary>
  55. /// <param name="strText"></param>
  56. /// <param name="strEncrKey"></param>
  57. /// <returns></returns>
  58. private static String Encrypts(String strText, String strEncrKey)
  59. {
  60. Byte[] byKey = { };
  61. Byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
  62. try
  63. {
  64. byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8));
  65. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  66. Byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
  67. MemoryStream ms = new MemoryStream();
  68. CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV),
  69. CryptoStreamMode.Write);
  70. cs.Write(inputByteArray, 0, inputByteArray.Length);
  71. cs.FlushFinalBlock();
  72. return Convert.ToBase64String(ms.ToArray());
  73. }
  74. catch (Exception ex)
  75. {
  76. return ex.Message;
  77. }
  78. }
  79. /// <summary>
  80. /// 解密
  81. /// </summary>
  82. /// <param name="strText"></param>
  83. /// <param name="sDecrKey"></param>
  84. /// <returns></returns>
  85. private static String Decrypt(String strText, String sDecrKey)
  86. {
  87. Byte[] byKey = { };
  88. Byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
  89. Byte[] inputByteArray = new byte[strText.Length];
  90. try
  91. {
  92. byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));
  93. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  94. inputByteArray = Convert.FromBase64String(strText);
  95. MemoryStream ms = new MemoryStream();
  96. CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV),
  97. CryptoStreamMode.Write);
  98. cs.Write(inputByteArray, 0, inputByteArray.Length);
  99. cs.FlushFinalBlock();
  100. System.Text.Encoding encoding = System.Text.Encoding.UTF8;
  101. return encoding.GetString(ms.ToArray());
  102. }
  103. catch (Exception ex)
  104. {
  105. return ex.Message;
  106. }
  107. }
  108. #endregion
  109. }
  110. }