一.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;
//对元素内容进行缩进
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)

...{
database = 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=" + 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

...
{
}