Search code examples
c#cryptographyprimesrsacryptoserviceprovider

How to extract the prime numbers generated by RSACryptoServiceProvider?


The below code is attempting to get the two RSA prime numbers generated by RSACryptoServiceProvider. I'm testing primeq for primality and it always turns up to be non prime. What am I doing wrong here ?

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(384);
var p = rsa.ExportParameters(true);
var primeq = new BigInteger(p.Q);

Solution

  • I struggled with this problem myself. I have found that there are in fact two issues:

    1. First, BigInteger uses different endianess from the RSACryptoServiceProvider
    2. BigInteger is signed and uses two's compliment notation, whereas RSA keys are unsigned integers.

    To solve this I would modify your code as follows:

    RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(384);
    var p = rsa.ExportParameters(true);
    var primeq = new BigInteger(p.Q.Reverse().Concat(new Byte[1]).ToArray());
    

    This was tested with the following code:

    using (var rsa = new RSACryptoServiceProvider()) {
        var a = rsa.ExportParameters(true);
        BigInteger p = new BigInteger(a.P.Reverse().Concat(new byte[1]).ToArray());
        BigInteger q = new BigInteger(a.Q.Reverse().Concat(new byte[1]).ToArray());
        BigInteger n = new BigInteger(a.Modulus.Reverse().Concat(new byte[1]).ToArray());
        Console.WriteLine(p * q == n);
    }