XmlDsigExcC14NTransform Klas

Definitie

Vertegenwoordigt de exclusieve C14N XML-canonicalisatietransformatie voor een digitale handtekening zoals gedefinieerd door het World Wide Web Consortium (W3C), zonder opmerkingen.

public ref class XmlDsigExcC14NTransform : System::Security::Cryptography::Xml::Transform
public class XmlDsigExcC14NTransform : System.Security.Cryptography.Xml.Transform
type XmlDsigExcC14NTransform = class
    inherit Transform
Public Class XmlDsigExcC14NTransform
Inherits Transform
Overname
XmlDsigExcC14NTransform
Afgeleid

Voorbeelden

In het volgende codevoorbeeld ziet u hoe u een XML-document met de XmlDsigExcC14NTransform klasse ondertekent met behulp van een envelophandtekening.

//
// This example signs an XML file using an
// envelope signature. It then verifies the
// signed XML.
//
using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography.Xml;
using System.Text;
using System.Xml;

public class SignVerifyEnvelope
{

    public static void Main(String[] args)
    {
        try
        {
            // Generate a signing key.
            RSA Key = RSA.Create();

            // Create an XML file to sign.
            CreateSomeXml("Example.xml");
            Console.WriteLine("New XML file created.");

            // Sign the XML that was just created and save it in a
            // new file.
            SignXmlFile("Example.xml", "SignedExample.xml", Key);
            Console.WriteLine("XML file signed.");

            // Verify the signature of the signed XML.
            Console.WriteLine("Verifying signature...");
            bool result = VerifyXmlFile("SignedExample.xml");

            // Display the results of the signature verification to \
            // the console.
            if (result)
            {
                Console.WriteLine("The XML signature is valid.");
            }
            else
            {
                Console.WriteLine("The XML signature is not valid.");
            }
        }
        catch (CryptographicException e)
        {
            Console.WriteLine(e.Message);
        }
    }

    // Sign an XML file and save the signature in a new file.
    public static void SignXmlFile(string FileName, string SignedFileName, RSA Key)
    {
        // Create a new XML document.
        XmlDocument doc = new XmlDocument();

        // Format the document to ignore white spaces.
        doc.PreserveWhitespace = false;

        // Load the passed XML file using it's name.
        doc.Load(new XmlTextReader(FileName));

        // Create a SignedXml object.
        SignedXml signedXml = new SignedXml(doc);

        // Add the key to the SignedXml document.
        signedXml.SigningKey = Key;

        // Specify a canonicalization method.
        signedXml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigExcC14NTransformUrl;

        // Set the InclusiveNamespacesPrefixList property.
        XmlDsigExcC14NTransform canMethod = (XmlDsigExcC14NTransform)signedXml.SignedInfo.CanonicalizationMethodObject;
        canMethod.InclusiveNamespacesPrefixList = "Sign";

        // Create a reference to be signed.
        Reference reference = new Reference();
        reference.Uri = "";

        // Add an enveloped transformation to the reference.
        XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
        reference.AddTransform(env);

        // Add the reference to the SignedXml object.
        signedXml.AddReference(reference);

        // Add an RSAKeyValue KeyInfo (optional; helps recipient find key to validate).
        KeyInfo keyInfo = new KeyInfo();
        keyInfo.AddClause(new RSAKeyValue((RSA)Key));
        signedXml.KeyInfo = keyInfo;

        // Compute the signature.
        signedXml.ComputeSignature();

        // Get the XML representation of the signature and save
        // it to an XmlElement object.
        XmlElement xmlDigitalSignature = signedXml.GetXml();

        // Append the element to the XML document.
        doc.DocumentElement.AppendChild(doc.ImportNode(xmlDigitalSignature, true));

        if (doc.FirstChild is XmlDeclaration)
        {
            doc.RemoveChild(doc.FirstChild);
        }

        // Save the signed XML document to a file specified
        // using the passed string.
        XmlTextWriter xmltw = new XmlTextWriter(SignedFileName, new UTF8Encoding(false));
        doc.WriteTo(xmltw);
        xmltw.Close();
    }
    // Verify the signature of an XML file and return the result.
    public static Boolean VerifyXmlFile(String Name)
    {
        // Create a new XML document.
        XmlDocument xmlDocument = new XmlDocument();

        // Format using white spaces.
        xmlDocument.PreserveWhitespace = true;

        // Load the passed XML file into the document.
        xmlDocument.Load(Name);

        // Create a new SignedXml object and pass it
        // the XML document class.
        SignedXml signedXml = new SignedXml(xmlDocument);

        // Find the "Signature" node and create a new
        // XmlNodeList object.
        XmlNodeList nodeList = xmlDocument.GetElementsByTagName("Signature");

        // Load the signature node.
        signedXml.LoadXml((XmlElement)nodeList[0]);

        // Check the signature and return the result.
        return signedXml.CheckSignature();
    }

