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

时间:2007/7/14 12:50:41      阅读:3819          
首先添加引用: using System.Web.Configuration;

加密操作如下:
Private Sub ProtectSection(ByVal sectionName As String, ByVal provider As String)
Configuration config
=
WebConfigurationManager.
OpenWebConfiguration(Request.ApplicationPath)

Dim section As ConfigurationSection = config.GetSection(sectionName)

If section <> Nothing And Not section.SectionInformation.IsProtected Then
section.SectionInformation.ProtectSection(provider)
config.Save()
End If
End Sub

解密操作如下:
Private Sub UnProtectSection(ByVal sectionName As String)
Configuration config
=
WebConfigurationManager.
OpenWebConfiguration(Request.ApplicationPath)

Dim section As ConfigurationSection = config.GetSection(sectionName)

If section <> Nothing And section.SectionInformation.IsProtected Then
section.SectionInformation.UnprotectSection()
config.Save()
End If
End Sub

实践:
加密前的配置文件:
<?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>
评论
  • Re:ASP.NET2.0中配置文件的加密与解密  (2007/7/14 16:50:49) by ensonfly 
     
    对加解密方法,过程,有详细的介绍.
  • Re:ASP.NET2.0中配置文件的加密与解密  (2007/8/27 19:39:09) by please_air 
    就是解密不方便哦!
  • Re:ASP.NET2.0中配置文件的加密与解密  (2007/9/9 20:47:28) by liufan76 
    ASP.NET 2.0 中有一个新的安全特性.可以对 Web.config 文件中的任何配置节进行加密处理,可以通过手工运行工具aspnet_regiis或者编程来完成这个工作。如果你可以直接访问你的Web 服务器,你可以通过运行如下的命令行:

     cd  %windows%\Microsoft.NET\Framework\versionNumber

    aspnet_regiis -pe "connectionStrings" -app "/SampleApplication" –prov RsaProtectedConfigurationProvider

     
  • Re:ASP.NET2.0中配置文件的加密与解密  (2007/10/24 10:23:49) by guo 
    谢谢分享。
  • Re:ASP.NET2.0中配置文件的加密与解密  (2007/11/1 13:54:35) by 吴毅 
    学习中
  • Re:ASP.NET2.0中配置文件的加密与解密  (2007/11/14 13:34:02) by 量子 
    学习
  • Re:ASP.NET2.0中配置文件的加密与解密  (2008/11/9 20:38:15) by 阿文 
    只要web.config有写权限,加密就不是什么问题。。
  • Re:ASP.NET2.0中配置文件的加密与解密  (2009/4/8 10:54:13) by lclc88 
    改写web.config
    session会丢失的哦
  • Re:ASP.NET2.0中配置文件的加密与解密  (2009/11/26 21:52:06) by y2222 
    改写web.config
    session会丢失的哦
标 题:
 
姓 名:
 
主 页:

验证码:

评论:
 

Because of the cache,you may see your comments several minutes later.