Decrypt XML document using Asymmetric key in C#

In the previous article we have discussed, how to encrypt XML element using Symmetric key. This is a continuation to previous article.


  • Add a reference to System.Security.dll to project
  • Include System.Security.Cryptography and System.Security.Cryptography.Xmlnamespace to your project
  • XML decryption  allows you to read 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.

Decrypt XML document using Asymmetric key in C#:
Create a CspParameters object and specify the name of the key container.

Retrieve a previously generated asymmetric key from the container using the RSACryptoServiceProvider object. The key is automatically retrieved from the key container when you pass the CspParameters object to the RSACryptoServiceProvider constructor.

Create a new EncryptedXml object to decrypt the document.

Add a key/name mapping to associate the RSA key with the element within the document that should be decrypted. You must use the same name for the key that you used when you encrypted the document. Note that this name is separate from the name used to identify the key in the key container specified in step 1.

Call the DecryptDocument method to decrypt the element. This method uses the RSA key to decrypt the session key and automatically uses the session key to decrypt the XML element. It also automatically replaces the element with the original plaintext.

Save the XML document.

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 an XML element that was encrypted using the techniques described in How to: Encrypt XML Elements with Asymmetric Keys.

© 2015, www.techkatak.com. All rights reserved.