<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<!-- 连接字符串是否加密 -->
<add key="ConStringEncrypt" value="true"/>
<add key="connectionstring" value="User ID=sa;Data Source=.;
Password=;Initial Catalog=test;Provider=SQLOLEDB.1;" />
<add key="TemplatePATH" value="Template" />
</appSettings>
</configuration>以上是我的代码,我明明加了 <add key="ConStringEncrypt" value="true"/>
,但是connectionstring 依旧如故,没有加密,天啊,为什么呢,
<configuration>
<appSettings>
<!-- 连接字符串是否加密 -->
<add key="ConStringEncrypt" value="true"/>
<add key="connectionstring" value="User ID=sa;Data Source=.;
Password=;Initial Catalog=test;Provider=SQLOLEDB.1;" />
<add key="TemplatePATH" value="Template" />
</appSettings>
</configuration>以上是我的代码,我明明加了 <add key="ConStringEncrypt" value="true"/>
,但是connectionstring 依旧如故,没有加密,天啊,为什么呢,
C:\WINDOWS\ Microsoft.net\ Framework\ 版本
aspnet_regiis –pef connectionStrings 项目地址
也可des,md5等加密
public string MD5(String str)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] data = System.Text.Encoding.Default.GetBytes(str);
byte[] result = md5.ComputeHash(data);
string s= BitConverter.ToString(result);
return s;
}
错误 2 找不到类型或命名空间名称“MD5CryptoServiceProvider”(是否缺少 using 指令或程序集引用?) D:\测试\加app配置文件\加app配置文件\Form1.cs 83 27 加app配置文件
.Net2.0中Winform配置文件的加密
对WinForm的App.config文件进行加密最近在做一个WinForm的项目,由于采用的是在客户端直接连接数据库的方式,需要在客户端部署App.config,由于使用了Enterprise Library,需要对App.config文件里的connectionStrings片断进行加密处理,搜索MSDN,发现已经有了现成的工具ASP.NET IIS 注册工具 (Aspnet_regiis.exe),可是它只能针对ASP.NET的Web.config文件,难道我们就没有办法了吗?答案当然是否定的。
配置选项
-pdf section webApplicationDirectory
对指定物理(非虚拟)目录中的 Web.config 文件的指定配置节进行解密。 -pef section webApplicationDirectory
对指定物理(非虚拟)目录中的 Web.config 文件的指定配置节进行加密。
-pdf 和-pef 参数是对指定的物理目录里的Web.config文件进行加密,我们可以先将App.config文件改名为Web.config,通过这两个参数便可以“骗”过系统,让它将指定的配置节进行加密,我们只需要将加密后的文件名改回App.config即可,我们来实验一下:
第一步:先将目录下的App.config改名为Web.config。
第二步:打开SDK命令提示,输入命令:aspnet_regiis -pef "配置节" "目录",
例如:
输入命令:aspnet_regiis -pef "connectionStrings" "E:\开发目录",
由此可见,我们已经完成了任务,现在只需要将Web.config文件名改回App.config即可,在应用程序项目中无需对该文件进行解密操作,.NET框架会自动替我们完成,如果想解密该文件也很简单,在SDK命令提示里输入aspnet_regiis -pdf "配置节" "目录"即可
using System.IO;
using System.Text;
///MD5加密
public string MD5Encrypt(string pToEncrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(),CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach(byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
ret.ToString();
return ret.ToString();
} ///MD5解密
public string MD5Decrypt(string pToDecrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
for(int x = 0; x < pToDecrypt.Length / 2; x++)
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
} des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(),CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder();
return System.Text.Encoding.Default.GetString(ms.ToArray());
}