Example #1
0
        public void AsymmetricEncryptDataTest()
        {
            Asymmetric asymmetric = new Asymmetric();

            // Short data test
            byte[] data = new byte[] { 7, 12, 69, 47, 249 };

            byte[] encrypted = asymmetric.EncryptData(data, mainPublicKey);
            byte[] actual = asymmetric.DecryptData(encrypted, mainPrivateKey);
            AssertBytesAreEqual(data, actual);

            // Longer data test
            string msg =
                "Implementing the IDisposable interface can be the source of great " +
                "confusion amongst many programmers. Whenever possible, I use the following " +
                "pattern when implementing it. So far it covers most of the cases I have needed " +
                "but for a more extensive discussion see this excellent article by Scott Dorman: " +
                "www.codeproject.com/KB/cs/idisposable.aspx";
            data = Encoding.Unicode.GetBytes(msg);
            encrypted = asymmetric.EncryptData(data, mainPublicKey);
            actual = asymmetric.DecryptData(encrypted, mainPrivateKey);
            AssertBytesAreEqual(data, actual);

            // Null argument
            data = null;
            try
            {
                encrypted = asymmetric.EncryptData(data, mainPublicKey);
                Assert.Fail("Expected exception");
            }
            catch (ArgumentNullException)
            {
            }

            // Zero data length
            data = new byte[0];
            encrypted = asymmetric.EncryptData(data, mainPublicKey);
            actual = asymmetric.DecryptData(encrypted, mainPrivateKey);
            AssertBytesAreEqual(data, actual);

            // Single byte
            data = new byte[] { 7 };
            encrypted = asymmetric.EncryptData(data, mainPublicKey);
            actual = asymmetric.DecryptData(encrypted, mainPrivateKey);
            AssertBytesAreEqual(data, actual);

            // Long data test
            data = new byte[2 * 1024 * 1024];
            Random random = new Random(71269);
            random.NextBytes(data);
            encrypted = asymmetric.EncryptData(data, mainPublicKey);
            actual = asymmetric.DecryptData(encrypted, mainPrivateKey);
            AssertBytesAreEqual(data, actual);
        }