Question

I'm testing the DSACryptoServiceProvider using the parameters given in FIPS PUB 186.

When I attempt to import the public key parameters, I receive a 'Bad Data' error:

Dim pString As String = "08df2a494492276aa3d25759bb06869cbeac0d83afb8d0cf7cbb8324f0d7882e5d0762fc5b7210eafc2e9adac32ab7aac49693dfbf83724c2ec0736ee31c80291"
Dim qString As String = "0c773218c737ec8ee993b4f2ded30f48edace915f"
Dim gString As String = "0626d027839ea0a13413163a55b4cb500299d5522956cefcb3bff10f399ce2c2e71cb9de5fa24babf58e5b79521925c9cc42e9f6f464b088cc572af53e6d78802"
Dim yString As String = "019131871d75b1612a819f29d78d1b0d7346f7aa77bb62a859bfd6c5675da9d212d3a36ef1672ef660b8c7c255cc0ec74858fba33f44c06699630a76b030ee333"
Dim seedString As String = "0d5014e4b60ef2ba8b6211b4062ba3224e0427dd3"

Dim p As BigInteger = GetBigInteger(pString)
Dim q As BigInteger = GetBigInteger(qString)
Dim g As BigInteger = GetBigInteger(gString)
Dim y As BigInteger = GetBigInteger(yString)
Dim seed As BigInteger = GetBigInteger(seedString)

Dim dp As New DSAParameters
dp.P = GetBytes(p,64)
dp.Q = GetBytes(q,20)
dp.G = GetBytes(g,64)
dp.Y = GetBytes(y,64)
dp.Counter = 105
dp.Seed = GetBytes(seed,20)

Using dcsp As New DSACryptoServiceProvider(512)
  dcsp.ImportParameters(dp) ' results in 'Bad Data'
End Using

.

Function GetBigInteger(hexString As String) As BigInteger
  Return BigInteger.Parse(hexString, NumberStyles.HexNumber)
End Function

.

Function GetBytes(bi As BigInteger,length As Int32) As Byte()
  Dim bytes() As Byte = bi.ToByteArray
  If bytes.Length > length Then
    Dim result(length - 1) As Byte
    Array.Copy(bytes,result,length)
    Return result
  Else
    Return bytes
  End If
End Function

The error message isn't helpful. I can't see anything wrong and I've tried everything I can think of.

Was it helpful?

Solution

It turns out the arrays need to be stored in reverse order:

Function GetBytes(bi As BigInteger,length As Int32) As Byte()
  Dim bytes() As Byte = bi.ToByteArray
  If bytes.Length > length Then
    Dim result(length - 1) As Byte
    Array.Copy(bytes,result,length)
    Array.Reverse(result)
    Return result
  Else
    Array.Reverse(bytes)
    Return bytes
  End If
End Function
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top