Dim xmldoc As New DOMDocument50 Dim xmldsig As New MXDigitalSignature50 Dim dsigKey As IXMLDSigKey Dim DispStr, file Dim outfile, provType, keyContainer Const NOKEYINFO = 0 Const KEYVALUE = 1 Const CERTIFICATES = 2 Const PURGE = 4 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 text3. 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 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, KEYVALUE) If oSignedKey Is Nothing Then DispStr = "sign failed." + vbNewLine End If DispStr = DispStr + "The data referenced in the signature template " _ + "was signed succussfully." + vbNewLine _ + "Resultant signature:" + vbNewLine + vbNewLine DispStr = DispStr + xmldoc.xml + vbNewLine output = App.Path + "\" + outfile xmldoc.save (output) 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 ' Change this key container name to your own if necessary. keyContainer = "MyRSAFullKeys" file = "signature_template.rsa.xml" outfile = "signature_document.rsa.xml" If LoadXML = False Then Text1.Text = DispStr Exit Sub End If If SignXML = False Then Text1.Text = 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!