Search code examples
c#cryptographykeydiffie-hellman

In C#, how do I store large prime number?


Good day everyone,

I am attempting to implement the Diffie-Hellman key exchange protocol in C#. Before anything, this is a test project. I am aware that I should NOT use it for any real application. I am just doing this to learn a bit more about it.

Now, according to this RFC (3526), I need to use the prime number P=2^6144 - 2^6080 - 1 + 2^64 * { [2^6014 pi] + 929484 } and the generator G=2.

https://datatracker.ietf.org/doc/html/rfc3526#page-6

But the prime number is a way too big ! Even for BigInteger !

How do I proceed now ?

EDIT:

I assume it's too large because this piece of code return a negative value :

string hexString = "...";
BigInteger number = BigInteger.Parse(hexString, NumberStyles.AllowHexSpecifier);
Console.WriteLine("Converted 0x{0}\nto\n{1}.", hexString, number);

https://dotnetfiddle.net/Ffr7Ip


Solution

  • BigInteger.Parse converts the hex string to binary. If the binary number start with a 1, that will mean the number is signed. That is why it gives you a negative number. As @canton7 said, just prepend a 0 and you will be good to go.