I'm working on a piece of code to access XML-files and encrypt some of the elements inside.
Mainly using example code from MSDN, and it seems to be working perfectly most of the way.
Here is my encryption method:
public static bool EncryptionMethod(string path) { // Testing Key generation - Rijndael is also known as the EAS-algorithm RijndaelManaged key = null; key = new RijndaelManaged(); // Create XmlDocument and populate with XML from chosen file/source XmlDocument myXML = new XmlDocument(); myXML.PreserveWhitespace = true; try { myXML.Load(path); } catch (System.IO.FileNotFoundException) { string exceptiontype = "The selected file was not found"; string caption = "File not found"; MessageBoxButton button = MessageBoxButton.OK; MessageBoxImage icon = MessageBoxImage.Error; MessageBoxResult result = MessageBox.Show(exceptiontype, caption, button, icon); return false; } // Actual encryption magic XmlElement elementToEncrypt = myXML.GetElementsByTagName("DataElement")[0] as XmlElement; EncryptedXml eXml = new EncryptedXml(); byte[] encryptedElement = eXml.EncryptData(elementToEncrypt, key, false); EncryptedData edElement = new EncryptedData(); // edElement.Type = EncryptedXml.XmlEncElementUrl; // Setting the URI of the 256-bit AES algorithm string algorithmOfEncryption = EncryptedXml.XmlEncAES256Url; edElement.EncryptionMethod = new EncryptionMethod(algorithmOfEncryption); edElement.CipherData.CipherValue = encryptedElement; EncryptedXml.ReplaceElement(elementToEncrypt, edElement, false); myXML.Save(path); return true; } But whenever I try to save the encrypted elements back to the original document with myXML.Save(path) I get a System.IO.IO.Exception with the reason that the file is used by another process.
The problem persists even if I create a new file or choose a different existing file.
What am I doing wrong?
No comments:
Post a Comment