Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 连接字符串RsaProtectedConfigurationProvider策略_Wpf_Security_Encryption_Connection String - Fatal编程技术网

Wpf 连接字符串RsaProtectedConfigurationProvider策略

Wpf 连接字符串RsaProtectedConfigurationProvider策略,wpf,security,encryption,connection-string,Wpf,Security,Encryption,Connection String,场景: 我有一个WPF桌面应用程序,它将为不同的客户分布在不同的机器上。 应用程序有一个XML配置文件“ApplicationConfiguration.XML” 此XML文件包含连接字符串。 我需要加密这些连接字符串,因为ApplicationConfiguration.xml文件将与主应用程序exe一起复制到应用程序的安装文件夹 计划战略: 我计划的策略是在安装后加密“ApplicationConfiguration.xml”文件。(如果我能在安装期间完成,那就更好了) 我尝试过的: 按照安

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

计划战略: 我计划的策略是在安装后加密“ApplicationConfiguration.xml”文件。(如果我能在安装期间完成,那就更好了)

我尝试过的: 按照安装后加密xml文件的策略,我决定编写一个简单的winforms应用程序,允许用户浏览“ApplicationConfiguration.xml”,只需按下一个按钮即可对其进行加密。 当我这样做时,我得到了一个以xml配置文件的形式创建的新文件。 “ApplicationConfiguration.xml.Config”,但原始的“ApplicationConfiguration.xml”文件仍然保持完整,连接字符串未被触及。。。 现在。。。。当我把这个文件的内容复制到我的“ApplicationConfiguration.xml”文件中时,程序就可以正常运行了。。。xml现在被加密了。 因此,.NET4.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");
}
我已经发布了由上述代码创建的示例输出(用伪字符替换密码数据)

    <?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>
。。我得到以下输出!为什么!:)


我本来希望得到我原来的3个连接字符串。。。没有

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

非常感谢您的帮助。

请参阅-Dataprotection API在这里没有帮助,您需要将其未加密发送,以便将其本地加密到计算机/用户密钥


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

•将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" "D:\location\location1\location2\location3\location4" 

•对于解密:

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 -pef "connectionStrings" "D:\location\location1\location2\location3\location4" -prov "DataProtectionConfigurationProvider" 
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -pdf "connectionStrings" "D:\location\location1\location2\location3\location4" 

•对于错误:将其添加到配置中

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

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

我记得读过这篇文章,但我自己放弃了这个想法。当加密配置时,我相信windows会将生成的密钥存储在注册表中的安全位置。因此,这必须在每台机器和/或用户上完成,并且不能在发货前完成。大家好,如果我在每台机器上正常安装应用程序。。。然后运行我的加密程序,在每台机器上分别进行加密,这样就可以完成工作了吗?这就是你的意思吗?thanks@Kev事实上,我认为这是推荐的方法。它被称为“每次安装”或“安装期间加密”。使用DPAPI,这是最安全的方法。设置程序的典型工作方式是,它们覆盖msi项目中的“安装前或安装后事件”(请原谅不正确的名称),并在那里编程调用加密程序。因此,当msi文件运行时,会触发事件,并且在安装过程中会对配置项进行加密。有一篇关于codeproject的文章,你可能会觉得很有用。对不起,我手头没有链接。