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里的连接字符串也就被解密了。

希望这篇文章对你有帮助!

posted @ 2016-11-16 17:39  E-iceblue  阅读(2927)  评论(0编辑  收藏  举报