Visual Basic Source: verify.frm
[This sample code uses features that were implemented in MSXML 5.0 for Microsoft Office Applications. XML digital signatures are not supported in MXSML 6.0 and later.]
Dim xmldoc As New DOMDocument50
Dim xmldsig As New MXDigitalSignature50
Dim dsigKey As IXMLDSigKey
Dim dataObj As IXMLDOMNode
Const DSIGNS = "xmlns:ds='http://www.w3.org/2000/09/xmldsig#'"
Const INFILE = "signature.verify.dsa.xml"
Private Function WriteLine(ByVal str As String)
Text1.Text = Text1.Text + str + vbNewLine
End Function
Private Function writeClear()
Text1.Text = ""
End Function
Private Function LoadXML(ByVal file As String)
' Read input xml file and display the content in the text1.
Path = App.Path + "\" + file
xmldoc.async = False
xmldoc.preserveWhiteSpace = True
xmldoc.validateOnParse = False
xmldoc.resolveExternals = False
If xmldoc.Load(Path) = False Then
WriteLine "Can't load " + Path
WriteLine "Reason: " + xmldoc.parseError.reason
LoadXML = False
Exit Function
End If
xmldoc.setProperty "SelectionNamespaces", DSIGNS
Set xmldsig.signature = xmldoc.selectSingleNode(".//ds:Signature")
LoadXML = True
End Function
Private Function VerifyXML()
If xmldsig.signature Is Nothing Then
WriteLine "Invalid signature."
VerifyXML = False
Exit Function
End If
Set oKeyInfo = xmldoc.selectSingleNode(".//ds:KeyInfo/ds:KeyValue")
If oKeyInfo Is Nothing Then
WriteLine "Invalid <KeyInfo> element."
VerifyXML = False
Exit Function
End If
Set oPubKey = xmldsig.createKeyFromNode(oKeyInfo)
If oPubKey Is Nothing Then
WriteLine "Can't generate public key for verification."
VerifyXML = False
Exit Function
End If
Set oVerifiedKey = xmldsig.verify(oPubKey)
If oVerifiedKey Is Nothing Then
WriteLine "Signature not verified."
End If
WriteLine "Signature verified."
VerifyXML = True
End Function
Private Sub Form_Load()
writeClear
WriteLine "Verifyin signature."
If LoadXML(INFILE) = True Then
VerifyXML
End If
End Sub
Try It!
Ensure that you have completed all the procedures in Getting Started with XML Digital Signatures.
Copy the XML signature resource file, and paste it into a text file. Save the file as signature.verify.dsa.xml.
Create a Standard EXE project in Visual Basic. Save the empty project as verify.vbp to the same directory where you saved signature.verify.dsa.xml. Name the form file verify.frm.
Create a reference to MSXML 5.0 for Microsoft Office Applications. To do this, select References... from the Project menu, then check the box for Microsoft XML, v5.0.
Copy the Visual Basic code listing above, and paste it into the code editor to replace whatever is already there.
Place a TextBox control onto the form. It should be named as Text1 (the default name). Click it to select it in the Properties window, and then modify the following properties for Text1: set the ScrollBars property to "2 – Vertical" and the MultiLine Property to True.
Execute the code by selecting Start from the Run menu.
Verify that the output is the same as that listed in the Output topic.