CipherData Class

Represents the <CipherData> element in XML encryption. This class cannot be inherited.

Namespace: System.Security.Cryptography.Xml
Assembly: System.Security (in

public sealed class CipherData
public final class CipherData
public final class CipherData
Not applicable.

The CipherData class represents the <CipherData> element in XML encryption. It is a required element that provides the encrypted data. It must either contain the encrypted data as base64-encoded text of the <CipherValue> element, or provide a reference to an external location containing the encrypted data using the <CipherReference> element.

In many cases, you do not need to directly create a new instance of the CipherData class. The EncryptedXml, EncryptedData, and EncryptedKey classes create instances for you.


A CipherData object can have either a CipherReference property or a CipherValue property, but not both. A CryptographicException is thrown if both are assigned to a CipherData object.

The following code example uses the EncryptedData class to create an <EncryptedData> element that contains a <CipherData> element, and then writes the XML to an XML file.

using System;
using System.Security.Cryptography.Xml;
using System.Xml;
using System.IO;

/// This sample used the EncryptedData class to create a EncryptedData element
/// and write it to an XML file.
namespace EncryptedDataSample
	class Sample1
		static void Main(string[] args)
			// Create a new CipherData object.
			CipherData cd = new CipherData();
			// Assign a byte array to be the CipherValue. This is a byte array representing encrypted data.
			cd.CipherValue = new byte[8];
			// Create a new EncryptedData object.
			EncryptedData ed = new EncryptedData();
			//Add an encryption method to the object.
			ed.Id = "ED";
			ed.EncryptionMethod = new EncryptionMethod("");
			ed.CipherData = cd;

			//Add key information to the object.
			KeyInfo ki = new KeyInfo();
			ki.AddClause(new KeyInfoRetrievalMethod("#EK", ""));
			ed.KeyInfo = ki;

			// Create new XML document and put encrypted data into it.
			XmlDocument doc = new XmlDocument();
			XmlElement encryptionPropertyElement = (XmlElement)doc.CreateElement("EncryptionProperty", EncryptedXml.XmlEncNamespaceUrl);
			EncryptionProperty ep = new EncryptionProperty(encryptionPropertyElement);

			// Output the resulting XML information into a file.
			string path = @"c:\test\MyTest.xml";



Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions