2011-12-20 48 views
4

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

计划的战略: 我计划的战略是在安装后的“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钥匙)来自哪里。

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文件,并允许在不同的机器上部署和读取应用程序。

任何帮助表示赞赏。

+0

我记得读过这篇文章,但自己却抛弃了这个想法。在加密配置窗口时,将生成的密钥存储在注册表中的安全位置,我相信HKCU。因此,这必须在每台机器和/或用户上完成,并且在发货前不能完成。 – 2011-12-20 17:07:24

回答

0

请参阅.Net Encryption - Dataprotection API在这里没有任何帮助,您需要将其不加密地发送,以便将其本地加密为机器/用户密钥。

充其量你可以使用任何可用的加密类来加密它存储在你的应用程序中的密钥,并希望没有人会拆卸你的软件。

+0

嗨,大家好,所以如果我像平常一样在每台机器上安装应用程序...然后运行我的加密程序来分别在每台机器上执行加密内容,那么这个工作会完成吗?你是这个意思吗?谢谢 – Kev 2011-12-20 20:26:02

+0

@Kev其实我会说这是推荐的方法。它被称为“每次安装”或“安装时加密”。使用DPAPI,这是最安全的方式。典型的安装程序的方式是,他们重写msi项目中的“Before或After Install事件”(原谅不正确的名称),并将调用编程为加密器。所以当msi文件运行时,事件被触发并且配置条目在安装过程中被加密。 有一篇关于codeproject的文章,您可能会觉得有用。对不起,我没有链接方便。 – gmaran23 2012-09-06 12:42:11