asp.net 加密连接串(摘录)
命令提示符里
加密:
aspnet_regiis -pef connectionStrings
E:\Project\Test\TestEncrptConnectionString\TestEncrptConnectionString
解密:
aspnet_regiis -pdf connectionStrings E:\Project\Test\TestEncrptConnectionString\TestEncrptConnectionString
下面显示的是通过代码方式对数据库连接字符串加密,代码如下:
Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection configSection = config.GetSection("connectionStrings");
if (configSection.SectionInformation.IsProtected)
...{//如果已经加密,就不用再加密了
configSection.SectionInformation.UnprotectSection();
config.Save();
}
else {
configSection.SectionInformation.ProtectSection ("DataProtectionConfigurationProvider");
config.Save();
}
正向:
<appSettings>
<add key="ConnectionString" value="server=localhost;database=test;pwd=sa;uid=sa;" />
</appSettings>
我们使用简单的base64编码数据
byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes(this.textBox1.Text);
string str = Convert.ToBase64String(data);
<appSettings>
<add key="ConnectionString" value="c2VydmVyPWxvY2FsaG9zdDtkYXRhYmFzZT10ZXN0O3B3ZD1zYTt1aWQ9c2E7" />
</appSettings>
反向:
string strconn = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
byte[] data = Convert.FromBase64String(strconn);
string strRealConn = System.Text.ASCIIEncoding.ASCII.GetString(data);