Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

KeyInfoX509Data Class

Represents an <X509Data> subelement of an XMLDSIG or XML Encryption <KeyInfo> element.

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

'Declaration
Public Class KeyInfoX509Data _
	Inherits KeyInfoClause
'Usage
Dim instance As KeyInfoX509Data

The KeyInfoX509Data class represents the <X509Data> element that contains X.509v3 certificate information related to the validation or encryption key. For example, an <X509Data> element might contain an X.509 certificate whose subject public key is the validation key, or a chain of X.509 certificates that terminate in a certificate for the validating key, or other identifiers for related X.509 certificates.

Use the KeyInfoX509Data class whenever you need to include X.509 certificate data in an encrypted or signed XML document.

For more information about the <X509Data> element, see Section 4.4.4 of the XMLDSIG specification, which is available from the World Wide Web Consortium (W3C) at www.w3.org/TR/xmldsig-core/, or see the XML Encryption specification, which is available from the W3C at www.w3.org/TR/xmlenc-core/.

This section contains two code examples. The first example demonstrates how to sign an XML file using a detached signature. The second example demonstrates how to sign an XML file using an envelope signature.

Example #1


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

Imports System
Imports System.Security.Cryptography
Imports System.Security.Cryptography.Xml
Imports System.Security.Cryptography.X509Certificates
Imports System.Text
Imports System.Xml





Class XMLDSIGDetached


   <STAThread()>  _
   Overloads Shared Sub Main(args() As String)
      ' The URI to sign. 
      Dim resourceToSign As String = "http://www.microsoft.com" 

      ' The name of the file to which to save the XML signature. 
      Dim XmlFileName As String = "xmldsig.xml" 

      ' The name of the X509 certificate 
      Dim Certificate As String = "microsoft.cer" 

      Try 

         ' Generate a signing key. This key should match the  
         ' certificate. 
         Dim Key As New RSACryptoServiceProvider()

         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 succesfully computed and saved to {0}.", XmlFileName)

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



   ' Sign an XML file and save the signature in a new file. 
   Public Shared Sub SignDetachedResource(URIString As String, XmlSigFileName As String, Key As RSA, Certificate As String)
      ' Create a SignedXml object. 
      Dim signedXml As New SignedXml()

      ' Assign the key to the SignedXml object.
      signedXml.SigningKey = Key

      ' Create a reference to be signed. 
      Dim reference As 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. 
      Dim keyInfo As New KeyInfo()

      ' Load the X509 certificate. 
      Dim MSCert As X509Certificate = 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. 
      Dim xmlDigitalSignature As XmlElement = signedXml.GetXml()

      ' Save the signed XML document to a file specified 
      ' using the passed string. 
      Dim xmltw As New XmlTextWriter(XmlSigFileName, New UTF8Encoding(False))
      xmlDigitalSignature.WriteTo(xmltw)
      xmltw.Close()
   End Sub   
End Class

Example #2


' This example signs an XML file using an 
' envelope signature. It then verifies the  
' signed XML. 

Imports System
Imports System.Security.Cryptography
Imports System.Security.Cryptography.Xml
Imports System.Security.Cryptography.X509Certificates
Imports System.Text
Imports System.Xml



Public Class SignVerifyEnvelope


   Overloads Public Shared Sub Main(args() As [String])

      Dim Certificate As String = "microsoft.cer" 

      Try 
         ' Generate a signing key. 
         Dim Key As New RSACryptoServiceProvider()

         ' 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 e As CryptographicException
         Console.WriteLine(e.Message)
      End Try 
   End Sub  


   ' Sign an XML file and save the signature in a new file. 
   Public Shared Sub SignXmlFile(FileName As String, SignedFileName As String, Key As RSA, Certificate As String)
      ' 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

      ' 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)

      ' Create a new KeyInfo object. 
      Dim keyInfo As New KeyInfo()

      ' Load the X509 certificate. 
      Dim MSCert As X509Certificate = 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. 
      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  


   ' Create example data to sign. 
   Public Shared Sub CreateSomeXml(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, "", "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. 
      Dim xmltw As New XmlTextWriter(FileName, New UTF8Encoding(False))
      document.WriteTo(xmltw)
      xmltw.Close()
   End Sub  
End Class

System.Object
  System.Security.Cryptography.Xml.KeyInfoClause
    System.Security.Cryptography.Xml.KeyInfoX509Data

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft