場景: 我有一個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文件,並允許在不同的機器上部署和讀取應用程序。
任何幫助表示讚賞。
我記得讀過這篇文章,但自己卻拋棄了這個想法。在加密配置窗口時,將生成的密鑰存儲在註冊表中的安全位置,我相信HKCU。因此,這必須在每臺機器和/或用戶上完成,並且在發貨前不能完成。 – 2011-12-20 17:07:24