CipherData Class

Definition

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

public ref class CipherData sealed
public sealed class CipherData
type CipherData = class
Public NotInheritable Class CipherData
Inheritance
CipherData

Examples

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.Xml.dll>
#using <System.Security.dll>

using namespace System;
using namespace System::Xml;
using namespace System::Security::Cryptography;
using namespace System::Security::Cryptography::Xml;
using namespace System::IO;

/// This sample used the EncryptedData class to create a EncryptedData element
/// and write it to an XML file.
int main()
{
    // Create a new CipherData object.
    CipherData^ cipher = gcnew CipherData();
    // Assign a byte array to be the CipherValue. This is a
    // byte array representing encrypted data.
    cipher->CipherValue = gcnew array<Byte>(8);
    // Create a new EncryptedData object.
    EncryptedData^ encryptionRoot = gcnew EncryptedData();
    //Add an encryption method to the object.
    encryptionRoot->Id = "ED";
    encryptionRoot->EncryptionMethod = gcnew EncryptionMethod(
        "http://www.w3.org/2001/04/xmlenc#aes128-cbc");
    encryptionRoot->CipherData = cipher;

    //Add key information to the object.
    KeyInfo^ keyDetails = gcnew KeyInfo();
    keyDetails->AddClause(gcnew KeyInfoRetrievalMethod("#EK",
        "http://www.w3.org/2001/04/xmlenc#EncryptedKey"));
    encryptionRoot->KeyInfo = keyDetails;

    // Create new XML document and put encrypted data into it.
    XmlDocument^ doc = gcnew XmlDocument();
    XmlElement^ encryptionPropertyElement =
        doc->CreateElement("EncryptionProperty", 
        EncryptedXml::XmlEncNamespaceUrl);
    EncryptionProperty^ property = gcnew EncryptionProperty(
        encryptionPropertyElement);
    encryptionRoot->AddProperty(property);

    // Output the resulting XML information into a file.
    String^ path = "test.xml";
    try
    {
        File::WriteAllText(path, encryptionRoot->GetXml()->OuterXml);
    }
    catch (IOException^ ex)
    {
        Console::WriteLine("There was an error writing to {0}", path);
        Console::WriteLine(ex->Message);
    }
    //Console.WriteLine(ed.GetXml().OuterXml);
}
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);
        }
    }
}
Imports System.Security.Cryptography.Xml
Imports System.Xml
Imports System.IO


'/ This sample used the EncryptedData class to create a EncryptedData element
'/ and write it to an XML file.
Module EncryptedDataSample1

    Sub Main()
        ' Create a new CipherData object.
        Dim cd As New CipherData
        ' Assign a byte array to the CipherValue.
        cd.CipherValue = New Byte(7) {}
        ' Create a new EncryptedData object.
        Dim ed As 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.
        Dim ki As 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.
        Dim doc As New XmlDocument
        Dim encryptionPropertyElement As XmlElement = CType(doc.CreateElement("EncryptionProperty", EncryptedXml.XmlEncNamespaceUrl), XmlElement)
        Dim ep As New EncryptionProperty(encryptionPropertyElement)
        ed.AddProperty(ep)

        ' Output the resulting XML information into a file.
        Dim path As String = "c:\test\MyTest.xml"
        File.WriteAllText(path, ed.GetXml().OuterXml)
        'End Sub
    End Sub

End Module

Remarks

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.

Note

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.

Constructors

CipherData()

Initializes a new instance of the CipherData class.

CipherData(Byte[])

Initializes a new instance of the CipherData class using a byte array as the CipherValue value.

CipherData(CipherReference)

Initializes a new instance of the CipherData class using a CipherReference object.

Properties

CipherReference

Gets or sets the <CipherReference> element.

CipherValue

Gets or sets the <CipherValue> element.

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
GetXml()

Gets the XML values for the CipherData object.

LoadXml(XmlElement)

Loads XML data from an XmlElement into a CipherData object.

MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to