ASP.NET2.0中配置文件的加密与解密

作者:网络 来源:佚名 更新时间:2008-12-13 02:28:16 点击:


收集最实用的网页特效代码!

首先添加引用: using system.web.configuration;

加密操作如下:

以下为引用的内容:

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();
        }
    }


实践:

加密前的配置文件:

以下为引用的内容:
<?xml version="1.0"?>
<configuration>
    <appsettings>
  <add key="name" value="shy520" />
  <add key="address" value="cnblogs" />
 </appsettings>
    <system.web>
        <compilation debug="true"/>
  </system.web>
</configuration>
加密后的配置文件:
<?xml version="1.0"?>
<configuration>
    <appsettings configprotectionprovider="dataprotectionconfigurationprovider">
  <encrypteddata>
   <cipherdata>
    <ciphervalue>
      aqaaancmnd8bfderjhoawe/cl+s
      baaaabi1atlnkeugef0xywgl2xg
      qaaaacaaaaaaadzgaaqaaaabaaa
      abihxmwlazantwiipst1cdxaaaa
      aasaaacgaaaaeaaaapz/ykyx07c
      b+h4fqdr4fklgaaaax1ieyc+wsx
      afsdw1vn2c/fxsg2tanyeugacov
      8e3ngfthhsh91gliqkregfpyzgr
      vw1xrez/3vwomji9es7efkrcxej
      nnhl66kg2inrk3ntlnzlztts3cz
      9w63u47vkajs6miwsgbz2gntl/9
      ughleligrjcr3yj+lsjoscexqnv
      hgva48efxpd+teifbtgxehsfkqx
      cqgysshx16vct2gunuc3zmeahbh
      usafkpiyqelyhd4+m9a/xpe2tqw
      gibla1wbw2ndefrzjpwnkfmpnqr
      hxijkimipwxbdvyy6o0uaaaas8d
      suynohj7qajja2c/4euc7sks=
      </ciphervalue>
   </cipherdata>
  </encrypteddata>
 </appsettings>
    <system.web>
        <compilation debug="true"/>
  </system.web>
</configuration>