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!