question

HuangShan avatar image
0 Votes"
HuangShan asked DanielZhang-MSFT edited

RSACryptoServiceProvider CheckValidate

I use RSACryptoServiceProvider to creat publicKey and privateKey.
but when I validate them, the return is false.



         private BigInteger getUnsignedBigInteger(byte[] b)
         {
             byte[] temp = new byte[b.Length + 1];
             Array.Copy(b, temp, b.Length);
             BigInteger ret = new BigInteger(temp);
             return ret;
         }
         public bool CheckKeyValidate(RSAParameters pp)
         {
             //(d*e)mod ((p-1)*(q-1))==1
             BigInteger d = getUnsignedBigInteger(pp.D);
             BigInteger e = getUnsignedBigInteger(pp.Exponent);
             BigInteger p = getUnsignedBigInteger(pp.P);
             BigInteger q = getUnsignedBigInteger(pp.Q);
    
             BigInteger m = BigInteger.ModPow((d*e), 1, ((p - 1) * (q - 1)));
             BigInteger X = d * e;
             BigInteger Y = (p - 1) * (q - 1);
    
             if (m==1)
             {
                 return true;
             }
             return false;
         }
    
         private void button1_Click(object sender, EventArgs e)
         {
             using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
             {
                 RSAParameters p = rsa.ExportParameters(true);
                 bool ret = CheckKeyValidate(p);
             }
         }



windows-formsdotnet-runtime
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered DanielZhang-MSFT edited

Hi HuangShan,
As this thread pointed that the Exponent field is really the public exponent for a public key.
So you can't use "e" to validate the privateKey.
And the offical document also said, in all cases, if anyone can derive the parameters, the key that you transmit becomes useless.
Best Regards,
Daniel Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.