EncryptedType Class

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Represents the abstract base class from which the classes EncryptedData and EncryptedKey derive.

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


[HostProtectionAttribute(SecurityAction.LinkDemand, MayLeakOnAbort = true)]
public abstract class EncryptedType

NameDescription
System_CAPS_protmethodEncryptedType()

Initializes a new instance of the EncryptedType class.

NameDescription
System_CAPS_pubpropertyCipherData

Gets or sets the CipherData value for an instance of an EncryptedType class.

System_CAPS_pubpropertyEncoding

Gets or sets the Encoding attribute of an EncryptedType instance in XML encryption.

System_CAPS_pubpropertyEncryptionMethod

Gets or sets the <EncryptionMethod> element for XML encryption.

System_CAPS_pubpropertyEncryptionProperties

Gets or sets the <EncryptionProperties> element in XML encryption.

System_CAPS_pubpropertyId

Gets or sets the Id attribute of an EncryptedType instance in XML encryption.

System_CAPS_pubpropertyKeyInfo

Gets of sets the <KeyInfo> element in XML encryption.

System_CAPS_pubpropertyMimeType

Gets or sets the MimeType attribute of an EncryptedType instance in XML encryption.

System_CAPS_pubpropertyType

Gets or sets the Type attribute of an EncryptedType instance in XML encryption.

NameDescription
System_CAPS_pubmethodAddProperty(EncryptionProperty)

Adds an <EncryptionProperty> child element to the <EncryptedProperties> element in the current EncryptedType object in XML encryption.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodGetXml()

Returns the XML representation of the EncryptedType object.

System_CAPS_pubmethodLoadXml(XmlElement)

Loads XML information into the <EncryptedType> element in XML encryption.

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

The EncryptedType class represents the abstract base class from which the classes EncryptedData and EncryptedKey derive. These two classes contain the actual encrypted data or key information in XML encryption. To comply with XML encryption standards, you should use these two derived classes.

For more information on XML encryption standards, see http://www.w3.org/TR/xmlenc-core/.

The following code example demonstrates how to encrypt and decrypt an XML element using the EncryptedData class that derives from the EncryptedType class.

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();

        // Load an XML file into the XmlDocument object.
        try
        {
            xmlDoc.PreserveWhitespace = true;
            xmlDoc.Load("test.xml");
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

        // Create a new TripleDES key. 
        TripleDESCryptoServiceProvider tDESkey = new TripleDESCryptoServiceProvider();


        try
        {
            // Encrypt the "creditcard" element.
            Encrypt(xmlDoc, "creditcard", tDESkey);

            // Display the encrypted XML to the console.
            Console.WriteLine("Encrypted XML:");
            Console.WriteLine();
            Console.WriteLine(xmlDoc.OuterXml);

            // Decrypt the "creditcard" element.
            Decrypt(xmlDoc, tDESkey);

            // Display the encrypted XML to the console.
            Console.WriteLine();
            Console.WriteLine("Decrypted XML:");
            Console.WriteLine();
            Console.WriteLine(xmlDoc.OuterXml);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        finally
        {
            // Clear the TripleDES key.
            tDESkey.Clear();
        }

    }

    public static void Encrypt(XmlDocument Doc, string ElementToEncrypt, TripleDESCryptoServiceProvider Alg)
    {

        ////////////////////////////////////////////////
        // Find the specified element in the XmlDocument
        // object and create a new XmlElemnt object.
        ////////////////////////////////////////////////

        XmlElement elementToEncrypt = Doc.GetElementsByTagName(ElementToEncrypt)[0] as XmlElement;

        // Throw an XmlException if the element was not found.
        if (elementToEncrypt == null)
        {
            throw new XmlException("The specified element was not found");

        }

        //////////////////////////////////////////////////
        // Create a new instance of the EncryptedXml class 
        // and use it to encrypt the XmlElement with the 
        // symmetric key.
        //////////////////////////////////////////////////

        EncryptedXml eXml = new EncryptedXml();

        byte[] encryptedElement = eXml.EncryptData(elementToEncrypt, Alg, false);

        ////////////////////////////////////////////////
        // Construct an EncryptedData object and populate
        // it with the desired encryption information.
        ////////////////////////////////////////////////


        EncryptedData edElement = new EncryptedData();

        edElement.Type = EncryptedXml.XmlEncElementUrl;


        // Create an EncryptionMethod element so that the 
        // receiver knows which algorithm to use for decryption.
        // Determine what kind of algorithm is being used and
        // supply the appropriate URL to the EncryptionMethod element.

        edElement.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncTripleDESUrl);

        // Add the encrypted element data to the 
        // EncryptedData object.
        edElement.CipherData.CipherValue = encryptedElement;

        ////////////////////////////////////////////////////
        // Replace the element from the original XmlDocument
        // object with the EncryptedData element.
        ////////////////////////////////////////////////////

        EncryptedXml.ReplaceElement(elementToEncrypt, edElement, false);

    }

    public static void Decrypt(XmlDocument Doc, SymmetricAlgorithm Alg)
    {

        // Find the EncryptedData element in the XmlDocument.
        XmlElement encryptedElement = Doc.GetElementsByTagName("EncryptedData")[0] as XmlElement;

        // If the EncryptedData element was not found, throw an exception.
        if (encryptedElement == null)
        {
            throw new XmlException("The EncryptedData element was not found.");
        }

        // Create an EncryptedData object and populate it.
        EncryptedData edElement = new EncryptedData();
        edElement.LoadXml(encryptedElement);

        // Create a new EncryptedXml object.
        EncryptedXml exml = new EncryptedXml();

        // Decrypt the element using the symmetric key.
        byte[] rgbOutput = exml.DecryptData(edElement, Alg);

        // Replace the encryptedData element with the plaintext XML element.
        exml.ReplaceData(encryptedElement, rgbOutput);

    }


}

.NET Framework
Available since 2.0

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

Return to top
Show: