CAPICOM 和 X509
CAPICOM and X509
我有一个网络申请表。目的是从 Web 表单创建 xml 数据并使用用户 usb 证书对其进行签名。
我正在使用 CAPICOM.store 成功打开所有用户证书。当我单击我想要的一个时,我将其导出。之后,我将在 X509Cetificate2 中导入选定的证书以签名 xml。但是在我的代码中,我在线上遇到错误
signedXml.ComputeSignature() 消息是 'Signing key is not loaded.' 请提供任何帮助或建议来解决此问题。
Function SignXML(uppXML As String) As String
Dim bResult As Boolean = False
Dim pCertContext As IntPtr = IntPtr.Zero
Dim doc As XmlDocument = Nothing
Dim signedXml As SignedXml = Nothing
Dim reference As Reference = Nothing
Dim trns As XmlDsigC14NTransform = Nothing
Dim env As XmlDsigEnvelopedSignatureTransform = Nothing
Dim keyInfo As KeyInfo = Nothing
Dim xmlDigitalSignature As XmlElement = Nothing
Dim hideFiledCapicom As String = Replace(txtCapicom.Text, " ", "+")
Dim certificate As New X509Certificate2(Convert.FromBase64String(hideFiledCapicom))
Dim key As AsymmetricAlgorithm = certificate.PrivateKey
doc = New XmlDocument
doc.PreserveWhitespace = True
doc.LoadXml(uppXML)
signedXml = New SignedXml(doc)
signedXml.SigningKey = key
reference = New Reference
reference.Uri = ""
trns = New XmlDsigC14NTransform
reference.AddTransform(trns)
env = New XmlDsigEnvelopedSignatureTransform
reference.AddTransform(env)
signedXml.AddReference(reference)
keyInfo = New KeyInfo()
keyInfo.AddClause(New KeyInfoX509Data(certificate))
signedXml.KeyInfo = keyInfo
signedXml.ComputeSignature()
xmlDigitalSignature = signedXml.GetXml()
doc.DocumentElement.AppendChild(doc.ImportNode(xmlDigitalSignature, True))
If TypeOf doc.FirstChild Is XmlDeclaration Then
doc.RemoveChild(doc.FirstChild)
End If
uppXML = doc.OuterXml
Return uppXML
End Function
我找到了解决方案。
在 JavaScript 我使用 CAPICOM 选择和导出证书的地方我也从证书中获取私钥并将其放在隐藏字段中。
var privateKey = certificates.Item(1).PrivateKey.KeySpec
var exportKey = document.getElementById("<%=hideFieldKey.ClientID%>");
exportKey = privateKey
document.getElementById('HiddenKey').value = exportKey;
在我的 vb 代码中,我使用 CspParameters() 获取私钥并使用 RSACryptoServiceProvider() 进一步签署 xml 文档。
我有一个网络申请表。目的是从 Web 表单创建 xml 数据并使用用户 usb 证书对其进行签名。 我正在使用 CAPICOM.store 成功打开所有用户证书。当我单击我想要的一个时,我将其导出。之后,我将在 X509Cetificate2 中导入选定的证书以签名 xml。但是在我的代码中,我在线上遇到错误 signedXml.ComputeSignature() 消息是 'Signing key is not loaded.' 请提供任何帮助或建议来解决此问题。
Function SignXML(uppXML As String) As String
Dim bResult As Boolean = False
Dim pCertContext As IntPtr = IntPtr.Zero
Dim doc As XmlDocument = Nothing
Dim signedXml As SignedXml = Nothing
Dim reference As Reference = Nothing
Dim trns As XmlDsigC14NTransform = Nothing
Dim env As XmlDsigEnvelopedSignatureTransform = Nothing
Dim keyInfo As KeyInfo = Nothing
Dim xmlDigitalSignature As XmlElement = Nothing
Dim hideFiledCapicom As String = Replace(txtCapicom.Text, " ", "+")
Dim certificate As New X509Certificate2(Convert.FromBase64String(hideFiledCapicom))
Dim key As AsymmetricAlgorithm = certificate.PrivateKey
doc = New XmlDocument
doc.PreserveWhitespace = True
doc.LoadXml(uppXML)
signedXml = New SignedXml(doc)
signedXml.SigningKey = key
reference = New Reference
reference.Uri = ""
trns = New XmlDsigC14NTransform
reference.AddTransform(trns)
env = New XmlDsigEnvelopedSignatureTransform
reference.AddTransform(env)
signedXml.AddReference(reference)
keyInfo = New KeyInfo()
keyInfo.AddClause(New KeyInfoX509Data(certificate))
signedXml.KeyInfo = keyInfo
signedXml.ComputeSignature()
xmlDigitalSignature = signedXml.GetXml()
doc.DocumentElement.AppendChild(doc.ImportNode(xmlDigitalSignature, True))
If TypeOf doc.FirstChild Is XmlDeclaration Then
doc.RemoveChild(doc.FirstChild)
End If
uppXML = doc.OuterXml
Return uppXML
End Function
我找到了解决方案。
在 JavaScript 我使用 CAPICOM 选择和导出证书的地方我也从证书中获取私钥并将其放在隐藏字段中。
var privateKey = certificates.Item(1).PrivateKey.KeySpec
var exportKey = document.getElementById("<%=hideFieldKey.ClientID%>");
exportKey = privateKey
document.getElementById('HiddenKey').value = exportKey;
在我的 vb 代码中,我使用 CspParameters() 获取私钥并使用 RSACryptoServiceProvider() 进一步签署 xml 文档。