コード例 #1
0
        public void TestRsa()
        {
            using (var rsa = new RSACryptoServiceProvider(1024)) {
                // first, check private key conversion
                var expected     = rsa.ExportParameters(true);
                var keyParameter = rsa.AsAsymmetricKeyParameter();
                var windows      = keyParameter.AsAsymmetricAlgorithm() as RSACryptoServiceProvider;
                var actual       = windows.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();
                windows = keyPair.AsAsymmetricAlgorithm() as RSACryptoServiceProvider;
                actual  = windows.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();
                windows      = keyParameter.AsAsymmetricAlgorithm() as RSACryptoServiceProvider;
                actual       = windows.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");
            }
        }