Visual Basic Source: KeyFromCSP.frm
[This sample code uses features that were implemented in MSXML 5.0 for Microsoft Office Applications. XML digital signatures are not supported in MXSML 6.0 and later.]
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!
Ensure that you have completed all the procedures in Getting Started with XML Digital Signatures.
Copy the XML signature template Resource: signature_template.rsa.xml, and paste it into a text file. Save the file as signature_template.rsa.xml.
Create a Standard EXE project in Visual Basic. Save the empty project as KeyFromCSP.vbp to the same directory where you saved signature_template.rsa.xml. Name the form file KeyFromCSP.frm.
Create a reference to MSXML 5.0 for Microsoft Office Applications. 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 panel. This results in a TextBox control being placed on the project's form named "Text1". In the Properties window, modify the following properties for "Text1": select "2 - Vertical" as the value for the ScrollBar property and set the value of 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.