ASP.NET2.0中配置文件的加密与解密(转载)
http://pw.cnblogs.com/archive/2006/06/25/435357.html
加密操作如下:
 private void ProtectSection(string sectionName, string provider)
private void ProtectSection(string sectionName, string provider)
 {
    {
 Configuration config =
        Configuration config =
 WebConfigurationManager.
            WebConfigurationManager.
 OpenWebConfiguration(Request.ApplicationPath);
                OpenWebConfiguration(Request.ApplicationPath);

 ConfigurationSection section = config.GetSection(sectionName);
        ConfigurationSection section = config.GetSection(sectionName);

 if (section != null && !section.SectionInformation.IsProtected)
        if (section != null && !section.SectionInformation.IsProtected)
 {
        {
 section.SectionInformation.ProtectSection(provider);
            section.SectionInformation.ProtectSection(provider);
 config.Save();
            config.Save();
 }
        }
 }
    }
解密操作如下:
 private void UnProtectSection(string sectionName)
 private void UnProtectSection(string sectionName)
 {
    {
 Configuration config =
        Configuration config =
 WebConfigurationManager.
            WebConfigurationManager.
 OpenWebConfiguration(Request.ApplicationPath);
                OpenWebConfiguration(Request.ApplicationPath);

 ConfigurationSection section = config.GetSection(sectionName);
        ConfigurationSection section = config.GetSection(sectionName);

 if (section != null && section.SectionInformation.IsProtected)
        if (section != null && section.SectionInformation.IsProtected)
 {
        {
 section.SectionInformation.UnprotectSection();
            section.SectionInformation.UnprotectSection();
 config.Save();
            config.Save();
 }
        }
 }
    }
加密前的配置文件:
 <?xml version="1.0"?>
<?xml version="1.0"?>
 <configuration>
<configuration>
 <appSettings>
    <appSettings>
 <add key="name" value="shy520" />
  <add key="name" value="shy520" />
 <add key="address" value="cnblogs" />
  <add key="address" value="cnblogs" />
 </appSettings>
 </appSettings>
 <system.web>
    <system.web>
 <compilation debug="true"/>
        <compilation debug="true"/>
 </system.web>
  </system.web>
 </configuration>
</configuration>
加密后的配置文件:
 <?xml version="1.0"?>
<?xml version="1.0"?>
 <configuration>
<configuration>
 <appSettings configProtectionProvider="DataProtectionConfigurationProvider">
    <appSettings configProtectionProvider="DataProtectionConfigurationProvider">
 <EncryptedData>
  <EncryptedData>
 <CipherData>
   <CipherData>
 <CipherValue>
    <CipherValue>
 AQAAANCMnd8BFdERjHoAwE/Cl+s
      AQAAANCMnd8BFdERjHoAwE/Cl+s
 BAAAABi1ATlNkEUGEf0XyWGL2Xg
      BAAAABi1ATlNkEUGEf0XyWGL2Xg
 QAAAACAAAAAAADZgAAqAAAABAAA
      QAAAACAAAAAAADZgAAqAAAABAAA
 ABIhxMWlazAntwIIpST1CDXAAAA
      ABIhxMWlazAntwIIpST1CDXAAAA
 AASAAACgAAAAEAAAAPz/YKYx07c
      AASAAACgAAAAEAAAAPz/YKYx07c
 b+h4fqdr4fkLgAAAAX1Ieyc+WSx
      b+h4fqdr4fkLgAAAAX1Ieyc+WSx
 AfsDW1vn2C/fXsG2TAnYeUgaCov
      AfsDW1vn2C/fXsG2TAnYeUgaCov
 8e3nGFTHHsH91gLiqKregFPYzGR
      8e3nGFTHHsH91gLiqKregFPYzGR
 vW1xrez/3VwOmJI9eS7EFKrcXej
      vW1xrez/3VwOmJI9eS7EFKrcXej
 NnHL66kg2iNRk3ntLNZlZtTs3cZ
      NnHL66kg2iNRk3ntLNZlZtTs3cZ
 9w63u47VKAjs6miWsGBz2GntL/9
      9w63u47VKAjs6miWsGBz2GntL/9
 UGHLELigrJcr3YJ+lsjOscExQnv
      UGHLELigrJcr3YJ+lsjOscExQnv
 HGvA48EfxpD+tEiFBtgXeHsFkQX
      HGvA48EfxpD+tEiFBtgXeHsFkQX
 cqGySshx16vCt2GUNUC3ZmEAhBh
      cqGySshx16vCt2GUNUC3ZmEAhBh
 UsAFkPIYqelYHd4+m9a/xPe2tqw
      UsAFkPIYqelYHd4+m9a/xPe2tqw
 GIbla1wbW2NDEfrzJPwnkfmpNqR
      GIbla1wbW2NDEfrzJPwnkfmpNqR
 hXijKImipwXbDVYy6o0UAAAAs8D
      hXijKImipwXbDVYy6o0UAAAAs8D
 suYNOhJ7qAjJa2c/4eUC7sks=
      suYNOhJ7qAjJa2c/4eUC7sks=
 </CipherValue>
      </CipherValue>
 </CipherData>
   </CipherData>
 </EncryptedData>
  </EncryptedData>
 </appSettings>
 </appSettings>
 <system.web>
    <system.web>
 <compilation debug="true"/>
        <compilation debug="true"/>
 </system.web>
  </system.web>
 </configuration>
