【问题标题】:How should I encrypt the connection string in app.config?我应该如何加密 app.config 中的连接字符串?
【发布时间】:2011-03-05 00:40:51
【问题描述】:

在 app.config 中加密 connstring 的最佳方式是什么?

  1. 使用cryptography进行加解密,或者
  2. 使用%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -pef "connectionStrings" "C:\documents and settings\bob\projects\myproject",就像Protect App.Config file or Encrypt 中推荐的那样。

关注点:
1)如果我使用 Crytography,一切正常。除了每次遇到using (leDataContext db = new leDataContext()) 时都会调用下面这段代码,这让我觉得它会拖慢系统。

public partial class leDataContext
{
    public leDataContext()
        : base("")
       // : base(ConfigurationManager.ConnectionStrings["leConnString"].ToString())
    {           
        string decrypted = Cryptography.Decrypt(ConfigurationManager.ConnectionStrings["leConnString"].ToString());
        base.Connection.ConnectionString = decrypted;
    }
}

2) 如果我使用方法 2,听起来不错,因为它会自动进行加密。但是,当我使用 ClickOnce 发布时,是否应该将这些加密的 <CipherValue>rUmEA8h02uMZ5M4uEVtL+5M/UvPuyJ4UJz5d/P...</CipherValue> 保留在我的 app.conf 中?

这是因为方法2只能在客户端机器上完成。那么我是否应该在客户端机器上执行方法2,然后将那些加密的代码复制到一个文件中,并且每次我想使用clickOnce发布时,然后在发布之前将其手动复制回App.config,以便客户端更新正确连串?

密码学代码:

  internal static string Encrypt(string sender, string key)
    {
        string text1;
        if (sender == null) sender = "";

        byte[] buffer4 = new byte[0];
        byte[] buffer1 = buffer4;
        byte[] buffer2 = new byte[] { 110, 120, 130, 140, 150, 160, 170, 180 };

        try
        {
            buffer1 = Encoding.UTF8.GetBytes(key.Substring(0, 8));
            DESCryptoServiceProvider provider1 = new DESCryptoServiceProvider();
            byte[] buffer3 = Encoding.UTF8.GetBytes(sender);
            MemoryStream stream1 = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream1, provider1.CreateEncryptor(buffer1, buffer2), CryptoStreamMode.Write);
            stream2.Write(buffer3, 0, buffer3.Length);
            stream2.FlushFinalBlock();
            text1 = Convert.ToBase64String(stream1.ToArray());
        }
        catch (Exception ex)
        {
            text1 = string.Empty;
        }
        return text1;
    }

你能建议吗?

【问题讨论】:

  • stackoverflow.com/questions/2874614/…。它可能会回答你的一些问题。另外,你有没有定时看看第一个选项是否真的会导致减速?我认为与服务器建立连接比解密密钥更昂贵。
  • 我已经尝试过第一个选项,我认为系统会变慢,但我只是担心当数据增加时它会变慢......想象一下你每次都必须运行那个加密代码初始化 datacontext()..

标签: c# encryption


【解决方案1】:

如果您担心解密代码一直被调用,您可以存储它(如果您担心同一页面上的多个调用,则针对 HttpContext.Items/Cache,或者如果您担心,则存储静态代码)在所有请求中都担心它)。

如果您要将其放入静态(注意:这意味着解密的值保存在内存中,这可能是一个问题,具体取决于您加密它的确切原因),我建议使用静态构造函数对其进行解密以确​​保代码只运行一次并且不会有任何并发​​问题:

public partial class leDataContext
{
    private static DecryptedConnectionString;
    static leDataContext()
    {
        // This code is guaranteed to run only once, by the framework, before any calls to the instance constructor below.
        DecryptedConnectionString = Cryptography.Decrypt(ConfigurationManager.ConnectionStrings["leConnString"].ToString());
    }

    public leDataContext()
        : base("")
    {           
        base.Connection.ConnectionString = DecryptedConnectionString;
    }
}

还有一些用于加密连接字符串的内置东西可能是更好的选择:

使用受保护的配置加密配置文件部分

ASP.NET 2.0 提供了一个新功能, 称为受保护的配置,即 使您能够加密敏感 配置文件中的信息。 虽然主要设计用于 ASP.NET,受保护的配置可以 也可用于加密配置 Windows 应用程序中的文件部分。 对于新的详细描述 受保护的配置功能, 见Encrypting Configuration Information Using Protected Configuration

【讨论】:

  • 谢谢。将其保存到内存中是个好主意。 :) 你应该得到 + 1。
猜你喜欢
  • 2016-02-10
  • 2012-07-23
  • 1970-01-01
  • 2010-09-06
  • 1970-01-01
  • 1970-01-01
  • 2011-05-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多