There are three approaches to Xml Encryption.
1. Encrypt the xml using symmetric encryption only
Only one session key is used and it’s the same key that encrypts the xml which is used to decrypt it. The key is not stored with the encrypted xml and so the key needs to be loaded during the process and protected when stored.
2. Encrypt the xml using a combination of asymmetric and symmetric encryption
The dual approach requires a symmetric session key to encrypt the data and an asymmetric key to protect the session key. Both the encrypted session key and the encrypted data are stored together in the xml document. The public asymmetric key is used to encrypt the session key while the private asymmetric key is used to decrypt the key.
This article covers this approach. To learn about the other approaches please look at the MSDN help for more information.
3. Encrypt the xml using a X.509 Certificate This approach uses a X.509 certificate as the symmetrical key. X.509 certificates are provided by a third party vendor such as VeriSign.
Xml encryption, regardless of how the encryption is performed, can store the encrypted data in one of two ways.
- After encryption the whole element is replaced with an element named
- After encryption only the data in the element is replaced and its name remains readable in the document.
The difference is very subtle but it’s rather important. For example:
Your xml document contains a root element called
<employee> that contains a child element called
<WrittenWarning> in which details of disciplinary action is stored. If you were sending this xml and wanted the
<WrittenWarning> elements details protected with approach 1 the
<WrittenWarning> is replaced with an element called
<EncryptedData> and no information can be gathered from the document.
With approach 2 however the
<WrittenWarning> element stays and only the data is encrypted. Anyone who intercepted this document might not know the specific details of the discipline action but they will still know that something has happened with that employee. Any attributes on the
<WrittenWarning> element are also not encrypted.
So the approach you take depends on what the data is and how much information you want to give away. In .NET v2.0 deciding on which approach to take is specified using a Boolean value and can be easily modified.
Example of XML Encryption
Below is an example of XML encryption using the asymmetric approach where the author element in the xml document is replaced with an
The XML Document
XPath expression = /article/articleinfo/author
The encrypted XML Document
The author element and its children have been replaced with the
<EncryptedData> element which contains a number of other elements that are used to describe the encrypted data, i.e. the encryption algorithms used, the session key used, etc.
The <EncryptedData> element
Looking at the tree hierarchy of the
<EncryptedData> element you can see the
<EncryptedData> element is broken down into a number of child elements. The
<KeyInfo> element is the same as the
<KeyInfo> element used in XML Digital Signatures.
EncryptedData element is contained in the "http://www.w3.org/2001/04/xmlenc#” namespace. It is the root of the encrypted data.
EncryptionMethod element is used to specify the symmetric method used when encrypting the data. It does this by using an Algorithm attribute containing a W3 URL that describes the method used. "http://www.w3.org/2001/04/xmlenc#aes256-cbc" indicates the data was encrypted using AES (Rijndael) with a 256k key size.
KeyInfo element is borrowed from XML Digital Signatures and is used to store information about the symmetric keys. The
KeyInfo element can store information about more than one key.
EncryptedKey element and its child elements contain information about one key stored in a
EncryptionMethod element of the
KeyInfo contains the asymmetric encryption method used to encrypt the session key. It does this using an Algorithm attribute set to a W3 URL. For example: http://www.w3.org/2001/04/xmlenc#rsa-1_5 describes that RSA asymmetric encryption was used to encrypt the session key.
KeyName element is an identifier used to find the key. You’ll see the importance of this later when it comes to coding XML Encryption.
CipherValue elements that are found as part of the
EncryptedData elements contain the cipher data. The actual cipher data is stored in the
CipherValue element. The
EncryptedKey element stores the encrypted key, while in the encrypted data is stored in the
CipherValue for the
Asymmetric XML Encryption Process
The process of XML encryption can be summarized in five steps:
- Select an element in an XML document (selecting the root will encrypt the whole document).
- Encrypt the element using a symmetric encryption key, known as the session key.
- Encrypt the session key using asymmetric encryption (the public key is used).
- Create an
EncryptedData element which will contain the encrypted data and the encrypted session key.
- Replace the original element with the
EncryptedData element. Most of the steps are performed automatically for you by .NET v2.0 classes.
Asymmetric XML Decryption Process
The process of decrypting the XML can be summarized into four steps,
- Select the
EncryptedData element in an XML document
- Decrypt the session key using an asymmetric key (the private key is used)
- Decrypt the cipher data using the unencrypted symmetric encryption.
- Replace the
EncryptedData element with the unencrypted element.
Most of the steps are performed automatically for you but .NET v2.0 classes.
The classes needed to perform XML Encryption can be found in three namespaces.
- System.Xml – contains XML classes that are needed to contain XML data.
- System.Security.Cryptography – contains encryption classes used to generate encryption keys.
- System.Security.Cryptography.Xml – contains XML Encryption classes that are used to perform the encryption.
Encrypting XML with .NET
A working sample application accompanies this article which is similar to the code we’ll examine in this section. The sample application can serve as a base for you to add additional functionality, such as selecting a single node or leading the asymmetric key.
First load the asymmetric public key used to encrypt the session key.
EncryptedXml class to encrypt the data and session keys.
Replace the original element with its encrypted equivalent.
Decrypting XML with .NET
First load the private asymmetric key needed to decrypt the session key.
Add a key name mapping to the encrypted document that specifies which private key should be used to decrypt the named session key.
Then decrypt the document, this will decrypt each of the Encrypted Data elements with the specified keys.
XML Encryption is a
W3 Standard to encrypting XML. It does this in such a way that the encrypted data remains and can be treated as XML. It uses both asymmetric and symmetric encryption algorithms, symmetric to encrypt the data and asymmetric to encrypt the symmetric session key. Both the session key and the cipher data are stored together in an XML element called
EncryptedData element contains a series of child elements that describe the algorithms used during the encryption process, as well as containing key information and the cipher data.
W3C Encryption Standard
Download Source Code
XMLEncryption Sample Project
Sorry, no bio is available
This author has published 6 articles on DotNetSlackers. View other articles or the complete profile here.
Please login to rate or to leave a comment.