コード例 #1
0
        public void AsymmetricCrypto_RsaEncryption()
        {
            string privateKey;
            string publicKey;

            byte[] plainText = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };
            byte[] encrypted;
            byte[] decrypted;

            try
            {
                privateKey = AsymmetricCrypto.CreatePrivateKey(CryptoAlgorithm.RSA, 1024);
                publicKey  = AsymmetricCrypto.GetPublicKey(CryptoAlgorithm.RSA, privateKey);

                encrypted = AsymmetricCrypto.Encrypt(CryptoAlgorithm.RSA, publicKey, plainText);
                CollectionAssert.AreNotEqual(encrypted, plainText);
                decrypted = AsymmetricCrypto.Decrypt(CryptoAlgorithm.RSA, privateKey, encrypted);
                CollectionAssert.AreEqual(plainText, decrypted);

                encrypted = AsymmetricCrypto.Encrypt(CryptoAlgorithm.RSA, privateKey, plainText);
                Assert.AreNotEqual(encrypted, plainText);
                decrypted = AsymmetricCrypto.Decrypt(CryptoAlgorithm.RSA, privateKey, encrypted);
                CollectionAssert.AreEqual(plainText, decrypted);

                AsymmetricCrypto.SaveKey(CryptoAlgorithm.RSA, KeyContainer, null, privateKey);

                encrypted = AsymmetricCrypto.Encrypt(CryptoAlgorithm.RSA, KeyContainer, plainText);
                CollectionAssert.AreNotEqual(encrypted, plainText);
                decrypted = AsymmetricCrypto.Decrypt(CryptoAlgorithm.RSA, KeyContainer, encrypted);
                CollectionAssert.AreEqual(plainText, decrypted);

                encrypted = AsymmetricCrypto.Encrypt(CryptoAlgorithm.RSA, KeyContainer, plainText);
                CollectionAssert.AreNotEqual(encrypted, plainText);
                decrypted = AsymmetricCrypto.Decrypt(CryptoAlgorithm.RSA, KeyContainer, encrypted);
                CollectionAssert.AreEqual(plainText, decrypted);
            }
            finally
            {
                AsymmetricCrypto.DeleteKey(CryptoAlgorithm.RSA, KeyContainer, null);
            }
        }
コード例 #2
0
        public void AsymmetricCrypto_RsaKeys()
        {
            string privateKey;
            string publicKey;

            try
            {
                privateKey = AsymmetricCrypto.CreatePrivateKey(CryptoAlgorithm.RSA, 1024);
                Assert.IsNotNull(privateKey);
                publicKey = AsymmetricCrypto.GetPublicKey(CryptoAlgorithm.RSA, privateKey);
                Assert.IsNotNull(publicKey);
                Assert.IsTrue(privateKey.Length > publicKey.Length);

                AsymmetricCrypto.SaveKey(CryptoAlgorithm.RSA, KeyContainer, null, privateKey);
                Assert.AreEqual(privateKey, AsymmetricCrypto.LoadPrivateKey(CryptoAlgorithm.RSA, KeyContainer, null));
                Assert.AreEqual(publicKey, AsymmetricCrypto.LoadPublicKey(CryptoAlgorithm.RSA, KeyContainer, null));
            }
            finally
            {
                AsymmetricCrypto.DeleteKey(CryptoAlgorithm.RSA, KeyContainer, null);
            }

            AsymmetricCrypto.DeleteKey(CryptoAlgorithm.RSA, KeyContainer, null);
        }