DataProtectionConfigurationProvider 加密 web.config

來源:互聯網
上載者:User

在這裡呢,將要介紹一種加密 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

對 appSettings 加密後

再在對 appSettings 加密的基礎上對 connectionStrings 加密

以上就是對 appSettings 和 connectionStrings

使用 DPAPI 加密後的結果

然後再對 appSettings 和 connectionStrings 解密

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

實質上還可以使用一種方法,也就是 RSA 加密,

使用這種方式加密其實和 DPAPI 加密方式差不多,

您只需要在加密時,把上面的 Provider 參數字串由

DataProtectionConfigurationProvider

改為 RSAProtectedConfigurationProvider 就 OK 了,

感興趣的可以去試試,還有就是推薦一下 MSDN Library 中的那個 Demo,

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

                                                2010—2—04

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.