1

I need to implement DSA without any libaries (except key generation) in C# as part of my homework. I managed to generate the public and private key but when i try to get the parameters like G or Y and convert them into a BigInteger i sometimes get negative parameters. The parameters itself are positive in the byte array! They just turn into negative ones when i convert them into BigInteger. Here is the way i try it:

DSAParameters keys;          
DSACryptoServiceProvider DSAalg = new DSACryptoServiceProvider();
keys = DSAalg.ExportParameters(true);
BigInteger G = new BigInteger(keys.G);
Console.WriteLine("G is {0} \n", G);

Any ideas how i could solve that?

Ɖiamond ǤeezeƦ
  • 3,223
  • 3
  • 28
  • 40
laphory
  • 13
  • 1
  • 5
  • Most significant bytes above 127 are seen as negative unless preceded by a 00 byte. That might make some of your results negative. See the docs to 'ToByteArray'. – Rudy Velthuis Mar 01 '16 at 22:15

1 Answers1

1

As per the Microsoft Documentation the BigInteger constructor expects the array to be in little-endian order (worth bearing in mind if you expect the resulting BigInteger to have a particular value).

The remarks for the BigInteger constructor state that you can make sure any BigInteger created from a byte[] is unsigned if you append a 00 byte to the end of the array before calling the constructor. In your case this would be something like:

DSAParameters keys;
var DSAalg = new DSACryptoServiceProvider();
keys = DSAalg.ExportParameters(true);
byte[] bytes = keys.G;
if ((bytes[bytes.Length - 1] & 0x80) > 0)
{
    var temp = new byte[bytes.Length];
    Array.Copy(bytes, temp, bytes.Length);
    bytes = new byte[temp.Length + 1];
    Array.Copy(temp, bytes, temp.Length);
}
Console.WriteLine("G is {0} \n", new BigInteger(bytes));
Ɖiamond ǤeezeƦ
  • 3,223
  • 3
  • 28
  • 40