' Be sure to select "CAPICOM v2.0 Type Library" ' and "Microsoft XML v5.0" options from the ' "Project->References..." menu item in ' Visual Studio. ' Dim xmldoc As New DOMDocument50 Dim xmldsig As New MXDigitalSignature50 Dim dsigKey As IXMLDSigKey Dim dataObj As IXMLDOMNode Dim infile, provType, keyContainer Dim certStore As New store Const DSIGNS = "xmlns:ds='http://www.w3.org/2000/09/xmldsig#'" Const PROV_RSA_FULL = 1 '
Change this key container name to your own if necessary.Const RSA_KEY = "MyRSAFullKeys"
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 text3.
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 SignXML(fwWriteKeyInfo As Long)
If xmldsig.signature Is Nothing Then
WriteLine "Invalid signature template."
SignXML = False
Exit Function
End If
If keyContainer = "" Then
WriteLine "Invalid key container."
SignXML = False
Exit Function
End If
Set oKey = xmldsig.createKeyFromCSP(provType, "", keyContainer, 0)
If oKey Is Nothing Then
WriteLine "Invalid key"
SignXML = False
Exit Function
End If
Set oSignedKey = xmldsig.sign(oKey, fwWriteKeyInfo)
If oSignedKey Is Nothing Then
WriteLine "sign failed."
SignXML = False
Exit Function
End If
WriteLine "The specified data was signed succesffully."
WriteLine "Resultant signature: "
WriteLine 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 - 750provType = PROV_RSA_FULL
keyContainer = RSA_KEY
infile = "signature_template.store.rsa.xml"
writeClear
certStore.open CAPICOM_CURRENT_USER_STORE, "MY", CAPICOM_STORE_OPEN_READ_ONLY
If LoadXML(infile) = True Then
WriteLine "Sign with WriteKeyInfo = CERTIFICATES:"
SignXML CERTIFICATES + PURGE
Set xmldsig.store = certStore
WriteLine "certStore set on xmldsig."
WriteLine "number of certificates in the store = " & certStore.CERTIFICATES.Count
WriteLine ""
WriteLine "Sign with WriteKeyInfo = CERTIFICATES:"
SignXML CERTIFICATES + PURGE
End If
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!