How To - Encrypt XML Elements With Asymmetric Keys - Microsoft Docs
How To - Encrypt XML Elements With Asymmetric Keys - Microsoft Docs
You can use XML Encryption to replace any XML element or document with an < EncryptedData >
element that contains the encrypted XML data. The < EncryptedData > element can also contain sub
elements that include information about the keys and processes used during encryption. XML
Encryption allows a document to contain multiple encrypted elements and allows an element to be
encrypted multiple times. The code example in this procedure shows how to create an
< EncryptedData > element along with several other sub elements that you can use later during
decryption.
This example encrypts an XML element using two keys. It generates an RSA public/private key pair and
saves the key pair to a secure key container. The example then creates a separate session key using
the Advanced Encryption Standard (AES) algorithm. The example uses the AES session key to encrypt
the XML document and then uses the RSA public key to encrypt the AES session key. Finally, the
example saves the encrypted AES session key and the encrypted XML data to the XML document
within a new < EncryptedData > element.
To decrypt the XML element, you retrieve the RSA private key from the key container, use it to decrypt
the session key, and then use the session key to decrypt the document. For more information about
how to decrypt an XML element that was encrypted using this procedure, see How to: Decrypt XML
Elements with Asymmetric Keys.
This example is appropriate for situations where multiple applications need to share encrypted data or
where an application needs to save encrypted data between the times that it runs.
C# Copy
https://docs.microsoft.com/en-us/dotnet/standard/security/how-to-encrypt-xml-elements-with-asymmetric-keys 1/10
19/11/2020 How to: Encrypt XML Elements with Asymmetric Keys | Microsoft Docs
C# Copy
C# Copy
C# Copy
https://docs.microsoft.com/en-us/dotnet/standard/security/how-to-encrypt-xml-elements-with-asymmetric-keys 2/10
19/11/2020 How to: Encrypt XML Elements with Asymmetric Keys | Microsoft Docs
5. Create a new session key using the Aes class. This key will encrypt the XML element, and then be
encrypted itself and placed in the XML document.
C# Copy
6. Create a new instance of the EncryptedXml class and use it to encrypt the specified element
using the session key. The EncryptData method returns the encrypted element as an array of
encrypted bytes.
C# Copy
7. Construct an EncryptedData object and populate it with the URL identifier of the encrypted XML
element. This URL identifier lets a decrypting party know that the XML contains an encrypted
element. You can use the XmlEncElementUrl field to specify the URL identifier. The plaintext XML
element will be replaced by an < EncryptedData > element encapsulated by
this EncryptedData object.
C# Copy
C# Copy
9. Create an EncryptedKey object to contain the encrypted session key. Encrypt the session key, add
it to the EncryptedKey object, and enter a session key name and key identifier URL.
C# Copy
https://docs.microsoft.com/en-us/dotnet/standard/security/how-to-encrypt-xml-elements-with-asymmetric-keys 3/10
19/11/2020 How to: Encrypt XML Elements with Asymmetric Keys | Microsoft Docs
10. Create a new DataReference object that maps the encrypted data to a particular session key. This
optional step allows you to easily specify that multiple parts of an XML document were
encrypted by a single key.
C# Copy
C# Copy
edElement.KeyInfo.AddClause(new KeyInfoEncryptedKey(ek));
12. Create a new KeyInfo object to specify the name of the RSA key. Add it to
the EncryptedData object. This helps the decrypting party identify the correct asymmetric key to
use when decrypting the session key.
C# Copy
C# Copy
https://docs.microsoft.com/en-us/dotnet/standard/security/how-to-encrypt-xml-elements-with-asymmetric-keys 4/10
19/11/2020 How to: Encrypt XML Elements with Asymmetric Keys | Microsoft Docs
edElement.CipherData.CipherValue = encryptedElement;
C# Copy
C# Copy
xmlDoc.Save("test.xml");
Example
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.
XML Copy
<root>
<creditcard>
<number>19834209</number>
<expiry>02/02/2002</expiry>
</creditcard>
</root>
C# Copy
using System;
using System.Xml;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
class Program
{
static void Main(string[] args)
{
// Create an XmlDocument object.
XmlDocument xmlDoc = new XmlDocument();
https://docs.microsoft.com/en-us/dotnet/standard/security/how-to-encrypt-xml-elements-with-asymmetric-keys 5/10
19/11/2020 How to: Encrypt XML Elements with Asymmetric Keys | Microsoft Docs
// Create a new RSA key and save it in the container. This key will encrypt
// a symmetric key, which will then be encrypted in the XML document.
RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider(cspParams);
try
{
// Encrypt the "creditcard" element.
Encrypt(xmlDoc, "creditcard", "EncryptedElement1", rsaKey, "rsaKey");
Console.ReadLine();
https://docs.microsoft.com/en-us/dotnet/standard/security/how-to-encrypt-xml-elements-with-asymmetric-keys 6/10
19/11/2020 How to: Encrypt XML Elements with Asymmetric Keys | Microsoft Docs
////////////////////////////////////////////////
// Find the specified element in the XmlDocument
// object and create a new XmlElement object.
////////////////////////////////////////////////
XmlElement elementToEncrypt = Doc.GetElementsByTagName(ElementToEncrypt)[0] as
XmlElement;
try
{
//////////////////////////////////////////////////
// Create a new instance of the EncryptedXml class
// and use it to encrypt the XmlElement with the
// a new random symmetric key.
//////////////////////////////////////////////////
https://docs.microsoft.com/en-us/dotnet/standard/security/how-to-encrypt-xml-elements-with-asymmetric-keys 7/10
19/11/2020 How to: Encrypt XML Elements with Asymmetric Keys | Microsoft Docs
edElement.EncryptionMethod = new
EncryptionMethod(EncryptedXml.XmlEncAES256Url);
// Encrypt the session key and add it to an EncryptedKey element.
EncryptedKey ek = new EncryptedKey();
edElement.KeyInfo.AddClause(new KeyInfoEncryptedKey(ek));
// Set the KeyInfo element to specify the
// name of the RSA key.
https://docs.microsoft.com/en-us/dotnet/standard/security/how-to-encrypt-xml-elements-with-asymmetric-keys 8/10
19/11/2020 How to: Encrypt XML Elements with Asymmetric Keys | Microsoft Docs
.NET Security
Never store a symmetric cryptographic key in plaintext or transfer a symmetric key between machines
in plaintext. Additionally, never store or transfer the private key of an asymmetric key pair in plaintext.
For more information about symmetric and asymmetric cryptographic keys, see Generating Keys for
Encryption and Decryption.
Never embed a key directly into your source code. Embedded keys can be easily read from an
assembly using the Ildasm.exe (IL Disassembler) or by opening the assembly in a text editor such as
Notepad.
When you are done using a cryptographic key, clear it from memory by setting each byte to zero or by
calling the Clear method of the managed cryptography class. Cryptographic keys can sometimes be
read from memory by a debugger or read from a hard drive if the memory location is paged to disk.
See also
Cryptography Model
Cryptographic Services
Cross-Platform Cryptography- System.Security.Cryptography.Xml
How to: Decrypt XML Elements with Asymmetric Keys
ASP.NET Core Data Protection
Yes No
https://docs.microsoft.com/en-us/dotnet/standard/security/how-to-encrypt-xml-elements-with-asymmetric-keys 10/10