I am getting this error: System.OverflowException: 'The value is not a number.' I was under the impression that big integer could store any sized value (500 ^ 500 in this case), so I don't understand why this is happening.
public int decode(int code)
{
int totient = (p - 1) * (q - 1);
int d = modInverse(e, totient);
int pq = p * q;
BigInteger decodedMessage = new BigInteger(Math.Pow(code, d) % pq);
return (int)decodedMessage;
}
Well,
Math.Pow(code, d) % pq
is not a BigInteger, it's an expression of typedouble
. Converting the result to a BigInteger won't have an effect until the computation is complete (and has overflowed).Math.Pow
can easily overflow toDouble.PositiveInfinity
with large numbers, andDouble.PositiveInfinity % someNumber
yieldsDouble.NaN
. Callingnew BigInteger(Double.NaN)
yields the error you have described.You need to do the computation in BigInteger. Fortunately, there's a method for exactly that purpose (BigInteger.ModPow):
(BigInteger.ModPow requires BigInteger parameters, but there are implicit conversions from int to BigInteger.)