EncryptedXml question

Hi,

This is the first time I'm trying to use EncryptedXml class to encrypt my xml file using Rijndael. I was wondering how I can use password and salt to encrypt the file.

Here is the code from MSDN, that doesn't use any password or salt. Please help me.

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");

////////////////////////////////////////////////

// Find the specified element in the XmlDocument

// object and create a new XmlElemnt object.

////////////////////////////////////////////////

XmlElement 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");

}

//////////////////////////////////////////////////

// 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);

}

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.

XmlElement 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.

EncryptedData edElement = new EncryptedData();

edElement.LoadXml(encryptedElement);

// Create a new EncryptedXml object.

EncryptedXml 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);

}

 



Answer this question

EncryptedXml question

  • robmnk

    Greetings;

    I see that no one has answered your question as yet.

    I'm also interested in using the EncryptedXML class.  I have the code from a DevX( ) article for the VS2005 Beta2 release.  It was published in Spring 2005.  And it worked nicely then, but needs some mods for the RTM version.  I can send it to you if you can't find it.

    The title of the article is: Demo: EncryptedXML class in .Net Framework 2.0.  You can probably Google the title and find it.

    The author is: Sanjay Shetty
    sanjay@wirelessstrategist.com
    *****************************************
    The reason I saw your post was that I'm ready to implement EncryptedXML in my own project and am seeking answers to my own question.

    I've got a web app for scholarship applications.  I'm storing the input data into a dataset that I pass around internally in the app (to build signature pages, etc) and then finally append to a file for later ftp download.

    My question is should I encrypt the single record then append or what



  • GaryGary

    Hi Binayak,

    You can use the password and salt to create the key which the XML encryption engine uses. To do this, you would create an Rfc2898DeriveBytes instance, and setup your password and salt on it. Then you would use the GetBytes() method to get a byte array that would be used as your Rijndael key. Finally, set the key on the Rijndael class that you create and pass that into the sample code that you've given.

    -Shawn



  • EncryptedXml question