.NET SignedXml 提供错误的摘要值
.NET SignedXml giving wrong digest value
我正在尝试创建一个已签名的 XML 文档,但我发送它的 Web 服务拒绝了它并声明签名无效。我在 php 中有一些其他代码并且它可以工作,Web 服务接受 php 生成并签名 xml 但不接受 vb.net 。我已经使 php 和 vb.net 输入 xml 完全相等,但它们仍然生成不同的摘要值。我还尝试使用 an online tool 来验证 xml 签名并且 php 那里是正确的,对于 vb.net 它说摘要值是错误的。这是我在 vb 中用于计算签名的函数:
Private Function PotpisiXml(cert As X509Certificate2, ByVal rsa As RSACryptoServiceProvider, ByVal xmlStream As MemoryStream) As XmlDocument
xmlStream.Position = 0
Dim xmlDoc As New XmlDocument()
xmlDoc.Load(xmlStream)
Dim signedXml As New SignedXml(xmlDoc)
signedXml.SigningKey = rsa
Dim reference As New Reference()
reference.Uri = "#racunId"
Dim env As New XmlDsigEnvelopedSignatureTransform()
Dim ec14n As New XmlDsigExcC14NTransform()
reference.AddTransform(env)
reference.AddTransform(ec14n)
signedXml.AddReference(reference)
Dim keyInfo As New KeyInfo()
Dim kdata As New KeyInfoX509Data(cert)
Dim xserial As X509IssuerSerial
xserial.IssuerName = cert.IssuerName.Name
xserial.SerialNumber = cert.SerialNumber
kdata.AddIssuerSerial(xserial.IssuerName, xserial.SerialNumber)
keyInfo.AddClause(kdata)
signedXml.ComputeSignature()
signedXml.KeyInfo = keyInfo
Dim xmlDigitalSignature As XmlElement = signedXml.GetXml()
xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, True))
Return xmlDoc
End Function
vb.net生成xml:https://pastebin.com/jrzLsv4k
php生成xml:https://pastebin.com/kbsE2PH7
xml用于签名的文档对于两种代码来说完全相同。双方的编码都是 UTF-8。
我已经通过删除过程中各处的所有空格和换行符解决了这个问题。
我正在尝试创建一个已签名的 XML 文档,但我发送它的 Web 服务拒绝了它并声明签名无效。我在 php 中有一些其他代码并且它可以工作,Web 服务接受 php 生成并签名 xml 但不接受 vb.net 。我已经使 php 和 vb.net 输入 xml 完全相等,但它们仍然生成不同的摘要值。我还尝试使用 an online tool 来验证 xml 签名并且 php 那里是正确的,对于 vb.net 它说摘要值是错误的。这是我在 vb 中用于计算签名的函数:
Private Function PotpisiXml(cert As X509Certificate2, ByVal rsa As RSACryptoServiceProvider, ByVal xmlStream As MemoryStream) As XmlDocument
xmlStream.Position = 0
Dim xmlDoc As New XmlDocument()
xmlDoc.Load(xmlStream)
Dim signedXml As New SignedXml(xmlDoc)
signedXml.SigningKey = rsa
Dim reference As New Reference()
reference.Uri = "#racunId"
Dim env As New XmlDsigEnvelopedSignatureTransform()
Dim ec14n As New XmlDsigExcC14NTransform()
reference.AddTransform(env)
reference.AddTransform(ec14n)
signedXml.AddReference(reference)
Dim keyInfo As New KeyInfo()
Dim kdata As New KeyInfoX509Data(cert)
Dim xserial As X509IssuerSerial
xserial.IssuerName = cert.IssuerName.Name
xserial.SerialNumber = cert.SerialNumber
kdata.AddIssuerSerial(xserial.IssuerName, xserial.SerialNumber)
keyInfo.AddClause(kdata)
signedXml.ComputeSignature()
signedXml.KeyInfo = keyInfo
Dim xmlDigitalSignature As XmlElement = signedXml.GetXml()
xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, True))
Return xmlDoc
End Function
vb.net生成xml:https://pastebin.com/jrzLsv4k
php生成xml:https://pastebin.com/kbsE2PH7
xml用于签名的文档对于两种代码来说完全相同。双方的编码都是 UTF-8。
我已经通过删除过程中各处的所有空格和换行符解决了这个问题。