[英]How can I use encrypted xml file as application resources for xaml data binding?
我想將加密的xml文件用作應用程序資源,以方便地進行xaml數據綁定。 XML文件在不同的應用程序中被加密。 我可以使用未加密的xml進行數據綁定。 我無法對加密的xml使用相同的方法,因為文件在加載時已加密。 我必須先解密它才能使用。 問題是,解密算法放在哪里?
在這里,我如何創建加密文件(省略了用於解密和驗證解密數據的代碼)
RijndaelManaged algorithm = null;
algorithm = new RijndaelManaged();
string passwordBytes = "password"; //password here
byte[] saltBytes = Encoding.UTF8.GetBytes("salt"); // salt here (another string)
var p = new Rfc2898DeriveBytes(passwordBytes, saltBytes);
algorithm.IV = p.GetBytes(algorithm.BlockSize / 8);
algorithm.Key = p.GetBytes(algorithm.KeySize / 8);
var xmlDoc = new XmlDocument();
xmlDoc.PreserveWhitespace = true;
xmlDoc.Load("Bands.xml");
// Encrypt the "Bands" element.
Encrypt(xmlDoc, "Bands", algorithm);
xmlDoc.Save("encryptedBands.xml");
要解密,我只稱這些(假設xmlDoc和算法與上面相同。
Decrypt(xmlDoc, algorithm);
這里是基於msdn的加密和解密算法(沒什么特別的)。
public static void Encrypt(XmlDocument Doc, string ElementName, SymmetricAlgorithm Key)
{
// Check the arguments.
if (Doc == null)
throw new ArgumentNullException("Doc");
if (ElementName == null)
throw new ArgumentNullException("ElementToEncrypt");
if (Key == null)
throw new ArgumentNullException("Alg");
var elementToEncrypt = Doc.GetElementsByTagName(ElementName)[0] as XmlElement;
// Throw an XmlException if the element was not found.
if (elementToEncrypt == null)
{
throw new XmlException("The specified element was not found");
}
var eXml = new EncryptedXml();
byte[] encryptedElement = eXml.EncryptData(elementToEncrypt, Key, false);
var edElement = new EncryptedData();
edElement.Type = EncryptedXml.XmlEncElementUrl;
string encryptionMethod = null;
if (Key is TripleDES)
{
encryptionMethod = EncryptedXml.XmlEncTripleDESUrl;
}
else if (Key is DES)
{
encryptionMethod = EncryptedXml.XmlEncDESUrl;
}
if (Key is Rijndael)
{
switch (Key.KeySize)
{
case 128:
encryptionMethod = EncryptedXml.XmlEncAES128Url;
break;
case 192:
encryptionMethod = EncryptedXml.XmlEncAES192Url;
break;
case 256:
encryptionMethod = EncryptedXml.XmlEncAES256Url;
break;
}
}
else
{
// Throw an exception if the transform is not in the previous categories
throw new CryptographicException("The specified algorithm is not supported for XML Encryption.");
}
edElement.EncryptionMethod = new EncryptionMethod(encryptionMethod);
// Add the encrypted element data to the
// EncryptedData object.
edElement.CipherData.CipherValue = encryptedElement;
EncryptedXml.ReplaceElement(elementToEncrypt, edElement, false);
}
public static void Decrypt(XmlDocument Doc, SymmetricAlgorithm Alg)
{
// Check the arguments.
if (Doc == null)
throw new ArgumentNullException("Doc");
if (Alg == null)
throw new ArgumentNullException("Alg");
// Find the EncryptedData element in the XmlDocument.
var encryptedElement = Doc.GetElementsByTagName("EncryptedData")[0] as XmlElement;
// If the EncryptedData element was not found, throw an exception.
if (encryptedElement == null)
{
throw new XmlException("The EncryptedData element was not found.");
}
// Create an EncryptedData object and populate it.
var edElement = new EncryptedData();
edElement.LoadXml(encryptedElement);
// Create a new EncryptedXml object.
var exml = new EncryptedXml();
// Decrypt the element using the symmetric key.
byte[] rgbOutput = exml.DecryptData(edElement, Alg);
// Replace the encryptedData element with the plaintext XML element.
exml.ReplaceData(encryptedElement, rgbOutput);
}
我想將加密的xml數據用作數據源,以便在xaml中輕松進行數據綁定。 我在應用程序級別聲明xml數據以進行應用程序范圍的訪問。 這是我在App.xaml中聲明的方式。
<Application.Resources>
<ResourceDictionary>
<XmlDataProvider x:Key="encryptedBandsDataSource" Source="/RemoteConfigurator;component/encryptedBands.xml" d:IsDataSource="True"/>
</ResourceDictionary>
</Application.Resources>
問題是,我需要在甚至加載App.xaml之前解密xml文件。 有可能做到這一點。 我怎么做。 我在哪里解密xml文件?
簡而言之,如何將加密的xml文件用作應用程序資源?
兩種選擇,快速選擇和清潔選擇...
選項1(快速):在加載應用程序之前解密文件
如果解密文件,則在app.xaml.cs中調用base.OnStartup(e)之前,它應該可以工作...
using System.Windows;
namespace MainApplication
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App
{
protected override void OnStartup(StartupEventArgs e)
{
DecryptXml();
base.OnStartup(e);
MainBootstrapper bootstrapper = new MainBootstrapper();
bootstrapper.Run();
}
}
}
選項2(干凈):定制XmlDataProvider處理加密的XML文件
另一個選擇是編寫一個自定義的XmlDataProvider,例如EncryptedXmlDataProvider
,它擁有一個EncryptedSource
屬性和一些其他屬性來指定如何解密文件。 設置EncryptedSource
屬性后, EncryptedXmlDataProvider
可以解密文件。 這樣,數據是可混合的,並且您具有可重用的類型。 與上面提出的解決方案相比,工作量更多,但更干凈。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.