Implementation of XML encryption and decryption in Asp.net

Source: Internet
Author: User
Tags asymmetric encryption

Introduction

We have three methods to encrypt XML.

1. Only use symmetric encryption to encrypt XML

This encryption method only uses one key, that is, both XML encryption and XML decryption use the same key. Because this key will not be saved in the encrypted XML, We need to load this key during encryption and decryption and protect it from being stolen.

2. Use symmetric encryption and asymmetric encryption to encrypt XML

This method requires a symmetric key for data encryption and an asymmetric key for protecting this symmetric key. The encrypted symmetric key and encrypted data are stored in the XML document. When using a private asymmetric key to decrypt a key, use a public asymmetric key to encrypt the key.

This method will be used in this article. For more information, see msdn.

(Translator's note: asymmetric encryptionAlgorithmTwo keys are required: public key and private key ). A public key is a pair of private keys. If a public key is used to encrypt data, only the corresponding private key can be used for decryption. If a private key is used to encrypt data, only the corresponding public key can be decrypted. Because encryption and decryption use two different keys, this algorithm is called asymmetric encryption algorithm .)

3. Use X.509 to encrypt XMLThis method uses X.509 as an asymmetric key, which is provided by a third party such as Verisign.

Method

No matter how XML encryption is completed, it is always one of two ways to save encrypted data.

1. All elements after encryption are named <encrypteddata>

2. After encryption, only the data is replaced, and the element name is still readable and will not change.

This subtle change is very important. For example:

If your XML document contains a root element called <employee>, this root element contains a sub-element named <writtenwarning> that stores a piece of details. If you send this XML and want to <writtenwarning> This element to be protected, <writtenwarning> will be replaced with <encrypteddata> if you use the method in 1st, you will not obtain any readable information from the encrypted document.

If you use the 2nd methods, the <writtenwarning> element is retained and only data is encrypted. Anyone who obtains this document does not know the details under this element, but still knows that something happened to this employee. In addition, all attributes of the <writtenwarning> element are not encrypted.

Therefore, if there are no special requirements, we generally use 1st methods. In. NET 2.0, you can modify the attribute of a Boolean value to easily select which method to use.

Example of XML Encryption

The following XML encryption example uses the non-symmetric encryption method to encrypt the content under the author element of the XML document and replace the author element with <encrypteddata>.

XML document:

<? XML version = "1.0" standalone = "no"?>
<Article>
<Articleinfo>
<Title> XPath queries on xmldocument objects in. NET 1.1 </title>
<ABSTRACT>
<Para> This article covers the basics. </para>
</Abstract>
<Author>
<Honorific> mr. <Firstname> George </firstname>
<Surname> James </surname>
<Email> gjames@doman.com </Email>
</Author>
</Articleinfo>
</Article>

The XPath expression is/article/articleinfo/author.

Encrypted XML document:

<? XML version = "1.0" standalone = "no"?>
<Article>
<Articleinfo>
<Title> XPath queries on xmldocument objects in. NET 1.1 </title>
<ABSTRACT>
<Para> This article covers the basics. </para>
<Para> This article does not cover. </para>
</Abstract>
<Encrypteddata type = "http://www.w3.org/2001/04/xmlenc#Element"
Xmlns = "http://www.w3.org/2001/04/xmlenc#">
<Encryptionmethod algorithm = "http://www.w3.org/2001/04/xmlenc#aes256-cbc"/>
<Keyinfo xmlns = "http://www.w3.org/2000/09/xmldsig#">
<Encryptedkey xmlns = "http://www.w3.org/2001/04/xmlenc#">
<Encryptionmethod algorithm = "http://www.w3.org/2001/04/xmlenc#rsa-1_5"/>
<Keyinfo xmlns = "http://www.w3.org/2000/09/xmldsig#">
<Keyname> session </keyname>
</Keyinfo>
<Cipherdata>
<Ciphervalue> r4f7si1azksvibb... </Ciphervalue>
</Cipherdata>
</Encryptedkey>
</Keyinfo>
<Cipherdata>
<Ciphervalue> sgnhkqcsovipjdofcfkyeemrfd... </Ciphervalue>
</Cipherdata>
</Encrypteddata>
</Articleinfo>
</Article>

The author element and its child elements will be replaced by <encrypteddata>, and other elements, such as encryption algorithms and keys, will also be included.

<Encrypteddata> element

Take a closer look at the tree structure of the <encrypteddata> element, and you will find that the <encrypteddata> element is divided into many sub-elements. The <keyinfo> element is the same as the <keyinfo> element in the XML digital signature.

The encrypteddata element is included in the "http://www.w3.org/2001/04/xmlenc?#namespace. It is the root element of the encrypted data.

The encryptionmethod element specifies the symmetric method for data encryption. To do this, you need to use an algorithm attribute that contains the W3 URL-"http://www.w3.org/2001/04/xmlenc?aes256-cbc=, which indicates that the data is encrypted using AES (Rijndael) with a 256k key.

The keyinfo element comes from an XML digital signature, which stores information about symmetric keys. In addition, this element can save more information.

The encryptedkey element and its sub-element package under the keyinfo element contain information about the saved key.

The encryptionmethod element in keyinfo contains asymmetric encryption methods used to encrypt symmetric keys. To do this, you need to set an algorithm attribute to the W3 URL. For example.

The keyname element is an identifier used to discover a key. You will find its importance later in programming.

The cipherdata and ciphervalue elements appear under the encryptedkey and encrypteddata elements, which contain password data. In fact, the password data is stored under the ciphervalue element. The encryption key is saved under the encryptedkey element, and the ciphervalue under the encrypteddata element stores the encrypted data.

Asymmetric XML encryption steps

The XML encryption process can be summarized into the following five steps:

1. Select an element in the XML document (the entire document will be encrypted if the root element is selected)

2. Use a symmetric key to encrypt Elements

3. Use asymmetric encryption to encrypt the above symmetric key (using a public key)

4. Create an encrypteddata element that contains the encrypted data and the Encrypted Key.

5. Replace the initial element with the encrypted element.

Most of these steps can be automatically completed using classes in. NET 2.0.

Asymmetric XML decryption steps

The XML decryption process can be summarized into the following four steps:

1. Select an encrypteddata element in the XML document.

2. Use an asymmetric key to decrypt the key (using a private key)

3. Use unencrypted keys to decrypt data

4. Replace the encrypteddata element with the unencrypted element.

Most of these steps can be automatically completed using classes in. NET 2.0.

Namespace

To complete XML encryption, we need to introduce three namespaces.

System. XML-class containing XML operations

System. Security. cryptography-class containing the generated encryption key

System. Security. cryptography. XML-Contains classes for completing encryption tasks

Use. Net to encrypt XML

This document provides a simple XML encryption and decryption application.ProgramNext, let's take a look at the relevantCode. This example only has some basic functions. You can add additional functions such as node selection.

First, load the asymmetric public key to encrypt the key.

// Create an asymmetric key for encryption.
Rsacryptoserviceprovider RSA = new rsacryptoserviceprovider ();
// Load a Public Key
Xmldocument pubkeys = new xmldocument ();
Pubkeys. Load (application. startuppath + "\ XML. Dev. Keys. Public ");
// Use a public key to encrypt the key
RSA. fromxmlstring (pubkeys. outerxml );

Next, load the XML document and select a node to be encrypted. The following code demonstrates how to use an XPATH expression to select a node. If no node is selected, the entire XML file is encrypted.

// XML document
This. xmlencdoc = new xmldocument ();

// Load some nodes and data (Omitted) to the XML document)

Xmlelement encelement;
// If no XPath exists
If (XPath = string. Empty)
{
Encelement = This. xmlencdoc. documentelement;
}
Else
{
Xmlnamespacemanager xmlns = This. xmlcntrlr. xmlnsmanager;
// Select the element to be encrypted through xpath
Encelement = This. xmlencdoc. selectsinglenode (XPath, xmlns) as xmlelement;
}

Use the encryptedxml class to encrypt data and keys

// Encrypt the XML class
Encryptedxml xmlenc = new encryptedxml (this. xmlencdoc );
// Add a "session" key and use RSA Encoding
Xmlenc. addkeynamemapping ("session", RSA );
// Use the "session" key to encrypt data
// The information is stored under the keyinfo element.
Encrypteddata encdata = xmlenc. Encrypt (encelement, "session ");

Replace the initial element with the encrypted Element

// Replace the initial element with the encrypted Element
Encryptedxml. replaceelement (encelement, encdata, false );

Use. Net to decrypt XML

First, load the private asymmetric key to decrypt the key.

// Create an asymmetric key for decryption.
Rsacryptoserviceprovider RSA = new rsacryptoserviceprovider ();
// Load the Private Key
Xmldocument privkeys = new xmldocument ();
Privkeys. Load (application. startuppath + "\ XML. Dev. Keys. Private ");
// Use a private key to decrypt the key
RSA. fromxmlstring (privkeys. outerxml );
Add a key name and map it to the encrypted document.
// Add a key name and map it to the encrypted document.
Encryptedxml encxml = new encryptedxml (xmlencdoc );
Encxml. addkeynamemapping ("session", RSA );
Decrypts each encrypteddata element of a document using the specified key.
// Decrypt all <encrypteddata> Elements
Encxml. decryptdocument ();

Summary

XML encryption is the W3C standard for XML encryption. The encrypted document is still in XML format. We use asymmetric and symmetric algorithms to encrypt XML. symmetric algorithms are used to encrypt data. asymmetric algorithms are used to encrypt keys in symmetric algorithms. encrypted data is stored in the encrypteddata element. The encrypteddata element contains sub-elements used to describe algorithms and key information.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.