一.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;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
namespace
JcsExpLibary.加密公共类
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
public class Public_Use
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
public static string server ; //服务器
public static string database ; //数据库
public static string user ;//用户名
public static string pwd ; //密码
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
"加密解密"#region "加密解密"
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public static string SetSecret(string filename)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (!System.IO.File.Exists(filename))
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
MessageBox.Show("项目的配置文件被删除或移动,请联系开发人员!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return "出错";
}
string returnvalue;
int index;
//密匙
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
byte[] b = ...{ 18, 68, 22, 238, 136, 21, 221, 65 };
XmlDocument doc = new XmlDocument();
doc.Load(filename);
//根节点
XmlElement root = doc.DocumentElement;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
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");
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//检验是否已经加过密
XmlNode checklist = root.SelectSingleNode("checkstate");
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
if (checklist.InnerText == "1")
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//表示已经加密
//已经加密就直接解密
pwd = Decrypt(pwdnode.InnerText, b);
index = pwd.IndexOf("=");
if (index != -1)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
pwd = pwd.Substring(index + 1, pwd.Length - index - 2);
}
}
else if (checklist.InnerText == "0")
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//加密
pwd = pwdnode.InnerText;
index = pwd.IndexOf("=");
if (index != -1)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
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)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
server = servernode.InnerText.Substring(index + 1, servernode.InnerText.Length - index - 2);
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
server = servernode.InnerText;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//取得database
index = databasenode.InnerText.IndexOf("=");
if (index != -1)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
database = databasenode.InnerText.Substring(index + 1, servernode.InnerText.Length - index - 2);
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
database = databasenode.InnerText;
}
//取得user
index = usernode.InnerText.IndexOf("=");
if (index != -1)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
user = usernode.InnerText.Substring(index + 1, usernode.InnerText.Length - index - 2);
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
user = usernode.InnerText;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//-----------------------------------------------
returnvalue = servernode.InnerText + databasenode.InnerText + usernode.InnerText + "pwd=" + pwd + ";" + othernode.InnerText;
return returnvalue;
}
//加密
public static string Encrypt(string datastr, byte[] key)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
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();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
return Convert.ToBase64String(ms.ToArray());
}
//解密
public static string Decrypt(string datastr, byte[] key)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
string returnstr = null ;
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
byte[] data = Convert.FromBase64String(datastr);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
MemoryStream ms = new MemoryStream(data, 0, data.Length);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
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
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
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)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
MessageBox.Show(ex.ToString());
}
finally
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
}