【发布时间】:2016-07-16 18:12:30
【问题描述】:
这两个函数应该是等价的,但是VerifyBouncyCastle() 对于VerifyDotNet() 返回true 的相同输入返回false(验证失败)。该消息使用 .Net 的DSACryptoServiceProvider.SignHash() 签名。 VerifyBouncyCastle() 有什么问题导致其行为与 .Net 版本不同?
.Net 内置加密库:
Private Function VerifyDotNet(hash() As Byte, p() As Byte, q() As Byte, g() As Byte, y() As Byte, r() As Byte, s() As Byte) As Boolean
'Public key
Dim dotNetDSA As New DSACryptoServiceProvider()
Dim dotNetParameters As New DSAParameters
dotNetParameters.P = p
dotNetParameters.Q = q
dotNetParameters.G = g
dotNetParameters.Y = y
dotNetDSA.ImportParameters(dotNetParameters)
'Signature
Dim signature(39) As Byte
Array.ConstrainedCopy(r, 0, signature, 0, 20)
Array.ConstrainedCopy(s, 0, signature, 20, 20)
'Verify
Return dotNetDSA.VerifyHash(hash, "SHA1", signature)
End Function
BouncyCastle:
Private Function VerifyBouncyCastle(hash() As Byte, p() As Byte, q() As Byte, g() As Byte, y() As Byte, r() As Byte, s() As Byte) As Boolean
'Public key
Dim pBigInt As New Org.BouncyCastle.Math.BigInteger(p)
Dim qBigInt As New Org.BouncyCastle.Math.BigInteger(q)
Dim gBigInt As New Org.BouncyCastle.Math.BigInteger(g)
Dim yBigInt As New Org.BouncyCastle.Math.BigInteger(y)
Dim bouncyParameters As New Org.BouncyCastle.Crypto.Parameters.DsaParameters(pBigInt, qBigInt, gBigInt)
Dim bouncyPublicKey As New Org.BouncyCastle.Crypto.Parameters.DsaPublicKeyParameters(yBigInt, bouncyParameters)
Dim bouncyDSA As New Org.BouncyCastle.Crypto.Signers.DsaSigner
bouncyDSA.Init(False, bouncyPublicKey)
'Signature
Dim signatureR As New Org.BouncyCastle.Math.BigInteger(r)
Dim signatureS As New Org.BouncyCastle.Math.BigInteger(s)
'Verify
Return bouncyDSA.VerifySignature(hash, signatureR, signatureS)
End Function
我已经尝试反转字节数组,并且还验证了 BouncyCastle 的 Sha1Digest.DoFinal() 为相同的消息生成了与 .Net 的 SHA1Managed.ComputeHash() 相同的哈希值,后者用于生成用于签名的哈希值。
【问题讨论】:
标签: vb.net bouncycastle dsa