连接字符串RsaProtectedConfigurationProvider策略

时间:2011-12-20 15:52:41

标签: wpf security encryption 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");
}

我已经发布了由上面的代码创建的示例输出(用虚拟字符替换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应用程序中编写新代码?如果是这样,如果我在自己的机器上进行所有加密处理,每台机器是否能够读取加密的连接字符串?正如我所读到的那样,'Key'需要..并且不明白上面的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文件,并允许在不同的机器上部署和读取应用程序。

任何帮助表示感谢。

2 个答案:

答案 0 :(得分:0)

请参阅.Net Encryption - Dataprotection API在此处没有帮助,您需要将其解密,以便将其本地加密到计算机/用户密钥。

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

答案 1 :(得分:0)

•将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" 

•对于错误:将其添加到配置enter image description here

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

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

相关问题