RSACryptoServiceProvider CheckValidate

Huang, Shan/黄 山 1 Reputation point
2021-04-07T01:14:34.877+00:00

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 Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,839 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,126 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,621 Reputation points
    2021-04-07T07:52:43.887+00:00

    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.

    0 comments No comments