    // Create example data to sign.
    public static void CreateSomeXml(string FileName)
    {
        // Create a new XmlDocument object.
        XmlDocument document = new XmlDocument();

        // Create a new XmlNode object.
        XmlNode node = document.CreateNode(XmlNodeType.Element, "", "MyXML", "Don't_Sign");

        // Append the node to the document.
        document.AppendChild(node);

        // Create a new XmlNode object.
        XmlNode subnode = document.CreateNode(XmlNodeType.Element, "", "TempElement", "Sign");

        // Add some text to the node.
        subnode.InnerText = "Here is some data to sign.";

        // Append the node to the document.
        document.DocumentElement.AppendChild(subnode);

        // Save the XML document to the file name specified.
        XmlTextWriter xmltw = new XmlTextWriter(FileName, new UTF8Encoding(false));
        document.WriteTo(xmltw);
        xmltw.Close();
    }
}
'
' This example signs an XML file using an
' envelope signature. It then verifies the 
' signed XML.
'
Imports System.Security.Cryptography
Imports System.Security.Cryptography.X509Certificates
Imports System.Security.Cryptography.Xml
Imports System.Text
Imports System.Xml



Module SignVerifyEnvelope


    Sub Main(ByVal args() As String)
        Try
            ' Generate a signing key.
            Dim Key As RSA = RSA.Create()

            ' Create an XML file to sign.
            CreateSomeXml("Example.xml")
            Console.WriteLine("New XML file created.")

            ' Sign the XML that was just created and save it in a 
            ' new file.
            SignXmlFile("Example.xml", "SignedExample.xml", Key)
            Console.WriteLine("XML file signed.")

            ' Verify the signature of the signed XML.
            Console.WriteLine("Verifying signature...")
            Dim result As Boolean = VerifyXmlFile("SignedExample.xml")

            ' Display the results of the signature verification to 
            ' the console.
            If result Then
                Console.WriteLine("The XML signature is valid.")
            Else
                Console.WriteLine("The XML signature is not valid.")
            End If

        Catch e As CryptographicException
            Console.WriteLine(e.Message)
        End Try

    End Sub


    ' Sign an XML file and save the signature in a new file.
    Sub SignXmlFile(ByVal FileName As String, ByVal SignedFileName As String, ByVal Key As RSA)
        ' Create a new XML document.
        Dim doc As New XmlDocument()

        ' Format the document to ignore white spaces.
        doc.PreserveWhitespace = False

        ' Load the passed XML file using it's name.
        doc.Load(New XmlTextReader(FileName))

        ' Create a SignedXml object.
        Dim signedXml As New SignedXml(doc)

        ' Add the key to the SignedXml document. 
        signedXml.SigningKey = Key

        ' Specify a canonicalization method.
        signedXml.SignedInfo.CanonicalizationMethod = signedXml.XmlDsigExcC14NTransformUrl

        ' Set the InclusiveNamespacesPrefixList property. 
        Dim canMethod As XmlDsigExcC14NTransform = CType(signedXml.SignedInfo.CanonicalizationMethodObject, XmlDsigExcC14NTransform)
        canMethod.InclusiveNamespacesPrefixList = "Sign"

        ' Create a reference to be signed.
        Dim reference As New Reference()
        reference.Uri = ""

        ' Add an enveloped transformation to the reference.
        Dim env As New XmlDsigEnvelopedSignatureTransform()
        reference.AddTransform(env)

        ' Add the reference to the SignedXml object.
        signedXml.AddReference(reference)


        ' Add an RSAKeyValue KeyInfo (optional; helps recipient find key to validate).
        Dim keyInfo As New KeyInfo()
        keyInfo.AddClause(New RSAKeyValue(CType(Key, RSA)))
        signedXml.KeyInfo = keyInfo

        ' Compute the signature.
        signedXml.ComputeSignature()

        ' Get the XML representation of the signature and save
        ' it to an XmlElement object.
        Dim xmlDigitalSignature As XmlElement = signedXml.GetXml()

        ' Append the element to the XML document.
        doc.DocumentElement.AppendChild(doc.ImportNode(xmlDigitalSignature, True))


        If TypeOf doc.FirstChild Is XmlDeclaration Then
            doc.RemoveChild(doc.FirstChild)
        End If

        ' Save the signed XML document to a file specified
        ' using the passed string.
        Dim xmltw As New XmlTextWriter(SignedFileName, New UTF8Encoding(False))
        doc.WriteTo(xmltw)
        xmltw.Close()

    End Sub

    ' Verify the signature of an XML file and return the result.
    Function VerifyXmlFile(ByVal Name As String) As [Boolean]
        ' Create a new XML document.
        Dim xmlDocument As New XmlDocument()

        ' Format using white spaces.
        xmlDocument.PreserveWhitespace = True

        ' Load the passed XML file into the document. 
        xmlDocument.Load(Name)

        ' Create a new SignedXml object and pass it
        ' the XML document class.
        Dim signedXml As New SignedXml(xmlDocument)

        ' Find the "Signature" node and create a new
        ' XmlNodeList object.
        Dim nodeList As XmlNodeList = xmlDocument.GetElementsByTagName("Signature")

        ' Load the signature node.
        signedXml.LoadXml(CType(nodeList(0), XmlElement))

        ' Check the signature and return the result.
        Return signedXml.CheckSignature()

    End Function


    ' Create example data to sign.
    Sub CreateSomeXml(ByVal FileName As String)
        ' Create a new XmlDocument object.
        Dim document As New XmlDocument()

        ' Create a new XmlNode object.
        Dim node As XmlNode = document.CreateNode(XmlNodeType.Element, "", "MyXML", "Don't_Sign")

        ' Append the node to the document.
        document.AppendChild(node)

        ' Create a new XmlNode object.
        Dim subnode As XmlNode = document.CreateNode(XmlNodeType.Element, "", "TempElement", "Sign")

        ' Add some text to the node.
        subnode.InnerText = "Here is some data to sign."

        ' Append the node to the document.
        document.DocumentElement.AppendChild(subnode)

        ' Save the XML document to the file name specified.
        Dim xmltw As New XmlTextWriter(FileName, New UTF8Encoding(False))
        document.WriteTo(xmltw)
        xmltw.Close()

    End Sub
