Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I use encrypted xml file as application resources for xaml data binding?
    primarykey
    data
    text
    <p>I want to use encrypted xml file as application resource for easy xaml data binding. Xml file are encrypted in different application. I can use unencrypted xml for data binding. I cant use the same method for encrypted xml because the file is encrypted when it is loaded. I have to decrypt it first before it can be use. Problem is, where do I put decryption algorithm?</p> <p>Here how i create the encrypted files(code for decrypt and verifying decrypted data are omitted)</p> <pre><code> 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"); </code></pre> <p>To decrypt, i just call these(assuming xmlDoc and algorithm are same as above.</p> <pre><code>Decrypt(xmlDoc, algorithm); </code></pre> <p>Here are encrypt and decrypt algorithm based on msdn(nothing special).</p> <pre><code> 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); } </code></pre> <p>I want to use the encrypted xml data as data source for easy data binding in xaml. I declare xml data at application level for application wide access. Here's how I declared it in App.xaml.</p> <pre><code> &lt;Application.Resources&gt; &lt;ResourceDictionary&gt; &lt;XmlDataProvider x:Key="encryptedBandsDataSource" Source="/RemoteConfigurator;component/encryptedBands.xml" d:IsDataSource="True"/&gt; &lt;/ResourceDictionary&gt; &lt;/Application.Resources&gt; </code></pre> <p>Problem is, I need to decrypt the xml file before App.xaml is even loaded. Is it possible to do that. How do I do that. Where do I decrypt the xml file? </p> <p>In short, how can I use encrypted xml file as application resources?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload