【发布时间】: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