怎么对配置档中的ConnetionString进行加密解密

        在asp.net程序里,无疑web.config配置档相当的重要,里面存放了程序的种种设置,特别是数据库的连接字符串,往往很多人会忽视这个问题。
        
        对web.config里面的关键信息进行加密可以有效的保护程序的安全。
        
        可以通过以下两个途径对配置档进行加密:
        1.  通过程序对配置档进行加密、解密。
       
    private void ProtectSection(string sectionName, string provider)
    
{
        Configuration config 
= WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
        ConfigurationSection section 
= config.GetSection(sectionName);
        
if (section != null && !section.SectionInformation.IsProtected)
        
{
            section.SectionInformation.ProtectSection(provider);
            config.Save();
        }

    }

    
private void UnProtectSection(string sectionName) 
    
{
        Configuration config 
=WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
       ConfigurationSection section 
= config.GetSection(sectionName);
       
if (section != null && section.SectionInformation.IsProtected)
       
{
          section.SectionInformation.UnprotectSection();
          config.Save();
       }

    }

    
protected void Button2_Click(object sender, EventArgs e)
    
{
        ProtectSection(
"connectionStrings""DataProtectionConfigurationProvider");
    }

    
protected void Button3_Click(object sender, EventArgs e)
    
{
        UnProtectSection(
"connectionStrings");
    }

        2.通过Aspnet_regiis配置工具来对web.config进行加密、解密。
        开始-》运行-》cmd—》cd C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
        加密:
        绝对目录用这个
        Aspnet_regiis –pef connectionStrings c:\ddd
        虚拟目录用这个
        Aspnet_regiis –pe connectionStrings  -app  /ddd

        解密同上:
        Aspnet_regiis –pdf connectionStrings c:\ddd

        Aspnet_regiis –pd connectionStrings  -app  /ddd


posted on 2008-04-14 09:23  酒吧  阅读(193)  评论(0)    收藏  举报

导航