EncryptedType Klasa

Definicja

Reprezentuje abstrakcyjną klasę bazową, z której klasy EncryptedData i EncryptedKey pochodzą.

C#
public abstract class EncryptedType
Dziedziczenie
EncryptedType
Pochodne

Przykłady

W poniższym przykładzie kodu pokazano, jak szyfrować i odszyfrowywać element XML przy użyciu EncryptedData klasy pochodzącej EncryptedType z klasy.

C#
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);
    }
}

Uwagi

Klasa EncryptedType reprezentuje abstrakcyjną klasę bazową, z której klasy EncryptedData i EncryptedKey pochodzą. Te dwie klasy zawierają rzeczywiste zaszyfrowane dane lub informacje o kluczu w szyfrowaniu XML. Aby zapewnić zgodność ze standardami szyfrowania XML, należy użyć tych dwóch klas pochodnych.

Aby uzyskać więcej informacji na temat standardów szyfrowania XML, zobacz Składnia szyfrowania XML i przetwarzanie w wersji 1.1.

Konstruktory

EncryptedType()

Inicjuje nowe wystąpienie klasy EncryptedType.

Właściwości

CipherData

Pobiera lub ustawia CipherData wartość dla wystąpienia EncryptedType klasy.

Encoding

Pobiera lub ustawia Encoding atrybut EncryptedType wystąpienia w szyfrowaniu XML.

EncryptionMethod

Pobiera lub ustawia <EncryptionMethod> element na potrzeby szyfrowania XML.

EncryptionProperties

Pobiera lub ustawia <EncryptionProperties> element w szyfrowaniu XML.

Id

Pobiera lub ustawia Id atrybut EncryptedType wystąpienia w szyfrowaniu XML.

KeyInfo

Pobiera zestawy elementów <KeyInfo> w szyfrowaniu XML.

MimeType

Pobiera lub ustawia MimeType atrybut EncryptedType wystąpienia w szyfrowaniu XML.

Type

Pobiera lub ustawia Type atrybut EncryptedType wystąpienia w szyfrowaniu XML.

Metody

AddProperty(EncryptionProperty)

Dodaje element podrzędny <EncryptionProperty> do <EncryptedProperties> elementu w bieżącym EncryptedType obiekcie w szyfrowaniu XML.

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetType()

Type Pobiera bieżące wystąpienie.

(Odziedziczone po Object)
GetXml()

Zwraca reprezentację EncryptedType XML obiektu.

LoadXml(XmlElement)

Ładuje informacje XML do <EncryptedType> elementu w szyfrowaniu XML.

MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Dotyczy

Produkt Wersje
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10