Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

CipherData Class

Note: This class is new in the .NET Framework version 2.0.

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

Namespace: System.Security.Cryptography.Xml
Assembly: System.Security (in system.security.dll)

public sealed class CipherData
public final class CipherData
public final class CipherData

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.

NoteNote

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
	{
		[STAThread]
		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("http://www.w3.org/2001/04/xmlenc#aes128-cbc");
			ed.CipherData = cd;

			//Add key information to the object.
			KeyInfo ki = new KeyInfo();
			ki.AddClause(new KeyInfoRetrievalMethod("#EK", "http://www.w3.org/2001/04/xmlenc#EncryptedKey"));
			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);
			ed.AddProperty(ep);

			// Output the resulting XML information into a file.
			string path = @"c:\test\MyTest.xml";
			File.WriteAllText(path,ed.GetXml().OuterXml);
			//Console.WriteLine(ed.GetXml().OuterXml);

		}
	}
}

System.Object
  System.Security.Cryptography.Xml.CipherData
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 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.