一.XML:
<?XML version="1.0" encoding="gb2312"?>
<constr>
<checkstate>1</checkstate>
<server>data source=10.0.0.181;</server>
<database>initial catalog=jiang;</database>
<user>user id=sa;</user>
<pwd>FKeBrrB5/x8=</pwd>
<other>workstation id=10.0.0.181;pooling=true;packet size=4096;persist security info=False;timeout=600</other>
<path>E:Jcs''s work我的文件C#綜合測試(datetime+form)WindowsApplication1inDebugWindowsApplication1.EXE</path>
</constr>
二.源代碼與使用說明
使用說明:checkstate 是用來判斷是否已經加過密的標志.
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Windows.Forms;
using System.XML;
namespace JcsExpLibary.加密公共類
...{
public class Public_Use
...{
public static string server ; //服務器
public static string database ; //數據庫
public static string user ;//用戶名
public static string pwd ; //密碼
"加密解密"#region "加密解密"
public static string SetSecret(string filename)
...{
if (!System.IO.File.Exists(filename))
...{
MessageBox.Show("項目的配置文件被刪除或移動,請聯系開發人員!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return "出錯";
}
string returnvalue;
int index;
//密匙
byte[] b = ...{ 18, 68, 22, 238, 136, 21, 221, 65 };
XmlDocument doc = new XMLDocument();
doc.Load(filename);
//根節點
XMLElement root = doc.DocumentElement;
XMLNode servernode = root.SelectSingleNode("server");
XMLNode databasenode = root.SelectSingleNode("database");
XMLNode usernode = root.SelectSingleNode("user");
XMLNode pwdnode = root.SelectSingleNode("pwd");
XMLNode othernode = root.SelectSingleNode("other");
XMLNode pathnode = root.SelectSingleNode("path");
//檢驗是否已經加過密
XMLNode checklist = root.SelectSingleNode("checkstate");
if (checklist.InnerText == "1")
...{
//表示已經加密
//已經加密就直接解密
pwd = Decrypt(pwdnode.InnerText, b);
index = pwd.IndexOf("=");
if (index != -1)
...{
pwd = pwd.Substring(index + 1, pwd.Length - index - 2);
}
}
else if (checklist.InnerText == "0")
...{
//加密
pwd = pwdnode.InnerText;
index = pwd.IndexOf("=");
if (index != -1)
...{
pwd = pwd.Substring(index + 1, pwd.Length - index - 2);
}
pwdnode.InnerText = Encrypt(pwdnode.InnerText, b);
checklist.InnerText = "1";
//設置為已經加密完成
pathnode.InnerText = Application.ExecutablePath;
StreamWriter sw = new StreamWriter(filename, false, Encoding.GetEncoding("gb2312"));
XmlTextWriter xw = new XMLTextWriter(sw);
//如何對輸出進行格式設置
xw.Formatting = Formatting.Indented;
&nb //對元素內容進行縮進
doc.Save(xw);
sw.Close();
}
//取得server
index = servernode.InnerText.IndexOf("=");
if (index != -1)
...{
server = servernode.InnerText.Substring(index + 1, servernode.InnerText.Length - index - 2);
}
else
...{
server = servernode.InnerText;
}
//取得database
index = databasenode.InnerText.IndexOf("=");
if (index != -1)
...{
&nbsdatabase = databasenode.InnerText.Substring(index + 1, servernode.InnerText.Length - index - 2);
}
else
...{
database = databasenode.InnerText;
}
//取得user
index = usernode.InnerText.IndexOf("=");
if (index != -1)
...{
user = usernode.InnerText.Substring(index + 1, usernode.InnerText.Length - index - 2);
}
else
...{
user = usernode.InnerText;
}
//-----------------------------------------------
returnvalue = servernode.InnerText + databasenode.InnerText + usernode.InnerText + =" + pwd + ";" + othernode.InnerText;
return returnvalue;
}
//加密
public static string Encrypt(string datastr, byte[] key)
...{
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(datastr);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, desc.CreateEncryptor(key, key), CryptoStreamMode.Write);
cs.Write(data, 0, data.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
//解密
public static string Decrypt(string datastr, byte[] key)
...{
string returnstr = null ;
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
byte[] data = Convert.FromBase64String(datastr);
MemoryStream ms = new MemoryStream(data, 0, data.Length);
CryptoStream cs = new CryptoStream(ms, desc.CreateDecryptor(key, key), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs);
returnstr = sr.ReadToEnd();
return returnstr;
}
#endregion
}
}
測試代碼:
_StrConstring = Public_Use.SetSecret(Application.StartupPath + "\aPPSet.XML");
SqlConnection conn = new SqlConnection(_StrConstring);
try
...{
conn.Open();
SqlCommand cmd = new SqlCommand("select id,name,description from a_test",conn);
SqlDataAdapter sqldpr = new SqlDataAdapter(cmd);
DataTable tbl = new DataTable();
sqldpr.Fill(tbl);
this.dataGridView1.DataSource = tbl.DefaultVIEw;
}
catch (Exception ex)
...{
MessageBox.Show(ex.ToString());
}
finally
...{
}