【问题标题】:Connection String RsaProtectedConfigurationProvider Strategy连接字符串 RsaProtectedConfigurationProvider 策略
【发布时间】:2012-01-24 13:48:32
【问题描述】:

场景: 我有一个 WPF 桌面应用程序,它将为不同的客户分布在不同的机器上。 应用程序有一个 XML 配置文件 'ApplicationConfiguration.xml' 此 XML 文件包含连接字符串。 我需要加密这些连接字符串,因为 ApplicationConfiguration.xml 文件将与主应用程序 exe 一起复制到应用程序的安装文件夹中。

计划策略: 我计划的策略是在安装后加密 'ApplicationConfiguration.xml' 文件。 (如果我能在安装过​​程中做到这一点就更好了)

我尝试过的: 采用在安装后加密 xml 文件的策略,我决定编写一个简单的 winforms 应用程序,以允许用户浏览“ApplicationConfiguration.xml”并只需按一个按钮即可对其进行加密。 当我这样做时,我得到了一个以 xml 配置文件的形式创建的新文件。 'ApplicationConfiguration.xml.Config',但原始 'ApplicationConfiguration.xml' 文件仍然保持原样,连接字符串未受影响... 现在....当我将此文件的内容复制到我的“ApplicationConfiguration.xml”文件中时,程序能够正常运行...xml现在已加密记住。 因此,.NET 4.0 框架似乎可以解密 xml 文件,而无需我在 WPF 应用程序中编写更多代码。

请参阅下面的代码进行加密:

   protected void EncryptConfig(Boolean bEncrypt)
    {
        string path = SelectedFilePath();

        Configuration config = ConfigurationManager.OpenExeConfiguration(path);

        // Define the Rsa provider name. 

        const string provider = "RsaProtectedConfigurationProvider";

        // Get the section to protect. 

        ConfigurationSection connStrings = config.ConnectionStrings;

        if (connStrings != null)
        {
            if (!connStrings.SectionInformation.IsProtected)
            {
                if (!connStrings.ElementInformation.IsLocked)
                {
                    // Protect the section.             
                    connStrings.SectionInformation.ProtectSection(provider);
                    connStrings.SectionInformation.ForceSave = true;
                    config.Save(ConfigurationSaveMode.Full);
                }
            }
        }

        MessageBox.Show("Config has been encrypted");
}

我已经发布了由上面的代码创建的示例输出(用虚拟字符替换 CipherData)

    <?xml version="1.0" encoding="utf-8"?>
 <configuration>    
<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>skfjshsadfhsadkjfsadhfsadkhfdsafhsadkfhkljdfh=</CipherValue>
                </CipherData>
            </EncryptedKey>
        </KeyInfo>
        <CipherData>
            <CipherValue>adfdsafdsafdsfdsafsadfsadfsadfsdfasfdsadfsafsadfdsf=</CipherValue>
        </CipherData>
    </EncryptedData>
</connectionStrings>

所以我对我上面所做的以及我正在尝试做的事情有几个问题:

1) 应用程序能否在不编写新代码的情况下在 WPF 应用程序中读取加密的连接字符串?如果是这样,如果我在自己的机器上进行所有加密处理,每台机器都能够读取加密的连接字符串吗?正如我已经阅读了所需的“密钥”.. 并且不明白上面的 keyName ( Rsa Key ) 来自哪里。

2) 为什么当我在上面的代码示例中保存 xml 文件时,会创建一个新的 'xml.config' 文件?我应该手动将新生成的代码复制到原始 applicationConfiguration.xml 文件中吗?

只是补充一下,当我使用以下代码解密新的 xml.config 文件时:

       connStrings.SectionInformation.UnprotectSection();
                config.Save(ConfigurationSaveMode.Full);

.. 我得到以下输出!为什么! :)

    <?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
    <clear />
    <add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
        providerName="System.Data.SqlClient" />
</connectionStrings>
    </configuration>

我本来希望得到我原来的 3 个连接字符串...不是吗?

基本上,我正在寻找正确的方法来继续加密连接字符串的 xml 文件,并允许在不同的机器上部署和读取应用程序。

任何帮助表示赞赏。

【问题讨论】:

  • 我记得读过这个,但我自己放弃了这个想法。加密配置窗口时,我相信 HKCU 会将生成的密钥存储在注册表中的安全位置。因此,这必须在每台机器和/或用户上完成,并且不能在发货前完成。

标签: wpf security encryption connection-string


【解决方案1】:

• 将 App.config 文件重命名为 web.config • 以管理员身份运行命令提示符:


• 用于加密:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -pef "connectionStrings" your project location within quotes and -prov "DataProtectionConfigurationProvider"

例子:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -pef "connectionStrings" "D:\location\location1\location2\location3\location4" -prov "DataProtectionConfigurationProvider" 

• 解密:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -pdf "connectionStrings" your project location within quotes

例子:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -pdf "connectionStrings" "D:\location\location1\location2\location3\location4" 

• 对于错误:在 Configuration 中添加此内容

(xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0")

• 最后将 web.config 重命名为 App.Config

【讨论】:

    【解决方案2】:

    请参阅.Net Encryption - 数据保护 API 在这里没有帮助,您需要将其发送未加密,以使其在本地加密到机器/用户密钥。

    您最多可以使用任何可用的加密类将其加密为存储在您的应用程序中的密钥,并希望没有人反汇编您的软件。

    【讨论】:

    • 大家好,好吧,如果我像往常一样在每台机器上安装应用程序......然后运行我的加密程序在每台机器上单独进行加密,这样就可以了?你是这个意思吗?谢谢
    • @Kev 实际上我会说这是推荐的方法。它被称为“每次安装”或“安装期间加密”。使用 DPAPI,这是最安全的方式。设置程序的典型工作方式是,它们覆盖 msi 项目中的“安装前或安装后事件”(原谅不正确的名称),并在那里对加密器的调用进行编程。因此,当 msi 文件运行时,事件会被触发,并且在安装过程中配置条目会被加密。有一篇关于 codeproject 的文章您可能会觉得有用。抱歉,我手头没有链接。
    猜你喜欢
    • 2012-03-13
    • 1970-01-01
    • 1970-01-01
    • 2015-02-17
    • 1970-01-01
    • 2021-03-31
    • 2016-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多