DataProtectionConfigurationProvider 加密 web.config

在这里呢,将要介绍一种加密 web.config 文件中节的方法,

就是 DPAPI 也就是使用 DataProtectionConfigurationProvider 来实现,

其实呢,还有一种加密的算法,叫做 RSA 加密算法,

不过在实现上这个 RSA 和 DPAPI 差不多,

所以只要注意看一下代码就 OK 了,

DPAPI 是使用的 Windows Data Provider API 来实现加密和解密的,

其中的 Provider 字符串为 DataProtectionConfigurationProvider,

而 RSA 的 Provider 字符串为 RSAProtectedConfigurationProvider,

对于 RSA ,其在 MSDN Library 中有一个非常详细的例子,

不懂得可以去看一下,

这一次呢,

主要是讲一下如何对 web.config 中的 appSettings 和

connectionStrings 实现加密和解密,

其实呢,这两个在加密和解密的实现上根本没有区别,

您只需要在 web.config 中获取这两个节就可以来加解密了,

还是直接看代码和效果比较实在

using System;
using System.Web.Configuration;
using System.Configuration;

namespace WebForm
{
    public partial class Demo__1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        //使用 DPAPI 加密 appSettings
        protected void btnAddApp_Click(object sender, EventArgs e)
        {
           
//Request.ApplicationPath
            //获取服务器上 ASP.NET 应用程序的虚拟应用程序根路径。
            //当前应用程序的虚拟路径。
            //开启 Request.ApplicationPath 应用程序所在的 web.config 文件
           
Configuration config = WebConfigurationManager.
                  OpenWebConfiguration(Request.ApplicationPath);

            //获取 web.config 中的 appSettings 区块
           
ConfigurationSection configSection =
                config.GetSection("appSettings");

           //如果这个区块还没有被加密
            if (!configSection.SectionInformation.IsProtected)
            {
                //进行加密操作
               
configSection.SectionInformation.
                    ProtectSection("DataProtectionConfigurationProvider");

               //将加密的结果保存回 web.config 文件中
                config.Save();
                lblMsg.Text = "AppSettings 使用 DPAPI 加密成功!!!";
            }
            else
            {
                lblMsg.Text = "AppSettings " +
                    "已经被 DPAPI 加密了,此次加密操作被取消!!!";
            }
        }

       //使用 DPAPI 加密 connectionStrings
        protected void btnAddCon_Click(object sender, EventArgs e)
        {
            //Request.ApplicationPath
            //获取服务器上 ASP.NET 应用程序的虚拟应用程序根路径。
            //当前应用程序的虚拟路径。
            //开启 Request.ApplicationPath 应用程序所在的 web.config 文件
        
   Configuration config = WebConfigurationManager.
                  OpenWebConfiguration(Request.ApplicationPath);
            //获取 web.config 中的 appSettings 区块
            ConfigurationSection configSection =
                config.GetSection("connectionStrings");
            //如果这个区块还没有被加密
            if (!configSection.SectionInformation.IsProtected)
            {
               //进行加密操作
                configSection.SectionInformation.
                    ProtectSection("DataProtectionConfigurationProvider");
               
//将加密的结果保存回 web.config 文件中
                config.Save();

                lblMsg.Text = "ConnectionStrings 使用 DPAPI 加密成功!!!";
            }
            else
            {
                lblMsg.Text = "ConnectionStrings " +
                    "已经被 DPAPI 加密了,此次加密操作被取消!!!";
            }
        }

       //进行解密
        protected void btnSub_Click(object sender, EventArgs e)
        {
            Configuration config = WebConfigurationManager.
                OpenWebConfiguration(Request.ApplicationPath);
            ConfigurationSection configAppSection =
                config.GetSection("appSettings"); 
            if (configAppSection.SectionInformation.IsProtected)
            {
               
//在解密时,并不需要区分是 DPAPI 加密的还是 RSA 加密的
                //其均会自行解密

               
configAppSection.SectionInformation.UnprotectSection();
                config.Save();

                lblMsg.Text = "解密成功!!!";
            }
            else
            {
                lblMsg.Text = "该区块暂时还没有被加密,所以无需解密!!!";
            }

            ConfigurationSection configConSection =
                config.GetSection("connectionStrings");

            if (configConSection.SectionInformation.IsProtected)
            {
                configConSection.SectionInformation.UnprotectSection();
                config.Save();
                lblMsg.Text = "解密成功!!!";
            }
            else
            {
                lblMsg.Text = "该区块暂时还没有被加密,所以无需解密!!!";
            }
        }
    }
}

以上就是所有的 Code-Behind 了

看截图吧

加密前的 appSettings 和 connectionStrings

image

对 appSettings 加密后

image

image

再在对 appSettings 加密的基础上对 connectionStrings 加密

image

image

以上就是对 appSettings 和 connectionStrings

使用 DPAPI 加密后的结果

然后再对 appSettings 和 connectionStrings 解密

image

image

以上就是使用 DPAPI 加密的过程了,

实质上还可以使用一种方法,也就是 RSA 加密,

使用这种方式加密其实和 DPAPI 加密方式差不多,

您只需要在加密时,把上面的 Provider 参数字符串由

DataProtectionConfigurationProvider

改为 RSAProtectedConfigurationProvider 就 OK 了,

image

感兴趣的可以去试试,还有就是推荐一下 MSDN Library 中的那个 Demo,

也蛮好的,自己去找找看吧。

                                                2010—2—04

posted @ 2010-03-01 13:27  小宝马的爸爸  阅读(2867)  评论(3编辑  收藏  举报