Exemple #1
0
        static void AssertRSA(RSA rsa)
        {
            // first, check private key conversion
            var expected            = rsa.ExportParameters(true);
            var keyParameter        = rsa.AsAsymmetricKeyParameter();
            var asymmetricAlgorithm = keyParameter.AsAsymmetricAlgorithm() as RSA;
            var actual = asymmetricAlgorithm.ExportParameters(true);

            AssertAreEqual(expected.D, actual.D, "D");
            AssertAreEqual(expected.DP, actual.DP, "DP");
            AssertAreEqual(expected.DQ, actual.DQ, "DQ");
            AssertAreEqual(expected.P, actual.P, "P");
            AssertAreEqual(expected.Q, actual.Q, "Q");
            AssertAreEqual(expected.Exponent, actual.Exponent, "Exponent");
            AssertAreEqual(expected.InverseQ, actual.InverseQ, "InverseQ");
            AssertAreEqual(expected.Modulus, actual.Modulus, "Modulus");

            // test AsymmetricCipherKeyPair conversion
            var keyPair = rsa.AsAsymmetricCipherKeyPair();

            asymmetricAlgorithm = keyPair.AsAsymmetricAlgorithm() as RSA;
            actual = asymmetricAlgorithm.ExportParameters(true);

            AssertAreEqual(expected.D, actual.D, "D");
            AssertAreEqual(expected.DP, actual.DP, "DP");
            AssertAreEqual(expected.DQ, actual.DQ, "DQ");
            AssertAreEqual(expected.P, actual.P, "P");
            AssertAreEqual(expected.Q, actual.Q, "Q");
            AssertAreEqual(expected.Exponent, actual.Exponent, "Exponent");
            AssertAreEqual(expected.InverseQ, actual.InverseQ, "InverseQ");
            AssertAreEqual(expected.Modulus, actual.Modulus, "Modulus");

            // test public key conversion
            expected = rsa.ExportParameters(false);
            var pubrsa = new RSACryptoServiceProvider();

            pubrsa.ImportParameters(expected);

            keyParameter        = pubrsa.AsAsymmetricKeyParameter();
            asymmetricAlgorithm = keyParameter.AsAsymmetricAlgorithm() as RSA;
            actual = asymmetricAlgorithm.ExportParameters(false);

            AssertAreEqual(expected.D, actual.D, "D");
            AssertAreEqual(expected.DP, actual.DP, "DP");
            AssertAreEqual(expected.DQ, actual.DQ, "DQ");
            AssertAreEqual(expected.P, actual.P, "P");
            AssertAreEqual(expected.Q, actual.Q, "Q");
            AssertAreEqual(expected.Exponent, actual.Exponent, "Exponent");
            AssertAreEqual(expected.InverseQ, actual.InverseQ, "InverseQ");
            AssertAreEqual(expected.Modulus, actual.Modulus, "Modulus");
        }