Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2010 > Visual Basic 2010 General Discussion
Visual Basic 2010 General Discussion For any discussions about Visual Basic 2010 topics which aren't related to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2010 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old June 15th, 2014, 09:11 AM
Registered User
Join Date: Jun 2014
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Signing an xml document with a certificate

Hello everyone,

I need to sign this xml file with a x509 v3 certificate but the signature must be at affixed there:

Invoice/ext:UBLExtensions/ext:UBLExtension/ext:ExtensionContent (lines 19-20)

I found a code to sign an xml file but it's affixed at the end of the document, how shall i proceed?

Here is my code:

Dim f_adresse As String = System.AppDomain.CurrentDomain.BaseDirectory
Dim f_temp As String = f_adresse & "TEMP\"
Dim f_fichier As String = "20381235051-01-FF11-04.xml"
Dim f_certificat As String = f_adresse & "aG9CcVpHVndCWTd3WlVOVw==.p12"
Dim f_pwd As String = "xxxxxxxxxxxxxxxx"
Dim xmlFile As String = f_temp & f_fichier
Dim MonCertificat As X509Certificate2 = New X509Certificate2(f_certificat, f_pwd)
Dim xmlDoc As XmlDocument = New XmlDocument()
xmlDoc.PreserveWhitespace = True
Dim signedXml As SignedXml = New SignedXml(xmlDoc)
Dim KeyInfo As KeyInfo = New KeyInfo()
Dim Reference As Reference = New Reference()
Reference.Uri = ""
Reference.AddTransform(New XmlDsigEnvelopedSignatureTransform())
Dim X509Chain As X509Chain = New X509Chain()
Dim local_element As X509ChainElement = X509Chain.ChainElements(0)
Dim x509Data As KeyInfoX509Data = New KeyInfoX509Data(local_element.Certificate)
Dim subjectName As String = local_element.Certificate.Subject
signedXml.KeyInfo = KeyInfo
Dim signature As XmlElement = signedXml.GetXml()
For Each node As XmlNode In signature.SelectNodes("descendant-or-self::*[namespace-uri()='http://www.w3.org/2000/09/xmldsig#']")
    node.Prefix = "ds"
    If node.LocalName = "Signature" Then
        Dim newAttribute As XmlAttribute = xmlDoc.CreateAttribute("Id")
        newAttribute.Value = "SignatureSP"
     End If
Next node
xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(signature, True))

Similar Threads
Thread Thread Starter Forum Replies Last Post
Create an XML document using JAXP and XML fragments. WilliamYou XML 2 October 29th, 2012 03:44 PM
unable to find the manifest signing certificate in the certificate store kgmmurugesh Visual Studio 2008 0 November 16th, 2011 07:31 AM
Split xml file with result document and javax.xml.transform.Transformer. nisargmca XSLT 3 January 12th, 2010 06:26 AM
Adding SSL Certificate to Server.xml ZiGz BOOK: Professional Apache Tomcat 0 August 24th, 2007 08:56 PM
ClickOnce Deployment - Code Signing - Certificate oceaniana General .NET 1 April 18th, 2006 10:01 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.