net 2.0中用代码对配置文件进行加密

14 篇文章 0 订阅

 

Dim  config  As  System.Configuration.Configuration  =  ConfigurationManager.OpenExeConfiguration

(ConfigurationUserLevel.None)
Dim  configSection ConfigurationSection
configSection 
=  config.ConnectionStrings
If   Not  (configSection  Is   Nothing Then
If   Not  (configSection.ElementInformation.IsLocked)  Then
configSection.SectionInformation.UnprotectSection()
configSection.SectionInformation.ForceSave 
=   True
config.Save(ConfigurationSaveMode.Full)
End   If
End   If
< connectionStrings  configProtectionProvider ="DataProtectionConfigurationProvider" >
< EncryptedData >

    
< CipherData >
          
< CipherValue > AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAaZ2eussTfEmhwe+kgLsWVwQAAAACAAA
                        AAAADZgAAqAAAABAAAADnyhn4dHzOLGFsIj8QUrXgAAAAAASAAACgAAAAEAAAAKFRR3MAelpxxV6J+KEhfq
                        nQAAAAFJOBaI5ciKhw3Ywra+G0hkZb67k0YTJmXYe5+5cpZ3Wd3H2696mEhAGQiTecOVGixqtF9lHa+Qipm
                       MSHcVECiWYjOh/6CIQL6GED37erb4TLZSNo4U7FrE2JscNCnKaKZUtvnxVqRmjcDWU7Gm2rYRAHoDSEy0UE
                      7 ebbcqr7LQ+Y+C7WrFk+VKf6NmN4js4vl7TJXl/Nr36Z65bvZDCxcle66rZ2yebtXMTP2bX95NasbQx0trv
                       njJrdIdMMrLOqLDPhQLwZ4ObCxkh+Rlg4NxQAAABU+1akHFhrg+4d0AmCGE8Egt3HrA==
</ CipherValue >
        
</ CipherData >
    
</ EncryptedData >
</ connectionStrings >

从.net 2.0 开始 .net提供了对配置文件加密的支持。

对[配置文件加密可以使用户看不到一些敏感的信息即使他们进入了配置文件,这样提高了应用程序的安全性

.net 2.0 允许你对Web.config 和 machine.config 中的大多数sections 进行操作。要通过System.Web.Configuration 命名空间是用来通过代码来对配置文件进行加密,其中包含了两种方法:   ProtectSection 和 UnprotectSection.
               ProtectSection: 加密(需要一个string类型的参数)
               UnprotectSection: 解密(不需要任何参数)
 

 示例(C#):
 以下面这样的配置文件为例,加密前

<? xml version="1.0" ?>
        
< configuration >
           
< appSettings />

           
< connectionStrings >
               
< add  name ="db"  connectionString ="connection details"   />
           
</ connectionStrings >
           
< system .web >
               
< compilation  debug ="false"   />
               
< authentication  mode ="Windows"   />

           
</ system.web >
        
</ configuration >

 

加密代码:

 

protected   void  Page_Load( object  sender, EventArgs e) 
{
        Configuration config;
        ConfigurationSection configSection;
        config 
= WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
        configSection 
= config.GetSection("connectionStrings");
        
if (configSection != null
        
{
                
if (!(configSection.SectionInformation.IsLocked)) 
                
{
                        configSection.SectionInformation.ProtectSection(
"DataProtectionConfigurationProvider");
                        config.Save();
                 }

         }

 }

 


加密后的配置文件如下所示:

 

< connectionStrings  configProtectionProvider ="DataProtectionConfigurationProvider" >
< EncryptedData >

< CipherData >
< CipherValue > AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAaZ2eussTfEmhwe+kgLsWVwQAAAACAAA
AAAADZgAAqAAAABAAAADnyhn4dHzOLGFsIj8QUrXgAAAAAASAAACgAAAAEAAAAKFRR3MAelpxxV6J+KEhfq
nQAAAAFJOBaI5ciKhw3Ywra+G0hkZb67k0YTJmXYe5+5cpZ3Wd3H2696mEhAGQiTecOVGixqtF9lHa+Qipm
MSHcVECiWYjOh/6CIQL6GED37erb4TLZSNo4U7FrE2JscNCnKaKZUtvnxVqRmjcDWU7Gm2rYRAHoDSEy0UE
7ebbcqr7LQ+Y+C7WrFk+VKf6NmN4js4vl7TJXl/Nr36Z65bvZDCxcle66rZ2yebtXMTP2bX95NasbQx0trv
njJrdIdMMrLOqLDPhQLwZ4ObCxkh+Rlg4NxQAAABU+1akHFhrg+4d0AmCGE8Egt3HrA==
</ CipherValue >
</ CipherData >
</ EncryptedData >
</ connectionStrings >


解密代码:

 

protected   void  Page_Load( object  sender, EventArgs e) 
{
        Configuration config;
        ConfigurationSection configSection;
        config 
=  WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
        configSection 
=  config.GetSection( " connectionStrings " );
        
if  (configSection  !=   null
        {
                
if  ( ! (configSection.SectionInformation.IsLocked)) 
                {
                         configSection.SectionInformation.UnprotectSection();config.Save();
                 }
         }
 }

 


加密非web应用程序:(VB.NET)

 

Dim  config  As  System.Configuration.Configuration  =  ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
Dim  configSection ConfigurationSection
configSection 
=  config.ConnectionStrings
If   Not  (configSection  Is   Nothing Then
If   Not  (configSection.ElementInformation.IsLocked)  Then
configSection.SectionInformation.ProtectSection (
" DataProtectionConfigurationProvider " )
configSection.SectionInformation.ForceSave 
=   True
config.Save(ConfigurationSaveMode.Full)
End   If
End   If


解密代码如下:


Dim  config  As  System.Configuration.Configuration  =  ConfigurationManager.OpenExeConfiguration

(ConfigurationUserLevel.None)
Dim  configSection ConfigurationSection
configSection 
=  config.ConnectionStrings
If   Not  (configSection  Is   Nothing Then
If   Not  (configSection.ElementInformation.IsLocked)  Then
configSection.SectionInformation.UnprotectSection()
configSection.SectionInformation.ForceSave 
=   True
config.Save(ConfigurationSaveMode.Full)
End   If
End   If
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值