</configuration>
加密操作如下:
 private void ProtectSection(string sectionName, string provider)
private void ProtectSection(string sectionName, string provider) {
    { Configuration config =
        Configuration config = WebConfigurationManager.
            WebConfigurationManager. OpenWebConfiguration(Request.ApplicationPath);
                OpenWebConfiguration(Request.ApplicationPath);
 ConfigurationSection section = config.GetSection(sectionName);
        ConfigurationSection section = config.GetSection(sectionName);
 if (section != null && !section.SectionInformation.IsProtected)
        if (section != null && !section.SectionInformation.IsProtected) {
        { section.SectionInformation.ProtectSection(provider);
            section.SectionInformation.ProtectSection(provider); config.Save();
            config.Save(); }
        } }
    }解密操作如下:
 private void UnProtectSection(string sectionName)
 private void UnProtectSection(string sectionName) {
    { Configuration config =
        Configuration config = WebConfigurationManager.
            WebConfigurationManager. OpenWebConfiguration(Request.ApplicationPath);
                OpenWebConfiguration(Request.ApplicationPath);
 ConfigurationSection section = config.GetSection(sectionName);
        ConfigurationSection section = config.GetSection(sectionName);
 if (section != null && section.SectionInformation.IsProtected)
        if (section != null && section.SectionInformation.IsProtected) {
        { section.SectionInformation.UnprotectSection();
            section.SectionInformation.UnprotectSection(); config.Save();
            config.Save(); }
        } }
    }加密前的配置文件:
 <?xml version="1.0"?>
<?xml version="1.0"?> <configuration>
<configuration> <appSettings>
    <appSettings> <add key="name" value="shy520" />
  <add key="name" value="shy520" /> <add key="address" value="cnblogs" />
  <add key="address" value="cnblogs" /> </appSettings>
 </appSettings> <system.web>
    <system.web> <compilation debug="true"/>
        <compilation debug="true"/> </system.web>
  </system.web> </configuration>
</configuration>加密后的配置文件:
 <?xml version="1.0"?>
<?xml version="1.0"?> <configuration>
<configuration> <appSettings configProtectionProvider="DataProtectionConfigurationProvider">
    <appSettings configProtectionProvider="DataProtectionConfigurationProvider"> <EncryptedData>
  <EncryptedData> <CipherData>
   <CipherData> <CipherValue>
    <CipherValue> AQAAANCMnd8BFdERjHoAwE/Cl+s
      AQAAANCMnd8BFdERjHoAwE/Cl+s BAAAABi1ATlNkEUGEf0XyWGL2Xg
      BAAAABi1ATlNkEUGEf0XyWGL2Xg QAAAACAAAAAAADZgAAqAAAABAAA
      QAAAACAAAAAAADZgAAqAAAABAAA ABIhxMWlazAntwIIpST1CDXAAAA
      ABIhxMWlazAntwIIpST1CDXAAAA AASAAACgAAAAEAAAAPz/YKYx07c
      AASAAACgAAAAEAAAAPz/YKYx07c b+h4fqdr4fkLgAAAAX1Ieyc+WSx
      b+h4fqdr4fkLgAAAAX1Ieyc+WSx AfsDW1vn2C/fXsG2TAnYeUgaCov
      AfsDW1vn2C/fXsG2TAnYeUgaCov 8e3nGFTHHsH91gLiqKregFPYzGR
      8e3nGFTHHsH91gLiqKregFPYzGR vW1xrez/3VwOmJI9eS7EFKrcXej
      vW1xrez/3VwOmJI9eS7EFKrcXej NnHL66kg2iNRk3ntLNZlZtTs3cZ
      NnHL66kg2iNRk3ntLNZlZtTs3cZ 9w63u47VKAjs6miWsGBz2GntL/9
      9w63u47VKAjs6miWsGBz2GntL/9 UGHLELigrJcr3YJ+lsjOscExQnv
      UGHLELigrJcr3YJ+lsjOscExQnv HGvA48EfxpD+tEiFBtgXeHsFkQX
      HGvA48EfxpD+tEiFBtgXeHsFkQX cqGySshx16vCt2GUNUC3ZmEAhBh
      cqGySshx16vCt2GUNUC3ZmEAhBh UsAFkPIYqelYHd4+m9a/xPe2tqw
      UsAFkPIYqelYHd4+m9a/xPe2tqw GIbla1wbW2NDEfrzJPwnkfmpNqR
      GIbla1wbW2NDEfrzJPwnkfmpNqR hXijKImipwXbDVYy6o0UAAAAs8D
      hXijKImipwXbDVYy6o0UAAAAs8D suYNOhJ7qAjJa2c/4eUC7sks=
      suYNOhJ7qAjJa2c/4eUC7sks= </CipherValue>
      </CipherValue> </CipherData>
   </CipherData> </EncryptedData>
  </EncryptedData> </appSettings>
 </appSettings> <system.web>
    <system.web> <compilation debug="true"/>
        <compilation debug="true"/> </system.web>
  </system.web> </configuration>
</configuration> 
                     
                    
                 
                    
                 

 
         
    
 
         
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号