我想使用加密的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文件作为应用程序资源?
答案 0 :(得分:1)
两个选项,快速选择和干净选项......
选项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
属性和一些其他属性来指定如何解密文件。设置EncryptedXmlDataProvider
属性时,EncryptedSource
可以解密文件。这样,数据是可混合的,并且您具有可重用的类型。比上面提出的解决方案更多的工作,但更清洁。