How to: Encrypt XML Elements with Symmetric Keys
You can use the classes in thenamespace to encrypt an element within an XML document. XML Encryption allows you to store or transport sensitive XML, without worrying about the data being easily read. This procedure decrypts an XML element using the Advanced Encryption Standard (AES) algorithm, also known as Rijndael.
For information about how to decrypt an XML element that was encrypted using this procedure, see.
When you use a symmetric algorithm like AES to encrypt XML data, you must use the same key to encrypt and decrypt the XML data. The example in this procedure assumes that the encrypted XML will be decrypted using the same key, and that the encrypting and decrypting parties agree on the algorithm and key to use. This example does not store or encrypt the AES key within the encrypted XML.
This example is appropriate for situations where a single application needs to encrypt data based on a session key stored in memory, or based on a cryptographically strong key derived from a password. For situations where two or more applications need to share encrypted XML data, consider using an encryption scheme based on an asymmetric algorithm or an X.509 certificate.
To encrypt an XML element with a symmetric key
Generate a symmetric key using theclass. This key will be used to encrypt the XML element.
Create anobject by loading an XML file from disk. The XmlDocument object contains the XML element to encrypt.
Find the specified element in the XmlDocument object and create a newobject to represent the element you want to encrypt. In this example, the "creditcard" element is encrypted.
Create a new instance of theclass and use it to encrypt the XmlElement with the symmetric key. The method returns the encrypted element as an array of encrypted bytes.
Construct anobject and populate it with the URL identifier of the XML Encryption element. This URL identifier lets a decrypting party know that the XML contains an encrypted element. You can use the field to specify the URL identifier.
Create anobject that is initialized to the URL identifier of the cryptographic algorithm used to generate the key. Pass the EncryptionMethod object to the property.
Add the encrypted element data to the EncryptedData object.
Replace the element from the original XmlDocument object with the EncryptedData element.
This example assumes that a file named "test.xml" exists in the same directory as the compiled program. It also assumes that "test.xml" contains a "creditcard" element. You can place the following XML into a file called test.xml and use it with this example.
<root> <creditcard> <number>19834209</number> <expiry>02/02/2002</expiry> </creditcard> </root>
Compiling the Code
To compile this example, you need to include a reference to System.Security.dll.
Include the following namespaces:, , and System.Security.Cryptography.Xml.
Never store a cryptographic key in plaintext or transfer a key between machines in plaintext. Instead, use a secure key container to store cryptographic keys.
When you are done using a cryptographic key, clear it from memory by setting each byte to zero or by calling themethod of the managed cryptography class.