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.

147 lines
5.1 KiB

  1. using NFine.Code;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Data;
  6. using System.Data.Common;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Security.Cryptography;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Web;
  14. namespace NFine.Application.Encrypt
  15. {
  16. public class EnciphermentApp
  17. {
  18. public void Encryption(string ICSInspections)
  19. {
  20. try
  21. {
  22. string url = HttpContext.Current.Request.Url.Host;
  23. if (url != "localhost")
  24. {
  25. throw new Exception("您无该操作权限,请联系管理员!");
  26. }
  27. var queryParam = ICSInspections.ToJObject();
  28. List<DbParameter> parameter = new List<DbParameter>();
  29. string DbTXT = queryParam["Encipherment"].ToString();
  30. string MD5DbTXT = ToMd5(DbTXT);
  31. SetValue("connstr", MD5DbTXT);
  32. }
  33. catch (Exception ex)
  34. {
  35. throw ex ;
  36. }
  37. }
  38. /// <summary>
  39. /// 根据Key修改Value
  40. /// </summary>
  41. /// <param name="key">要修改的Key</param>
  42. /// <param name="value">要修改为的值</param>
  43. public static void SetValue(string key, string value)
  44. {
  45. System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
  46. xDoc.Load(HttpContext.Current.Server.MapPath("~/Configs/database.config"));
  47. System.Xml.XmlNode xNode;
  48. System.Xml.XmlElement xElem1;
  49. System.Xml.XmlElement xElem2;
  50. xNode = xDoc.SelectSingleNode("//connectionStrings");
  51. xElem1 = (System.Xml.XmlElement)xNode.SelectSingleNode("//add[@name='" + key + "']");
  52. if (xElem1 != null) xElem1.SetAttribute("connectionString", value);
  53. else
  54. {
  55. xElem2 = xDoc.CreateElement("add");
  56. xElem2.SetAttribute("name", key);
  57. xElem2.SetAttribute("connectionString", value);
  58. xNode.AppendChild(xElem2);
  59. }
  60. xDoc.Save(HttpContext.Current.Server.MapPath("~/Configs/database.config"));
  61. }
  62. #region 字符串加解密
  63. /// <summary>
  64. /// MD5加密
  65. /// </summary>
  66. /// <param name="str"></param>
  67. /// <returns></returns>
  68. public static string ToMd5(string str)
  69. {
  70. return Encrypt(str, "&%#@?,:*_");
  71. }
  72. /// <summary>
  73. /// MD5解密
  74. /// </summary>
  75. /// <param name="str"></param>
  76. /// <returns></returns>
  77. public static string FromMd5(string str)
  78. {
  79. //return str;
  80. return Decrypt(str, "&%#@?,:*_");
  81. }
  82. /// <summary>
  83. /// 加密
  84. /// </summary>
  85. /// <param name="strText"></param>
  86. /// <param name="strEncrKey"></param>
  87. /// <returns></returns>
  88. private static String Encrypt(String strText, String strEncrKey)
  89. {
  90. Byte[] byKey = { };
  91. Byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
  92. try
  93. {
  94. byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8));
  95. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  96. Byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
  97. MemoryStream ms = new MemoryStream();
  98. CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV),
  99. CryptoStreamMode.Write);
  100. cs.Write(inputByteArray, 0, inputByteArray.Length);
  101. cs.FlushFinalBlock();
  102. return Convert.ToBase64String(ms.ToArray());
  103. }
  104. catch (Exception ex)
  105. {
  106. return ex.Message;
  107. }
  108. }
  109. /// <summary>
  110. /// 解密
  111. /// </summary>
  112. /// <param name="strText"></param>
  113. /// <param name="sDecrKey"></param>
  114. /// <returns></returns>
  115. private static String Decrypt(String strText, String sDecrKey)
  116. {
  117. Byte[] byKey = { };
  118. Byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
  119. Byte[] inputByteArray = new byte[strText.Length];
  120. try
  121. {
  122. byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));
  123. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  124. inputByteArray = Convert.FromBase64String(strText);
  125. MemoryStream ms = new MemoryStream();
  126. CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV),
  127. CryptoStreamMode.Write);
  128. cs.Write(inputByteArray, 0, inputByteArray.Length);
  129. cs.FlushFinalBlock();
  130. System.Text.Encoding encoding = System.Text.Encoding.UTF8;
  131. return encoding.GetString(ms.ToArray());
  132. }
  133. catch (Exception ex)
  134. {
  135. return ex.Message;
  136. }
  137. }
  138. #endregion
  139. }
  140. }