【问题标题】:Equivalent DSA verifiers with BouncyCastle and DSACryptoServiceProvider具有 BouncyCastle 和 DSACryptoServiceProvider 的等效 DSA 验证器
【发布时间】: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


    【解决方案1】:

    BouncyCastle 的公钥参数 p,q,g,y 的字节数组具有前导 0,而 .Net 则没有。所以用前导零填充,以使 BouncyCastle 在 .Net 关键参数上工作。

    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 bouncyCastleP(p.Length) As Byte
        Dim bouncyCastleQ(q.Length) As Byte
        Dim bouncyCastleG(g.Length) As Byte
        Dim bouncyCastleY(y.Length) As Byte
        Array.ConstrainedCopy(p, 0, bouncyCastleP, 1, p.Length)
        Array.ConstrainedCopy(q, 0, bouncyCastleQ, 1, q.Length)
        Array.ConstrainedCopy(g, 0, bouncyCastleG, 1, g.Length)
        Array.ConstrainedCopy(y, 0, bouncyCastleY, 1, y.Length)
        Dim pBigInt As New Org.BouncyCastle.Math.BigInteger(bouncyCastleP)
        Dim qBigInt As New Org.BouncyCastle.Math.BigInteger(bouncyCastleQ)
        Dim gBigInt As New Org.BouncyCastle.Math.BigInteger(bouncyCastleG)
        Dim yBigInt As New Org.BouncyCastle.Math.BigInteger(bouncyCastleY)
        Dim bouncyParameters As New Org.BouncyCastle.Crypto.Parameters.DsaParameters(pBigInt, qBigInt, gBigInt)
        Dim bouncyPublicKey As New Org.BouncyCastle.Crypto.Parameters.DsaPublicKeyParameters(yBigInt, bouncyParameters)
    
        'Signature
        Dim bouncyDSA As New Org.BouncyCastle.Crypto.Signers.DsaSigner
        bouncyDSA.Init(False, bouncyPublicKey)
        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
    

    或者,您可以使用Org.BouncyCastle.Security.DotNetUtilities.GetDsaPublicKey() 将 .Net 公钥参数转换为 BouncyCastle 的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-17
      • 2011-05-28
      • 2013-10-10
      • 1970-01-01
      • 2011-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多