PROBLEM CERTIFICATE X509

J

Jesus Suarez

Hi all.

I have a problem when signing the xml using x509 certificate created with
private and public keys.

Here is my code example



getting certificate:

Dim cert As X509Certificate2 = Nothing

Dim store As New X509Store("My", StoreLocation.LocalMachine)

Try

store.Open(OpenFlags.ReadOnly Or OpenFlags.OpenExistingOnly)

Dim CertCol As X509Certificate2Collection = store.Certificates

Dim c As X509Certificate2

For Each c In CertCol

If c.Subject.Contains(CertificateSubject) Then

cert = c

Exit For

End If

Next c

If cert Is Nothing Then

Throw New CryptographicException("The certificate could not be found.")

End If

Finally

store.Close()

End Try

Return cert



signing xml:

Dim MSCert As X509Certificate2 = cert 'GetCertificateBySubject(conf_Cer)

If MSCert Is Nothing Then Throw New Exception("Cert is nothing")


Dim signedXml As New SignedXml(Doc)

signedXml.SigningKey = MSCert.PrivateKey

Dim reference As New Reference()

reference.Uri = ""

Dim trns As XmlDsigC14NTransform = New XmlDsigC14NTransform()

reference.AddTransform(trns)

Dim env As New XmlDsigEnvelopedSignatureTransform()

reference.AddTransform(env)

signedXml.AddReference(reference)

Dim keyInfo As New KeyInfo()

keyInfo.AddClause(New KeyInfoX509Data(MSCert))

signedXml.KeyInfo = keyInfo

signedXml.ComputeSignature()

Dim xmlDigitalSignature As XmlElement = signedXml.GetXml()

Doc.DocumentElement.AppendChild(Doc.ImportNode(xmlDigitalSignature, True))



The keyset does not exist exception happens here:


Dim MSCert As X509Certificate2 = cert 'GetCertificateBySubject(conf_Cer)

If MSCert Is Nothing Then Throw New Exception("Cert is nothing")


Dim signedXml As New SignedXml(Doc)

signedXml.SigningKey = MSCert.PrivateKey
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top