Chilkat HOME ASP Visual Basic VB.NET C# Visual C++ C MFC Delphi FoxPro Java Perl PHP Python Ruby SQL Server VBScript
|
Find Certificates for Signing This VB.NET example program shows how to iterate over the certificates on a computer to find those that can be used for signing. ' What certificates exist on my computer, and which
' ones can I use for creating digital signatures?
Dim ccs As New Chilkat.CreateCS()
Dim crypt As New Chilkat.Crypt2()
Dim unlocked As Boolean
unlocked = crypt.UnlockComponent("anything begins 30-day trial")
If unlocked = 0 Then
MsgBox("Encryption component is not unlocked!")
Exit Sub
End If
' Use the Microsoft Enhanced Cryptographic Service Provider
' Add a reference to ChilkatUtil for the ChilkatCSP object
Dim csp As New Chilkat.Csp()
csp.SetProviderMicrosoftEnhanced()
crypt.SetCSP(csp)
' -------------------------------------------------------
' First check the current-user certificate store.
' If running in ASP.NET, do not check the current-user store.
' -------------------------------------------------------
Dim certStore As Chilkat.CertStore
certStore = ccs.OpenCurrentUserStore()
TextBox1.Text = "Current User Certificate Store -------------"
TextBox1.Text = TextBox1.Text & vbCrLf & "-"
Dim cert As Chilkat.Cert
Dim signature As String
Dim i As Integer
For i = 0 To certStore.NumCertificates - 1
cert = certStore.GetCertificate(i)
' Display the certificate we are testing..
TextBox1.Text = TextBox1.Text & vbCrLf & cert.SubjectDN
' Set the certificate to be used for signing, and our output string encoding.
crypt.SetSigningCert(cert)
crypt.EncodingMode = "base64"
' Try signing....
signature = crypt.SignStringENC("This is a test")
If Len(signature) = 0 Then
' Failed to create signature
TextBox1.Text = TextBox1.Text & vbCrLf & "FAILED"
If cert.HasPrivateKey() = 0 Then
TextBox1.Text = TextBox1.Text & vbCrLf & "NO PRIVATE KEY"
End If
TextBox1.Text = TextBox1.Text & vbCrLf & crypt.LastErrorText
Else
TextBox1.Text = TextBox1.Text & vbCrLf & "SUCCESS!"
TextBox1.Text = TextBox1.Text & vbCrLf & crypt.LastErrorText
End If
TextBox1.Text = TextBox1.Text & vbCrLf & "----------------" & vbCrLf
TextBox1.Refresh()
Next
' -------------------------------------------------------
' Now check the local machine certificate store.
' -------------------------------------------------------
certStore = ccs.OpenLocalSystemStore()
TextBox1.Text = TextBox1.Text & vbCrLf & "---------------------------------------------"
TextBox1.Text = TextBox1.Text & vbCrLf & "Local Machine Certificate Store -------------"
TextBox1.Text = TextBox1.Text & vbCrLf & "---------------------------------------------"
TextBox1.Text = TextBox1.Text & vbCrLf & "-"
For i = 0 To certStore.NumCertificates - 1
cert = certStore.GetCertificate(i)
' Display the certificate we are testing..
TextBox1.Text = TextBox1.Text & vbCrLf & cert.SubjectDN
' Set the certificate to be used for signing, and our output string encoding.
crypt.SetSigningCert(cert)
crypt.EncodingMode = "base64"
' Try signing....
signature = crypt.SignStringENC("This is a test")
If Len(signature) = 0 Then
' Failed to create signature
TextBox1.Text = TextBox1.Text & vbCrLf & "FAILED"
If cert.HasPrivateKey() = 0 Then
TextBox1.Text = TextBox1.Text & vbCrLf & "NO PRIVATE KEY"
End If
TextBox1.Text = TextBox1.Text & vbCrLf & crypt.LastErrorText
Else
TextBox1.Text = TextBox1.Text & vbCrLf & "SUCCESS!"
TextBox1.Text = TextBox1.Text & vbCrLf & crypt.LastErrorText
End If
TextBox1.Text = TextBox1.Text & vbCrLf & "----------------" & vbCrLf
TextBox1.Refresh()
Next
' -------------------------------------------------------
' Now check Outlook's certificate store.
' If running in ASP.NET, do not check the Outlook store.
' -------------------------------------------------------
certStore = ccs.OpenOutlookStore()
If certStore Is Nothing Then
Exit Sub
End If
TextBox1.Text = TextBox1.Text & vbCrLf & "---------------------------------------------"
TextBox1.Text = TextBox1.Text & vbCrLf & "Outlook Certificate Store -------------------"
TextBox1.Text = TextBox1.Text & vbCrLf & "---------------------------------------------"
TextBox1.Text = TextBox1.Text & vbCrLf & "-"
For i = 0 To certStore.NumCertificates - 1
cert = certStore.GetCertificate(i)
' Display the certificate we are testing..
TextBox1.Text = TextBox1.Text & vbCrLf & cert.SubjectDN
' Set the certificate to be used for signing, and our output string encoding.
crypt.SetSigningCert(cert)
crypt.EncodingMode = "base64"
' Try signing....
signature = crypt.SignStringENC("This is a test")
If Len(signature) = 0 Then
' Failed to create signature
TextBox1.Text = TextBox1.Text & vbCrLf & "FAILED"
If cert.HasPrivateKey() = 0 Then
TextBox1.Text = TextBox1.Text & vbCrLf & "NO PRIVATE KEY"
End If
TextBox1.Text = TextBox1.Text & vbCrLf & crypt.LastErrorText
Else
TextBox1.Text = TextBox1.Text & vbCrLf & "SUCCESS!"
TextBox1.Text = TextBox1.Text & vbCrLf & crypt.LastErrorText
End If
TextBox1.Text = TextBox1.Text & vbCrLf & "----------------" & vbCrLf
TextBox1.Refresh()
Next
Important: The download for this
example does not contain the ChilkatDotNet.dll which |
Need a specific example? Send a request to support@chilkatsoft.com
© 2000-2007 Chilkat Software, Inc. All Rights Reserved.