ElementsConfig.cs.svn-base 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Security.Cryptography;
  5. using System.Xml;
  6. using System.Configuration;
  7. using System.IO;
  8. namespace Core.LgMes.Client.LgIntegrationQuery
  9. {
  10. public class ElementsConfig
  11. {
  12. public ElementsConfig()
  13. {
  14. }
  15. //密钥
  16. static string encryptKey = "HNSTCGLG";
  17. //默认密钥向量
  18. static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
  19. /// <summary>
  20. /// DES加密字符串
  21. /// </summary>
  22. /// <param name="encryptString">待加密的字符串</param>
  23. /// <param name="encryptKey">加密密钥,要求为8位</param>
  24. /// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
  25. public static string EncryptDES(string encryptString, string encryptKey)
  26. {
  27. try
  28. {
  29. byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
  30. byte[] rgbIV = Keys;
  31. byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
  32. DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
  33. MemoryStream mStream = new MemoryStream();
  34. CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
  35. cStream.Write(inputByteArray, 0, inputByteArray.Length);
  36. cStream.FlushFinalBlock();
  37. return Convert.ToBase64String(mStream.ToArray());
  38. }
  39. catch
  40. {
  41. return encryptString;
  42. }
  43. }
  44. /// <summary>
  45. /// DES解密字符串
  46. /// </summary>
  47. /// <param name="decryptString">待解密的字符串</param>
  48. /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
  49. /// <returns>解密成功返回解密后的字符串,失败返源串</returns>
  50. public static string DecryptDES(string decryptString, string decryptKey)
  51. {
  52. try
  53. {
  54. byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
  55. byte[] rgbIV = Keys;
  56. byte[] inputByteArray = Convert.FromBase64String(decryptString);
  57. DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
  58. MemoryStream mStream = new MemoryStream();
  59. CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
  60. cStream.Write(inputByteArray, 0, inputByteArray.Length);
  61. cStream.FlushFinalBlock();
  62. return Encoding.UTF8.GetString(mStream.ToArray());
  63. }
  64. catch
  65. {
  66. return decryptString;
  67. }
  68. }
  69. /// <summary>
  70. /// 读取Value值
  71. /// </summary>
  72. /// <param name="key">KEY</param>
  73. /// <returns></returns>
  74. public static string GetConfigString(string key)
  75. {
  76. //
  77. // TODO: 在此处添加构造函数逻辑
  78. //
  79. string ElementsValue = "";// = ConfigurationManager.AppSettings[key];
  80. XmlDocument xDoc = new XmlDocument();
  81. //获取可执行文件的路径和名称
  82. xDoc.Load(System.Windows.Forms.Application.StartupPath + "\\ElementsRowSet.config");
  83. // xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
  84. XmlNode xNode;
  85. XmlElement xElem1;
  86. xNode = xDoc.SelectSingleNode("//appSettings");
  87. xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='Elements']");
  88. if (xElem1 != null)
  89. ElementsValue = xElem1.GetAttribute("value");
  90. return ElementsValue;
  91. }
  92. /// <summary>
  93. /// 对Config文件进行写操作
  94. /// </summary>
  95. /// <param name="AppKey">Key值</param>
  96. /// <param name="AppValue">要写的值</param>
  97. public static void SetValue(string AppKey, string AppValue)
  98. {
  99. XmlDocument xDoc = new XmlDocument();
  100. //获取可执行文件的路径和名称
  101. xDoc.Load(System.Windows.Forms.Application.StartupPath + "\\ElementsRowSet.config");
  102. XmlNode xNode;
  103. XmlElement xElem1;
  104. XmlElement xElem2;
  105. xNode = xDoc.SelectSingleNode("//appSettings");
  106. xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
  107. if (xElem1 != null) xElem1.SetAttribute("value", AppValue);
  108. else
  109. {
  110. xElem2 = xDoc.CreateElement("add");
  111. xElem2.SetAttribute("key", AppKey);
  112. xElem2.SetAttribute("value", AppValue);
  113. xNode.AppendChild(xElem2);
  114. }
  115. xDoc.Save(System.Windows.Forms.Application.StartupPath + "\\ElementsRowSet.config");
  116. }
  117. /// <summary>
  118. /// 从文件获取列名
  119. /// </summary>
  120. public static string GetElementsRow()
  121. {
  122. string strElementsRow = DecryptDES(GetConfigString("Elements"), encryptKey); //服务器名
  123. return strElementsRow;
  124. }
  125. /// <summary>
  126. /// 将设置好的列名保存到文件
  127. /// </summary>
  128. public static void SetElementsRow(string strElementsRow)
  129. {
  130. //修改服务器联接
  131. SetValue("Elements", EncryptDES(strElementsRow, encryptKey));
  132. }
  133. }
  134. }