ASP.NET加密和解密數據庫連接字符串


大家知道,在應用程序中進行數據庫操作需要連接字符串,而如果沒有連接字符串,我們就無法在應用程序中完成檢索數據,創建數據等一系列的數據庫操作。當有人想要獲取你程序中的數據庫信息,他首先看到的可能會是Web.Config文件。而多數情況下我們不希望那些高度敏感的數據信息被別人看到,所以我們就需要對其加密。這篇文章我將演示怎么實現加密和解密數據庫的連接字符串。

目錄

1.創建一個新項目

2.添加一個連接字符串

3.加密連接字符串

4.解密連接字符串

需要的工具

Visual Studio

SQL Server

1.創建一個新項目

File->New->New Project->Name the project->Select Empty->Click

接下來連接數據庫。工具->連接數據庫,你可以選擇連接本地或者服務器數據庫。

2.添加連接字符串

我們需要將連接字符串的屬性添加在web config文件的configuration標簽下:

<connectionStrings>  
   <add name="myConnection" connectionString="Data Source=SIBEESHVENU\SQLEXPRESS;Initial Catalog=ReportServer$SQLEXPRESS;Integrated Security=True" />  
</connectionStrings>  

然后創建一個web頁面,在頁面加載事件中,獲取這個連接字符串並輸出,代碼如下:

 using System;  
   
    namespace EncryptConnectionString  
    {  
        public partial class Default: System.Web.UI.Page  
        {  
            protected void Page_Load(object sender, EventArgs e)  
            {  
                if (!IsPostBack)  
                {  
                    try  
                    {  
                        string myCon = System.Configuration.ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString;  
                        if (myCon != null)  
                        {  
                            Response.Write("My connection string is :" + myCon);  
                        }  
                    }  
                    catch (Exception)  
                    {  
                        throw;  
                    }  
                }  
            }  
        }  
    }   

運行頁面結果:

3.加密連接字符串

在操作之前,以管理員權限打開命令窗口,輸入如下命令:

 cd C:\Windows\Microsoft.NET\Framework\v4.0.30319  

輸入完后,返回項目,復制文件夾地址,我的地址為:F:\Visual Studio\EncryptConnectionString\EncryptConnectionString。回到剛才的命令提示行,輸入:

ASPNET_REGIIS -PEF "connectionStrings" "F:\Visual Studio\EncryptConnectionString\EncryptConnectionString"

點擊Enter,輸出結果如下:

這里要注意connectionStrings大小寫要寫正確。一旦輸錯,就會得到這樣的結果:

  C:\Windows\Microsoft.NET\Framework\v4.0.30319>ASPNET_REGIIS -PEF "connectionstrings" "F:\Visual Studio\EncryptConnectionString\EncryptConnectionString" 

    Microsoft (R) ASP.NET RegIIS version 4.0.30319.0 

    Administration utility to install and uninstall ASP.NET on the local machine. 

    Copyright (C) Microsoft Corporation. All rights reserved. 

    Encrypting configuration section... 

    The configuration section 'connectionstrings' was not found. 

    Failed!  

所以在輸入時一定要仔細。最后打開程序的web.config,我們就會看到連接字符串已經加密了:

    <connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider">  
        <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns="http://www.w3.org/2001/04/xmlenc#">  
            <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />  
            <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">  
                <EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">  
                    <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />  
                    <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">  
                        <KeyName>Rsa Key</KeyName>  
                    </KeyInfo>  
                    <CipherData>  
                        <CipherValue>B4B3oZrbpQsYM7Eaq5smukqDj9XUYUCwygBYRG1iasN4ll5W4wAKVCIFCRfvOJGoIXzgqpyjAI30IKf5pnZ/xWqmo3p/wGfOKdMrzd041dt9llLGbxFpLJs0Nkm583PJ1FppXLAy7FOD0YoBVhG/PBtBgLjTQqcXRNbVcgufzuArlv/EH+7lzSNRclXSTMOPMtISF65hPI9ICj9qLx7RBGhVZ6uFZVFteyyuRd2i3D2r7wJfr6KflFkakdxp1OWE2JK4Ldb8kZSwAy3bNaI/qaV9EgIWt9wM6RZO/IrI3kI/bX8JuvirPw3j/+TLDB3MoIgKjSbLpR3GYTm9csPu8g==</CipherValue>  
                    </CipherData>  
                </EncryptedKey>  
            </KeyInfo>  
            <CipherData>  
                <CipherValue>0n1Y6ScSNZDR4x1sXfK05w9h+pp2OrAEQFQsoAUP5Y/hPsfpJS/7jv21PbPlkYmdCzycM4PGGb0+fuffR3RuL1x0tn7rfyUdA9llTfkyRQKwS9xOmkMsVFXgQDr8P4aXGef1fZPE2gjhcjm/JQToLwsfQZK1gNr4d6cIPFNqKD6wt24F7fuySJPX3OgLb8wXfQMd7ij+JcZzNlnyNHbq/DIjxSpPOnMrC52t06Jj8F8+MsSud9GcijcFB2UhvLVXQwyZ51nEj6Tf36Zbca8bgw==</CipherValue>  
            </CipherData>  
        </EncryptedData>  
    </connectionStrings>   

4.解密連接字符串

解密連接字符串的方法一樣,輸入命令:

    cd C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319  

輸入后,執行下一個命令:

ASPNET_REGIIS -PDF "connectionStrings" "F:\Visual Studio\EncryptConnectionString\EncryptConnectionString"

輸出結果:

這時Web config里的連接字符串也就被解密了。

希望這篇文章對你有幫助!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM