Example #1
0
        private void ValidatePrivateKey(bool showWarning = true)
        {
            try
            {
                var formatting = new string[9]
                {
                    "<RSAKeyValue><Modulus>",
                    "</Modulus><Exponent>",
                    "</Exponent><P>",
                    "</P><Q>",
                    "</Q><DP>",
                    "</DP><DQ>",
                    "</DQ><InverseQ>",
                    "</InverseQ><D>",
                    "</D></RSAKeyValue>"
                };

                var readKey = ReadXMLKey(PrivateKey, 8, false, formatting, showWarning);

                if (readKey == null)
                {
                    isPrivateKeyValid = false;

                    return;
                }

                bool isValidated = false;

                BigInteger n    = new BigInteger(Convert.FromBase64String(readKey[0]));
                int        e    = BitConverter.ToInt32(Convert.FromBase64String(readKey[1]), 0);
                BigInteger p    = new BigInteger(Convert.FromBase64String(readKey[2]));
                BigInteger q    = new BigInteger(Convert.FromBase64String(readKey[3]));
                BigInteger dp   = new BigInteger(Convert.FromBase64String(readKey[4]));
                BigInteger dq   = new BigInteger(Convert.FromBase64String(readKey[5]));
                BigInteger invQ = new BigInteger(Convert.FromBase64String(readKey[6]));
                BigInteger d    = new BigInteger(Convert.FromBase64String(readKey[7]));

                if (e > 1 && e % 2 != 0)
                {
                    if (KeyGenerator.IsPrime(p, 40) && BigInteger.GreatestCommonDivisor(p - 1, new BigInteger(e)) == 1 && KeyGenerator.IsPrime(q, 40) && BigInteger.GreatestCommonDivisor(q - 1, new BigInteger(e)) == 1)
                    {
                        if (p * q == n)
                        {
                            if (KeyGenerator.CalculateD(e, (p - 1) * (q - 1)) == d)
                            {
                            }
                        }
                    }
                }

                if (isValidated)
                {
                    SetKey(e, n, p, q, dp, dq, invQ, d);
                }
                else
                {
                    if (isPublicKeyValid)
                    {
                        SetKey(Exponent, Modulus, 0, 0, 0, 0, 0, 0);
                    }
                    else
                    {
                        SetKey(0, 0, 0, 0, 0, 0, 0, 0);
                    }
                }

                isPrivateKeyValid = isValidated;
                EnableKeyValidation(false, true, isPrivateKeyValid);
            }
            catch (Exception ex)
            {
                if (showWarning)
                {
                    MessageBox.Show("Impossível validar chave. " + ex.Message);
                }

                isPrivateKeyValid = false;
            }
        }