使用 WSE 2.0 和 .NET 获取本地数字证书信息






4.60/5 (4投票s)
2007年2月27日
2分钟阅读

45951

329
本文档描述了如何获取存储在本地机器上的数字证书信息。它还展示了如何按名称搜索证书,以及检索证书哈希值。
引言
首先,我想说明这是我的第一篇文章,所以请大家多多包涵 :) 本文描述了如何获取存储在本地机器上的数字证书信息;更具体地说,它会查看你的系统提供程序。它还展示了如何按名称搜索证书,以及检索证书哈希值。有关 .NET 加密学的更多信息,请阅读这篇文章。
背景
已经有很多关于 .NET 中加密类别的文章发表,所以我不会深入探讨这些内容,而是建议你阅读上面提到的文章。 在本文中,我将向你展示如何访问你的数字证书信息并使用它;特别是,我将给你一个示例,说明如何从“CurrentUser”和“LocalMachine”存储位置检索信息。 如果你需要按名称搜索证书,或者计划获取证书哈希值,那么这篇文章就是为你准备的。 最近,我需要编写一些代码,该代码必须基于证书名称或证书密钥查找证书。 之后,我需要使用该证书的私钥加密一些数据。 虽然听起来很简单,但当你尝试编写代码时,你会注意到你无法真正看到哈希值,因为它被编码了。 因此,我必须获取证书,然后首先将其转换为字符串。
代码
在这个项目的核心,我有一个函数 getCertFromStore
,它将根据请求的证书信息从存储中检索所有证书。 它有一个可选参数 Name
,它将搜索具有该名称的证书。
Public Function getCertFromStore(ByVal pCert_info As X509_Certificate_info, _
ByRef psError_Msg As String, _
Optional ByVal Name As String = "") _
As X509CertificateCollection
Dim certLocation As X509CertificateStore.StoreLocation
If pCert_info.Certficate_Location = "LocalMachine" Then
certLocation = X509CertificateStore.StoreLocation.LocalMachine
ElseIf pCert_info.Certficate_Location = "CurrentUser" Then
certLocation = X509CertificateStore.StoreLocation.CurrentUser
Else
psError_Msg = "Error Setting Location"
Return Nothing
End If
Dim certProvider As X509CertificateStore.StoreProvider
' for this test use system provider
If pCert_info.Provider = "System" Then
'Opening the System Store
certProvider = X509CertificateStore.StoreProvider.System
Else
psError_Msg = "Retrieving certificate data: " & _
"StoreProvider is not SYSTEM Store "
Return Nothing
End If
Dim certStore As X509CertificateStore
'Specify store there certificates reside
If pCert_info.Store_Name.ToUpper = "MY" Then
certStore = New X509CertificateStore(certProvider, _
certLocation, pCert_info.Store_Name)
ElseIf pCert_info.Store_Name.ToUpper = "ROOT" Then
certStore = _
X509CertificateStore.LocalMachineStore(
X509CertificateStore.RootStore.ToString())
Else
psError_Msg = "Unknown Store"
Return Nothing
End If
Try
' Try opening certificate store store
Dim boolOpen As Boolean = certStore.OpenRead()
Catch ex As Exception
psError_Msg = "Can not open certificate store for Provider:" & _
certProvider & " Location:" & _
certLocation & " Store Name:" & _
pCert_info.Store_Name
Return Nothing
End Try
Dim cersCollection As X509CertificateCollection
If Name <> "" Then
'Search for the certificate in the store based
' on the subject name (exact match)
cersCollection = certStore.FindCertificateBySubjectString(Name)
Else
cersCollection = certStore.Certificates
End If
Return cersCollection
End Function
获取存储中的所有证书后,我们可以根据需要获取有关单个证书的所有信息。
txtName.Text= certMy.GetName
txtHash.Text = System.Convert.ToBase64String(certMy.GetKeyIdentifier)
如果需要使用 RSACryptoServiceProvider
,我还会进行检查以确保我拥有私钥。
Try
rsaCSP = CType(certMy.Key, RSACryptoServiceProvider)
MsgBox("You have access to private keys on this certificate", _
MsgBoxStyle.Information)
'Here you can start encrypting stuff
Catch ex As Exception
MsgBox("Error Getting RSACryptoServiceProvider" & _
ex.Message, MsgBoxStyle.Critical)
End Try
结论
虽然这段代码并不复杂,但当你需要使用证书哈希值时,它会变得非常有用,因为你将无法从证书控制台中获取它。 请注意,certMy
具有许多你可以使用的属性。