您的位置:首页 > 其它

如何对ConnectionString进行加密解码?

2014-07-17 14:46 120 查看
这个就不说了就是一个类

public static class EncryptionConfig
{
/*  加密配置节点
*  Response.Write(EncryptionConfig.Encryption("connectionStrings"));
*  解密配置节点
*  Response.Write(EncryptionConfig.Deciphering("connectionStrings"));
*  不影响读取
Response.Write(WebConfigurationManager.AppSettings[0].ToString());
Response.Write(WebConfigurationManager.AppSettings[1].ToString());
*/
/// <summary>
/// 加密配置节点
/// </summary>
/// <param name="strSection"></param>
/// <returns></returns>
public static string Encryption(string strSection)
{
System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(System.Web.HttpContext.Current.Request.ApplicationPath);

//在打开的配置文件中找到指定的节
System.Configuration.ConfigurationSection section = config.GetSection(strSection);

if (section != null && !section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
config.Save();

return "加密成功";
}
else
{
return "加密失败";
}
}

/// <summary>
/// 解密配置节点
/// </summary>
/// <param name="strSection"></param>
/// <returns></returns>
public static string Deciphering(string strSection)
{
System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(System.Web.HttpContext.Current.Request.ApplicationPath);
System.Configuration.ConfigurationSection section = config.GetSection(strSection);
if (section != null && section.SectionInformation.IsProtected)
{
section.SectionInformation.UnprotectSection();
config.Save();
return "解密成功";
}
else
{
return "解密失败";
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: