连接字符串 RsaProtectedConfigurationProvider 策略

Kev*_*Kev 5 security encryption wpf connection-string

场景: 我有一个 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");
}
Run Code Online (Sandbox Code Playgroud)

我已经发布了由上面的代码创建的示例输出(用虚拟字符替换 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>
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

       connStrings.SectionInformation.UnprotectSection();
                config.Save(ConfigurationSaveMode.Full);
Run Code Online (Sandbox Code Playgroud)

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

    <?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>
Run Code Online (Sandbox Code Playgroud)

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

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

任何帮助表示赞赏。

Ste*_*n B 0

请参阅.Net 加密- 数据保护 API 在这里没有帮助,您需要以未加密的方式发送它,以便将其本地加密到计算机/用户密钥。

最好的情况是,您可以使用任何可用的加密类将其加密到存储在应用程序中的密钥,并希望没有人拆解您的软件。