Visual Basic Source: storeProp.frm
' 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 + vbNewLineEnd FunctionPrivate Function writeClear()Text1.Text = ""End FunctionPrivate Function LoadXML(ByVal file As String)' Read input xml file and display the content in the text3.Path = App.Path + "\" + filexmldoc.async = Falsexmldoc.preserveWhiteSpace = Truexmldoc.validateOnParse = Falsexmldoc.resolveExternals = FalseIf xmldoc.Load(Path) = False ThenWriteLine "Can't load " + PathWriteLine "Reason: " + xmldoc.parseError.reasonLoadXML = FalseExit FunctionEnd Ifxmldoc.setProperty "SelectionNamespaces", DSIGNSSet xmldsig.signature = xmldoc.selectSingleNode(".//ds:Signature")LoadXML = TrueEnd FunctionPrivate Function SignXML(fwWriteKeyInfo As Long)If xmldsig.signature Is Nothing ThenWriteLine "Invalid signature template."SignXML = FalseExit FunctionEnd IfIf keyContainer = "" ThenWriteLine "Invalid key container."SignXML = FalseExit FunctionEnd IfSet oKey = xmldsig.createKeyFromCSP(provType, "", keyContainer, 0)If oKey Is Nothing ThenWriteLine "Invalid key"SignXML = FalseExit FunctionEnd IfSet oSignedKey = xmldsig.sign(oKey, fwWriteKeyInfo)If oSignedKey Is Nothing ThenWriteLine "sign failed."SignXML = FalseExit FunctionEnd IfWriteLine "The specified data was signed succesffully."WriteLine "Resultant signature: "WriteLine xmldoc.xmlSignXML = TrueEnd FunctionPrivate 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_FULLkeyContainer = RSA_KEYinfile = "signature_template.store.rsa.xml"writeClearcertStore.open CAPICOM_CURRENT_USER_STORE, "MY", CAPICOM_STORE_OPEN_READ_ONLYIf LoadXML(infile) = True ThenWriteLine "Sign with WriteKeyInfo = CERTIFICATES:"SignXML CERTIFICATES + PURGESet xmldsig.store = certStoreWriteLine "certStore set on xmldsig."WriteLine "number of certificates in the store = " & certStore.CERTIFICATES.CountWriteLine ""WriteLine "Sign with WriteKeyInfo = CERTIFICATES:"SignXML CERTIFICATES + PURGEEnd IfEnd SubPrivate Sub Form_Resize()'Set text box to use form in determining its width and height'when form is resizedText1.Width = Form1.Width - 350Text1.Height = Form1.Height - 750End Sub
Try It!
- Ensure that you have completed all the procedures in Getting Started with XML Digital Signatures.
- Copy the XML signature template from Resource Files and paste it into a text file. Save the file as signature_template.store.rsa.xml.
- Create a Standard EXE project in Visual Basic. Save the empty project as storeProp.vbp to the same directory where you saved signature_template.store.rsa.xml. Name the form file storeProp.frm.
- Create references to MSXML 5.0 and CAPICOM 2.0. To do this, select References... from the Project menu, and then check the boxes for Microsoft XML, v5.0 and CAPICOM v2.0 Type Library.
- 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 window, set the value for the ScrollBars property to "2 – Vertical" and 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.
- Verify that your output is similar to that listed in the Output topic.