End Module

Opmerkingen

De XmlDsigExcC14NTransform klasse vertegenwoordigt de exclusieve C14N XML-canonicalisatietransformatie zonder opmerkingen. Deze klasse is vergelijkbaar met de XmlDsigC14NTransform klasse, waardoor een ondertekenaar een samenvatting kan maken met behulp van de canonieke vorm van een XML-document. De XmlDsigExcC14NTransform klasse sluit echter bovenliggende context uit van een canonieke subdocument.

Gebruik de XmlDsigC14NTransform klasse wanneer u een XML-subdocument moet canonicaliseren zodat het onafhankelijk is van de XML-context. Toepassingen zoals webservices die gebruikmaken van ondertekende XML binnen complexe communicatieprotocollen, moeten XML bijvoorbeeld vaak op deze manier canonicaliseren. Dergelijke toepassingen bevatten vaak XML binnen verschillende dynamisch samengestelde elementen, waardoor het document aanzienlijk kan worden gewijzigd en dat verificatie van XML-handtekeningen mislukt. De XmlDsigExcC14NTransform klasse lost dit probleem op door dergelijke bovenliggende context uit te sluiten van het canonieke subdocument.

Normaal gesproken maakt u geen nieuw exemplaar van een canonicalisatietransformatieklasse. Als u een canonicalisatietransformatie wilt opgeven, geeft u de URI (Uniform Resource Identifier) door die een transformatie beschrijft naar de CanonicalizationMethod eigenschap, die toegankelijk is vanuit de SignedInfo eigenschap. Als u een verwijzing naar de canonicalisatietransformatie wilt verkrijgen, gebruikt u de CanonicalizationMethodObject eigenschap die toegankelijk is vanuit de SignedInfo eigenschap.

U moet alleen een nieuw exemplaar van een canonicalisatietransformatieklasse maken als u handmatig een XML-document wilt hashen of uw eigen canonicalisatie-algoritme wilt gebruiken.

De URI die de XmlDsigExcC14NWithCommentsTransform klasse beschrijft, wordt gedefinieerd door het XmlDsigExcC14NWithCommentsTransformUrl veld.

