Sorry for the lack of comprehensive answer. But I can suggest what you can do.
Please see my comments to the question, all of them. In my last comment, I described my experiment following your idea, which provides a pretty strong evidence that the problem you observed is related to your wrong assumption that numeric interpretation of P and Q parameters, which are array of bytes, is the same as the interpretation used to initialize
System.Numeric.BigInteger
. I think that good chances are, the interpretation will match, for non-negative numbers, if you invert the array of byte. I don't have any proof (theoretically speaking, there can be many different interpretations), but it's unlikely that any other options could be used.
If you really want to dig into it, you need to expect the source code of
RSACryptoServiceProvider
and relevant dependencies of it. Unfortunately, Microsoft documentation is not detailed enough. (I would not probably need
BigInteger
source code.) You can access .NET FCL source code here:
Reference Source[
^].
Also, I just found one article on the topic, did not read it yet. Please see:
.NET 4 BigInteger and RSA Signature and Encryption[
^].
Generally, this class is not really intended to provide the source of prime number, but I must admit that your idea is very good, as the class provides decent performance. You only need to find out correct numeric interpretation or, better yet, the whole arithmetic of all operations performed directly on arrays of bytes.
—SA