加密和解密连接字符串 [英] Encrypt and decrypting connectionstring

查看:97
本文介绍了加密和解密连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我加密我的app连接字符串,但是当我启动解密连接字符串并连接到我的数据库的应用程序时,连接字符串以加密格式维护



我尝试了什么:



我正在训练解密我在app.config中输入的加密连接字符串,所以在第一次我加密它比替换真正的一个由加密的链接,它看起来

i encrypt my app connectionstring but when i launch the app that decrypt the connection string and connect to my database the connection string maintain in the encrypted format

What I have tried:

i'm training to decrypt my encrypted connection string putted in app.config , so in first i crypt it thani replaced the real one by the crypted chaine and it look

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="Benificiare.Properties.Settings.LogementConnectionString" connectionString="6uDB/32KxMOEPPO0maQDJ63Adjp7okmRdGd9s67mV5+8v6wiRK8UKWscnJSbQzFZTQCLiHQnZPGC8S6lI5Uw28qgjKLL14bkL8sNaDkwRUZ+bB6GMFelH9OVZpG1p+4T/I1LosmrkHylQKHotPFZg3xq3EwDJOY3rjbgE5mu6ow=" providerName="System.Data.SqlClient"/>
    </connectionStrings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/></startup></configuration>





在我的代码中我做反向







in my code i do the inverse


Configuration configuration = null;
            string connection = null;
           configuration = ConfigurationManager.OpenExeConfiguration(@".\Myapp.exe");
            connection = ConfigurationManager.ConnectionStrings["Benificiare.Properties.Settings.LogementConnectionString"].ConnectionString;
            string ENC = DecryptString(connection, "KEY");    //decrypte the connection string                   
            configuration.ConnectionStrings.ConnectionStrings["Benificiare.Properties.Settings.LogementConnectionString"].ConnectionString = ENC;





当我尝试连接的时候我会有消息确认连接字符串没有改变。



when i try to connect i'll have message indecate that the connectionstring is not changed.

推荐答案

这一行:
connection = ConfigurationManager.ConnectionStrings["Myapp.Properties.Settings.LogementConnectionString"].ConnectionString;

寻找名为 Myapp.Properties.Settings.LogementConnectionString 的连接。



但是这一行:

seeks for a connection named Myapp.Properties.Settings.LogementConnectionString.

But this line:

<add name="Benificiare.Properties.Settings.LogementConnectionString" ...

在名称<下声明连接code> Benificiare.Properties.Settings.LogementConnectionString 。



您在XML文件中错误地命名了连接,或者您错误地将其命名为当您尝试从C#代码中获取它时。我很惊讶当您尝试访问不存在的连接字符串时,此代码在运行时不会产生异常。



这里最好的选择是开始使用调试器^ 。调试是开发人员工作的一个非常有趣的部分,我热烈鼓励你试一试,因为你甚至可能觉得它很有趣。



在您希望开始调试的行上放置一个断点^ ,并启动调试会话(参见上面的链接)。



我们不能为您做到这一点,我们也没有所有相关信息。但是,自己学习可以给你带来很大的满足感。

declares the connection under the name Benificiare.Properties.Settings.LogementConnectionString.

Either you misnamed the connection in the XML file, or you misnamed it when you try to fetch it from your C# code. I'm surprised this code does not produce an exception at runtime when you try to access an unexistent connection string.

Your best option here is to start using your debugger^. Debugging is a very funny part of a developer's job, I warmly encourage you to give it a try as you even may find it entertaining.

Put a breakpoint^ on the line you wish to start debugging from, and launch the debug session (see above link for that).

We cannot do that for you, nor do we have all relevant informations. Learning to do it yourself could bring you quite a satisfaction, though.


我调试它并且它正常工作,

我的想法分为两个步骤/>
1-我加密连接字符串,所以我从app.config中获取它,然后用加密()函数加密它然后返回字符串我用app.config连接字符串替换它。

2-在我的应用程序中加载时我必须从app.config中提取连接字符串

connection =
i debug it and it works correctly,
my idea is divided in two steps
1- i encrypt the connectionstring so i take it from app.config and i encrypt it outsid with an Encrypt() function then string returned i replace it with the app.config connectionstring .
2- in my app and when it loading i have to extract the connectionstring from the app.config
connection =
ConfigurationManager.OpenExeConfiguration(@".\Benificiare.exe").ConnectionStrings.ConnectionStrings["Benificiare.Properties.Settings.LogementConnectionString"].ConnectionString;



然后我解密它




then i decrypt it

string ENC = DecryptString(connection, "MyPassPhrase");





然后我要更改我的连接字符串





then ihave to change my connectionstring

ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).ConnectionStrings.CurrentConfiguration.ConnectionStrings.ConnectionStrings["Benificiare.Properties.Settings.LogementConnectionString"].ConnectionString = ENC;





我的错误是运行时间的pb(文件受到保护),所以我使用 OpenExeConfiguration(ConfigurationUserLevel.None)修复它相反

OpenExeConfiguration(@。\ Myapp.exe)


我想你可能会让事情变得复杂一点点。给出这样的 app.config 文件


I think you may be over complicating things a bit. Given an app.config file like this

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <clear />
    <add name="MyConnection"
     providerName="System.Data.ProviderName"
     connectionString="Tie two pieces of string together" />
  </connectionStrings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
</configuration>




以下应用程序显示了一种管理加密和解密的方法。它主要基于此文档。 < br>



The following app shows a way of managing encryption and decryption. It's based largely on this documentation.

using System;
//may need to add reference to System.Configuration
using System.Configuration;
namespace Scratch
{
    class Program
    {
        static void Main(string[] args)
        {
                                        //path to the exe file
            EnsureConnectionIsEncrypted(@".\Scratch.exe");
            //To do:use a text reader to check .\Scratch.exe.config 
            //now has an encrypted connection
            //Note: The App.config file in solution explorer is not changed
            //To retrieve the decrypted string, simply do this:- 
            var connectionString = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
            Console.WriteLine(connectionString);
            Console.ReadLine();
        }
        //The encryption must be enacted on the client machine, 
        //as decryption can only be done by the machine 
        //that performed the encryption
        static void EnsureConnectionIsEncrypted(string exeFileName)
        {
                 Configuration config = ConfigurationManager.
                    OpenExeConfiguration(exeFileName);
                ConnectionStringsSection section =
                    config.GetSection("connectionStrings")
                    as ConnectionStringsSection;
                if (!section.SectionInformation.IsProtected)
                {
                    section.SectionInformation.ProtectSection(
                       "DataProtectionConfigurationProvider");
                    config.Save();
                }
        }
    }
}




加密后, exe。输出目录中的config 文件看起来像这样,实际的 CipherValue 已被截断。




After encryption, the exe.config file in the output directory will look something like this, the actual CipherValue has been truncated.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings configProtectionProvider="DataProtectionConfigurationProvider">
    <EncryptedData>
      <CipherData>
        <CipherValue>AQAAANCMnd8BFdERjHoAwE/Cl........................</CipherValue>
      </CipherData>
    </EncryptedData>
  </connectionStrings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
</configuration>


这篇关于加密和解密连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