De URI die de XmlDsigExcC14NTransform klasse beschrijft, wordt gedefinieerd door het XmlDsigExcC14NTransformUrl veld.

Zie de W3C XMLDSIG-specificatie voor meer informatie over de exclusieve C14N-transformatie. Het canonicalisatie-algoritme wordt gedefinieerd in de W3C Canonical XML-specificatie.

Constructors

Name Description
XmlDsigExcC14NTransform()

Initialiseert een nieuw exemplaar van de XmlDsigExcC14NTransform klasse.

XmlDsigExcC14NTransform(Boolean, String)

Initialiseert een nieuw exemplaar van de XmlDsigExcC14NTransform klasse waarin wordt opgegeven of opmerkingen moeten worden opgenomen en wordt een lijst met voorvoegsels voor de naamruimte opgegeven.

XmlDsigExcC14NTransform(Boolean)

Initialiseert een nieuw exemplaar van de XmlDsigExcC14NTransform klasse waarin een waarde wordt opgegeven die bepaalt of opmerkingen moeten worden opgenomen.

XmlDsigExcC14NTransform(String)

Initialiseert een nieuw exemplaar van de XmlDsigExcC14NTransform klasse waarin een lijst met naamruimtevoorvoegsels wordt opgegeven om canonicaliseren te maken met behulp van het standaardgoritme voor canonicalisatie.

Eigenschappen

Name Description
Algorithm

Haalt de URI (Uniform Resource Identifier) op of stelt deze in die het algoritme identificeert dat wordt uitgevoerd door de huidige transformatie.

(Overgenomen van Transform)
Context

Hiermee wordt een XmlElement object opgehaald of ingesteld dat de documentcontext vertegenwoordigt waaronder het huidige Transform object wordt uitgevoerd.

(Overgenomen van Transform)
InclusiveNamespacesPrefixList

Hiermee haalt u een tekenreeks op die naamruimtevoorvoegsels bevat om canonicaliseren te maken met behulp van het standaard-canonicalisatie-algoritme.

InputTypes

Hiermee haalt u een matrix op van typen die geldige invoer zijn voor de LoadInput(Object) methode van het huidige XmlDsigExcC14NTransform object.

OutputTypes

Hiermee haalt u een matrix op van typen die mogelijke uitvoer van de GetOutput() methoden van het huidige XmlDsigExcC14NTransform object zijn.

PropagatedNamespaces

Hiermee wordt een Hashtable object opgehaald of ingesteld dat de naamruimten bevat die in de handtekening worden doorgegeven.

(Overgenomen van Transform)
Resolver

Hiermee stelt u het huidige XmlResolver object in.

(Overgenomen van Transform)

Methoden

Name Description
Equals(Object)

Bepaalt of het opgegeven object gelijk is aan het huidige object.

(Overgenomen van Object)
GetDigestedOutput(HashAlgorithm)

Retourneert de samenvatting die is gekoppeld aan een XmlDsigExcC14NTransform object.

GetHashCode()

Fungeert als de standaardhashfunctie.

(Overgenomen van Object)
GetInnerXml()

Retourneert een XML-weergave van de parameters van een XmlDsigExcC14NTransform object dat geschikt is om te worden opgenomen als subelementen van een XMLDSIG-element <Transform> .

GetOutput()

Retourneert de uitvoer van het huidige XmlDsigExcC14NTransform object.

GetOutput(Type)

Retourneert de uitvoer van het huidige XmlDsigExcC14NTransform object als een object van het opgegeven type.

GetType()

Hiermee haalt u de Type huidige instantie op.

(Overgenomen van Object)
GetXml()

Retourneert de XML-weergave van het huidige Transform object.

(Overgenomen van Transform)
LoadInnerXml(XmlNodeList)

Parseert het opgegeven XmlNodeList object als transformatiespecifieke inhoud van een <Transform> element en configureert de interne status van het huidige XmlDsigExcC14NTransform object zodat het overeenkomt met het <Transform> element.

LoadInput(Object)

Wanneer deze wordt overschreven in een afgeleide klasse, wordt de opgegeven invoer in het huidige XmlDsigExcC14NTransform object geladen.

MemberwiseClone()

Hiermee maakt u een ondiepe kopie van de huidige Object.

(Overgenomen van Object)
ToString()

Retourneert een tekenreeks die het huidige object vertegenwoordigt.

(Overgenomen van Object)

Van toepassing op