我有一份网络申请表。目的是从Web表单创建xml数据并使用用户usb证书对其进行签名。 我正在使用CAPICOM.store成功打开所有用户证书。当我点击我想要的一个时,我将其导出。之后我在X509Cetificate2中导入选定的证书以签署xml。但在我的代码中我得到了错误 signedXml.ComputeSignature()和消息是'未加载签名密钥'。请任何帮助或建议来解决这个问题。
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
答案 0 :(得分:0)
我找到了解决方法。
在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文档。