Lire en anglais

Partager via


KeyInfoX509Data Classe

Définition

Représente un sous-élément <X509Data> d'un élément <KeyInfo> de chiffrement XMLDSIG ou XML.

C#
public class KeyInfoX509Data : System.Security.Cryptography.Xml.KeyInfoClause
Héritage
KeyInfoX509Data

Exemples

Cette section contient deux exemples de code. Le premier exemple montre comment signer un fichier XML à l’aide d’une signature détachée. Le deuxième exemple montre comment signer un fichier XML à l’aide d’une signature d’enveloppe.

Exemple n° 1

C#
//
// This example signs a file specified by a URI 
// using a detached signature. It then verifies  
// the signed XML.
//

using System;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Xml;

class XMLDSIGDetached
{
    
    [STAThread]
    static void Main(string[] args)
    {
        // The URI to sign.
        string resourceToSign = "http://www.microsoft.com";
        
        // The name of the file to which to save the XML signature.
        string XmlFileName = "xmldsig.xml";

        // The name of the X509 certificate
        string Certificate = "microsoft.cer";

        try
        {

            // Generate a signing key. This key should match the certificate.
            RSA Key = RSA.Create();

            Console.WriteLine("Signing: {0}", resourceToSign);

            // Sign the detached resourceand save the signature in an XML file.
            SignDetachedResource(resourceToSign, XmlFileName, Key, Certificate);

            Console.WriteLine("XML signature was successfully computed and saved to {0}.", XmlFileName);
        }
        catch(CryptographicException e)
        {
            Console.WriteLine(e.Message);
        }
    }

    // Sign an XML file and save the signature in a new file.
    public static void SignDetachedResource(string URIString, string XmlSigFileName, RSA Key, string Certificate)
    {
        // Create a SignedXml object.
        SignedXml signedXml = new SignedXml();

        // Assign the key to the SignedXml object.
        signedXml.SigningKey = Key;

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

        // Add the passed URI to the reference object.
        reference.Uri = URIString;
        
        // Add the reference to the SignedXml object.
        signedXml.AddReference(reference);

        // Create a new KeyInfo object.
        KeyInfo keyInfo = new KeyInfo();

        // Load the X509 certificate.
        X509Certificate MSCert = X509Certificate.CreateFromCertFile(Certificate);
 
        // Load the certificate into a KeyInfoX509Data object
        // and add it to the KeyInfo object.
        keyInfo.AddClause(new KeyInfoX509Data(MSCert));
  
        // Add the KeyInfo object to the SignedXml object.
        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();

        // Save the signed XML document to a file specified
        // using the passed string.
        XmlTextWriter xmltw = new XmlTextWriter(XmlSigFileName, new UTF8Encoding(false));
        xmlDigitalSignature.WriteTo(xmltw);
        xmltw.Close();
    }
}

Exemple n° 2

C#
//
// 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.Xml;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Xml;

public class SignVerifyEnvelope
{

    public static void Main(String[] args)
    {

        string Certificate =  "microsoft.cer";

        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, Certificate);
            Console.WriteLine("XML file signed."); 
        }
        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, string Certificate)
    {
        // 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;

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

        // Create a new KeyInfo object.
        KeyInfo keyInfo = new KeyInfo();

        // Load the X509 certificate.
        X509Certificate MSCert = X509Certificate.CreateFromCertFile(Certificate);
 
        // Load the certificate into a KeyInfoX509Data object
        // and add it to the KeyInfo object.
        keyInfo.AddClause(new KeyInfoX509Data(MSCert));
  
        // Add the KeyInfo object to the SignedXml object.
        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();
    }

    // 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, "", "MyElement", "samples");
        
        // Add some text to the node.
        node.InnerText = "Example text to be signed.";

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

        // Save the XML document to the file name specified.
        XmlTextWriter xmltw = new XmlTextWriter(FileName, new UTF8Encoding(false));
        document.WriteTo(xmltw);
        xmltw.Close();
    }
}

Remarques

La KeyInfoX509Data classe représente l’élément qui contient des <X509Data> informations de certificat X.509v3 liées à la clé de validation ou de chiffrement. Par exemple, un <X509Data> élément peut contenir un certificat X.509 dont la clé publique objet est la clé de validation, ou une chaîne de certificats X.509 qui se terminent dans un certificat pour la clé de validation, ou d’autres identificateurs pour les certificats X.509 associés.

Utilisez la KeyInfoX509Data classe chaque fois que vous avez besoin d’inclure des données de certificat X.509 dans un document XML chiffré ou signé.

Pour plus d’informations sur l’élément <X509Data> , consultez la section 4.4.4 de la spécification XMLDSIG ou de la spécification de chiffrement XML, qui sont disponibles sur le site web W3C.

Constructeurs

KeyInfoX509Data()

Initialise une nouvelle instance de la classe KeyInfoX509Data.

KeyInfoX509Data(Byte[])

Initialise une nouvelle instance de la classe KeyInfoX509Data à partir du codage ASN.1 DER spécifié d'un certificat X.509v3.

KeyInfoX509Data(X509Certificate, X509IncludeOption)

Initialise une nouvelle instance de la classe KeyInfoX509Data à partir du certificat X.509v3 spécifié.

KeyInfoX509Data(X509Certificate)

Initialise une nouvelle instance de la classe KeyInfoX509Data à partir du certificat X.509v3 spécifié.

Propriétés

Certificates

Obtient une liste des certificats X.509v3 contenus dans l'objet KeyInfoX509Data.

CRL

Obtient ou définit la liste de révocation de certificats (CRL) contenue dans l'objet KeyInfoX509Data.

IssuerSerials

Obtient une liste des structures X509IssuerSerial qui représentent une paire nom d'émetteur/numéro de série.

SubjectKeyIds

Obtient la liste des identificateurs de clé du sujet (SKI) contenus dans l'objet KeyInfoX509Data.

SubjectNames

Obtient une liste des noms des sujets des entités contenues dans l'objet KeyInfoX509Data.

Méthodes

AddCertificate(X509Certificate)

Ajoute le certificat X.509v3 spécifié à KeyInfoX509Data.

AddIssuerSerial(String, String)

Ajoute la paire nom d'émetteur/numéro de série spécifiée à l'objet KeyInfoX509Data.

AddSubjectKeyId(Byte[])

Ajoute le tableau d'octets de l'identificateur de clé du sujet (SKI) spécifié à l'objet KeyInfoX509Data.

AddSubjectKeyId(String)

Ajoute la chaîne de l'identificateur de clé du sujet (SKI) spécifié à l'objet KeyInfoX509Data.

AddSubjectName(String)

Ajoute à l'objet KeyInfoX509Data le nom de sujet de l'entité pour laquelle un certificat X.509v3 a été émis.

Equals(Object)

Détermine si l'objet spécifié est égal à l'objet actuel.

(Hérité de Object)
GetHashCode()

Fait office de fonction de hachage par défaut.

(Hérité de Object)
GetType()

Obtient le Type de l'instance actuelle.

(Hérité de Object)
GetXml()

Retourne une représentation XML de l'objet KeyInfoX509Data.

LoadXml(XmlElement)

Analyse l'objet XmlElement en entrée et configure l'état interne de l'objet KeyInfoX509Data à faire correspondre.

MemberwiseClone()

Crée une copie superficielle du Object actuel.

(Hérité de Object)
ToString()

Retourne une chaîne qui représente l'objet actuel.

(Hérité de Object)

S’applique à

Produit Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 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