我应该如何加密app.config中的连接字符串?

时间:2011-03-05 00:40:52

标签: c# encryption

哪种方式最好加密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中建议的一样。
  3. 关注:
    1)如果我使用密码学,一切正常。除非每次碰到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发布时,是否应该在我的app.conf中保留加密的<CipherValue>rUmEA8h02uMZ5M4uEVtL+5M/UvPuyJ4UJz5d/P...</CipherValue>

    这是因为方法2只能在客户端机器上完成。因此,我应该在客户端计算机上执行方法2,然后将这些加密的代码复制到文件中,每次当我想使用clickOnce发布时,然后在发布之前将其手动复制回App.config,以便客户端更新权限CONNSTRING?

    加密代码:

      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;
        }
    
    你可以建议吗?

1 个答案:

答案 0 :(得分:3)

如果您担心一直调用解密代码,您可以存储它(如果您担心同一页面上有多个调用,则可以存储在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