Visual Basic Source: KeyFromNode.frm

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - Digital Signatures

Visual Basic Source: KeyFromNode.frm

Dim xmldoc As New DOMDocument50
Dim xmldsig As New MXDigitalSignature50
Dim dsigKey As IXMLDSigKey
Dim DSIGNS As String
Dim DispStr, file

Private Function LoadXML()
    ' Read the input xml file and display the content in the Text1.
    DispStr = ""
    If xmldoc.xml = "" Then
        If file = "" Then
            DispStr = "invalid input xml file name"
            LoadXML = False
            Exit Function
        End If
    End If
        
    Path = App.Path + "\" + file
    xmldoc.async = False
    xmldoc.preserveWhiteSpace = True
    xmldoc.validateOnParse = False
    If xmldoc.Load(Path) = False Then
        DispStr = DispStr + vbNewLine + _
            "can't load " + Path + vbNewLine + xmldoc.parseError.reason
        LoadXML = False
        Exit Function
    End If
    xmldoc.setProperty "SelectionNamespaces", DSIGNS
     
    DispStr = "Input signature element:" + vbNewLine + vbNewLine _
             + xmldoc.xml + vbNewLine
    LoadXML = True
End Function

Private Function VerifyXML()
    If xmldoc.xml = "" Then
        DispStr = "signature doc is empty."
        VerifyXML = False
        Exit Function
    End If
    
    DispStr = DispStr + vbNewLine
    Set signature = xmldoc.selectSingleNode(".//ds:Signature")
    Set xmldsig.signature = signature
    
    Set oKeyInfo = xmldoc.selectSingleNode(".//ds:KeyInfo/ds:KeyValue")
    Set oPubKey = xmldsig.createKeyFromNode(oKeyInfo)
    Set oVerifiedKey = xmldsig.verify(oPubKey)
    
    If oVerifiedKey Is Nothing Then
        DispStr = "verify failed." + vbNewLine
    End If
         
    DispStr = DispStr + "The signature has been verified." _
            + vbNewLine + vbNewLine
    
    VerifyXML = True
End Function

Private Sub Form_Load()

    ' Resize the text box control to the size of the form
    Text1.Top = 100
    Text1.Left = 100
    Text1.Width = Form1.Width - 350
    Text1.Height = Form1.Height - 750

    DSIGNS = "xmlns:ds='http://www.w3.org/2000/09/xmldsig#'"
    file = "signature.dsa.xml"
    
    If LoadXML = False Then
       Text1.Text = DispStr
       Exit Sub
    End If
    
    
    If VerifyXML = False Then
       Text1.Text = DispStr
       Exit Sub
    End If
    
    Text1.Text = DispStr
End Sub

Private Sub Form_Resize()
    ' Resize the text box control to the size of the form
    Text1.Width = Form1.Width - 350
    Text1.Height = Form1.Height - 750
End Sub

Try It!

  1. Ensure that you have completed all the procedures in Getting Started with XML Digital Signatures.
  2. Copy the XML signature resource file, and paste it into a text file. Save the file as signature.dsa.xml.
  3. Create a Standard EXE project in Visual Basic. Save the empty project as KeyFromNode.vbp to the same directory where you saved signature.dsa.xml. Name the form file KeyFromNode.frm.
  4. Create a reference to MSXML 5.0. To do this, select References... from the Project menu, and then check the box for Microsoft XML, v5.0.
  5. Place a TextBox control onto the form. It should be named as Text1 (the default name). Click it to select it. From the Properties window, modify the following properties for Text1: set the ScrollBars property to "2 – Vertical" and the MultiLine Property to True.
  6. Copy the Visual Basic code listing above, and paste it into the code editor to replace whatever code is already there.
  7. Execute the code by selecting Start from the Run menu.
  8. Verify that your output is the same as that listed in the Output. Resize the form as needed to view the output.