I'm trying to code the Secure Remote Password protocol (RFC 2945). I wasn't sure if I was getting the correct values at each stage so I did some searching and came across RFC 5054 which supplies some test vectors. I believe my problem is coming from the conversions between hex string, integers, bytes, and BigIntegers, however I just can't seem to track it down. Below is just one parameter that I need to calculate, but since all the other parameters are calculated in a similar way, if I can get this one working, all the rest should be fine.
N - Large 1,024 bit safe prime number. The hex value is:
A - Public value I'm trying to computer. A = g^a % N
Obviously these are large numbers to be working with, so I'm using the BigInteger class from the System.Numeric namespace in VB.NET 4. I'm tried many different ways to get this to work. Below is my latest incarnation of the code trying to compute A:
Dim strN AsString = "EEAF0AB9ADB38DD69C33F80AFA8FC5E86072618775FF3C0B9EA2314C" & _
"9C256576D674DF7496EA81D3383B4813D692C6E0E0D5D8E250B98BE4" & _
"8E495C1D6089DAD15DC7D7B46154D6B6CE8EF4AD69B15D4982559B29" & _
"7BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA9A" & _
"FD5138FE8376435B9FC61D2FC0EB06E3"Dim N As BigInteger = BigInteger.Parse(strN, Globalization.NumberStyles.HexNumber)
Dim g AsNew BigInteger(2)
Dim private_a_hex AsString = "60975527035CF2AD1989806F0407210BC81EDC04E2762A56AFD529DDDA2D4393"Dim private_a As BigInteger = BigInteger.Parse(private_a_hex, Globalization.NumberStyles.HexNumber)
Dim public_A As BigInteger = BigInteger.ModPow(g, private_a, N)
Dim public_A_hex AsString = public_A.ToString("X")
When I run the above code public_A_hex (which is hold the value of A), I'm getting the following hex string:
I'm thinking the error is coming from the endianness of the byte arrays/hex strings; I've tried reversing arrays before and after each conversion with still no luck. I don't know where to go from here at this point as I've been working on this for a little over 2 days.
I know that there are some libraries out there, but at this point I don't want to use them. Since the values don't work out, and I can't figure out why, I would like to know what I'm doing wrong and improve my knowledge in the process.
Any help or guidance would be greatly appreciated.
as interpreted with E3 being the lowest byte and EE the highest, is not a prime number. It's divisible by 2609.
That same number interpreted as EE being the low byte and 3E the high byte (ie the string-reverse), is not an prime either, rather trivially, since E is even.
I have tried other arrangements, but I have yet to find one that's actually prime.
Chances are that the other numbers are using some weird order as well.
Thanks for the responese however, after burning through the night and checking the code hundreds of time, I found the issue. It turned out that the number was being interpreted as a negative number by the BigInteger class. Once I added a 0 to the beginning of the prime number the code worked and matched up with all the test vector's outputs.