Hi again everyone. I have an XML encryption routine based on the MSDN article http://msdn.microsoft.com/en-us/library/sb7w85t6.aspx
My code is here
public static void Encrypt(XmlDocument Doc, string ElementName, SymmetricAlgorithm Key)
{
////////////////////////////////////////////////
// Check the arguments.
////////////////////////////////////////////////
if (Doc == null || (Doc.ToString() == string.Empty))
throw new ArgumentNullException("The XML document was not given or it is empty");
if (ElementName == null || ElementName == string.Empty)
throw new ArgumentNullException("The XML element was not given or it is empty");
if (Key == null || Key.ToString() == string.Empty)
throw new ArgumentNullException("The encryption algorithm was not given or it is empty");
try
{
////////////////////////////////////////////////
// Find the specified element in the XmlDocument
// object and create a new XmlElemnt object.
////////////////////////////////////////////////
foreach (XmlElement elementToEncrypt in Doc.GetElementsByTagName("schema"))
{
//XmlElement elementToEncrypt = Doc.GetElementsByTagName("schema")[0] as XmlElement;
// Throw an XmlException if the element was not found.
if (elementToEncrypt == null || elementToEncrypt.ToString() == string.Empty)
{
throw new XmlException("The specified element was not found or it is empty");
}
//////////////////////////////////////////////////
// Create a new instance of the EncryptedXml class
// and use it to encrypt the XmlElement with the
// symmetric key.
//////////////////////////////////////////////////
EncryptedXml eXml = new EncryptedXml();
byte[] encryptedElement = eXml.EncryptData(elementToEncrypt, Key, false);
////////////////////////////////////////////////
// Construct an EncryptedData object and populate
// it with the desired encryption information.
////////////////////////////////////////////////
EncryptedData edElement = new EncryptedData();
edElement.Type = EncryptedXml.XmlEncElementUrl;
////////////////////////////////////////////////
// Create an EncryptionMethod element so that the
// receiver knows which algorithm to use for decryption.
// Determine what kind of algorithm is being used and
// supply the appropriate URL to the EncryptionMethod element.
////////////////////////////////////////////////
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;
////////////////////////////////////////////////////
// Replace the element from the original XmlDocument
// object with the EncryptedData element.
////////////////////////////////////////////////////
EncryptedXml.ReplaceElement(elementToEncrypt, edElement, false);
}
}
catch (XmlException xexc)
{
throw new Exception(xexc.Message);
}
catch (Exception exc)
{
throw new XmlException("The XML document could not be used to encrypt the elements given: " + exc.Message);
}
finally
{
////////////////////////////////////////////////////
// Replace the XML file with the encrypted version
////////////////////////////////////////////////////
Doc.Save("testMIPS.config");
}
}
This works for the first "schema" element it finds and I can see the Doc.XML string showing the first element having been encrypted.
But when the loop traverses again to supposedly encrypt the next "schema" element, the program throws this exception...
"The element list has changed. The enumeration operation failed to continue."
But I have not actually saved the Doc.XML file as yet.
Why is this happening?
Thank you in advance for your time.