ASP.NET中配置文件的加密与解密在实际开发中应用广泛,下面就具体谈谈对ASP.NET配置文件的加密与解密的用法。
web.config可以保持数据库连接字符串,web.config为XML语法结构,很容易读懂,同时非法用户也可以直接到期数据库连接信息,造成不必要的损失。下面,使用一个简单有效的加密算法来加密一段数据库连接字符,使非法用户不能清楚地得到这些信息。
在通常情况下,保持数据库连接字符串:
<appSettings>
<add key=”ConnectionString” value=”server=(Local);database = test; pwd=sa;uid=sa;”/>
</appSettings
对其进行加密的代码如下:
Configurationconfig= WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section config.GetSection(”appSettings”);
if(section !=null && !section.SectionInformation.IsProtected)
{
Section.SectionInformation.ProtectSection(”RsaProtectedConfigurationProvider”);
Config.Save();
}
对其进行解密的代码如下:
Configuration config=WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section =config.GetSection(“appSettings”);
If(section !=null && secion.SectionInformation.IsProtected)
{
Section SectionInformation.UnprotectSection();
Config.Save();
}