Imports System.Security.Cryptography
Imports System.Security.Cryptography.Xml
Imports System.Xml
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
’ Create a new CspParameters object to specify
’ a key container.
Dim cspParams As New CspParameters()
cspParams.KeyContainerName = "XML_DSIG_RSA_KEY"
’ Create a new RSA signing key and save it in the container.
Dim rsaKey As New RSACryptoServiceProvider(cspParams)
’ Create a new XML document.
Dim xmlDoc As New XmlDocument()
’ Load an XML file into the XmlDocument object.
xmlDoc.PreserveWhitespace = True
xmlDoc.LoadXml("
’ Sign the XML document.
SignXml(xmlDoc, rsaKey)
MsgBox("XML file signed.")
’ Save the document.
xmlDoc.Save("c:\ok.xml")
MsgBox(VerifyXml(xmlDoc, rsaKey))
End Sub
’ Sign an XML file.
’ This document cannot be verified unless the verifying
’ code has the key with which it was signed.
Sub SignXml(ByVal Doc As XmlDocument, ByVal Key As RSA)
’ Check arguments.
If Doc Is Nothing Then
Throw New ArgumentException("Doc")