Visual Basic Source: signature.frm
Dim xmldoc As New DOMDocument50 Dim xmldsig As New MXDigitalSignature50 Dim dsigKey As IXMLDSigKey Dim DispStr, file, provType, keyContainer Const DSIGNS = "xmlns:ds='http://www.w3.org/2000/09/xmldsig#'" Const PROV_RSA_FULL = 1 Private Function LoadXML() ' Read the input XML file, and display the content in ' the Text1 text box. 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 xmldoc.resolveExternals = False If xmldoc.Load(Path) = False Then DispStr = DispStr + vbNewLine + _ "can't load " + Path + vbNewLine + xmldoc.parseError.reason LoadXML = False Exit Function End If DispStr = "Input signature template:" + vbNewLine + vbNewLine _ + xmldoc.xml + vbNewLine LoadXML = True End Function Private Function SignXML() If xmldoc.xml = "" Then DispStr = "signature template is empty." SignXML = False Exit Function End If If keyContainer = "" Then DispStr = DispStr + "invalid keyContainer." SignXML = False Exit Function End If xmldoc.async = False xmldoc.preserveWhiteSpace = True xmldoc.validateOnParse = False xmldoc.setProperty "SelectionNamespaces", DSIGNS Set signature = xmldoc.selectSingleNode(".//ds:Signature") Set xmldsig.signature = signature Set oKey = xmldsig.createKeyFromCSP(provType, "", keyContainer, 0) Set oSignedKey = xmldsig.sign(oKey, 1) If oSignedKey Is Nothing Then DispStr = "sign failed." + vbNewLine End If DispStr = DispStr + "The data referenced in the signature template " _ + "was signed successfully." + vbNewLine _ + "Resultant signature:" + vbNewLine + vbNewLine DispStr = DispStr + xmldoc.xml SignXML = True End Function Private Sub Form_Load() 'Set text box to use form to determine its width 'and height when form is loaded Text1.Left = 100 Text1.Top = 100 Text1.Width = Form1.Width - 350 Text1.Height = Form1.Height - 750 provType = PROV_RSA_FULL file = "signature_template.xml" ' Change this key container name to your own if necessary. keyContainer = "MyRSAFullKeys" If LoadXML = False Then MsgBox DispStr Exit Sub End If If SignXML = False Then MsgBox DispStr Exit Sub End If Text1.Text = DispStr End Sub Private Sub Form_Resize() 'Set text box to use form in determining its width and height 'when form is resized Text1.Width = Form1.Width - 350 Text1.Height = Form1.Height - 750 End Sub
Try It!
- Ensure that you have completed all the procedures in Getting Started with XML Digital Signatures.
- Copy the XML signature template, and paste it into a text file. Save the file as signature_template.xml.
- Create a Standard EXE project in Visual Basic. Save the empty project as signature.vbp to the same directory where you saved signature_template.xml. Name the form file signature.frm.
- Create a reference to MSXML 5.0. To do this, select References... from the Project menu, then check the box for Microsoft XML, v5.0.
- Double click on the TextBox icon from the tools menu. A TextBox control will appear on the project's form named Text1. In the properties for Text1, set the ScrollBars property to use "2 - Vertical" as the value and set the MultiLine property to True.
- Copy the Visual Basic code listing above, and paste it into the Visual Basic code editor to replace whatever code is already there.
- Execute the code by selecting Start from the Run menu.
- Verify that your output is similar to that listed in the Output topic. To view the contents of the output more easily, resize the application form as needed.