Пример #1
0
        public void TestRandomBytes(Symmetric_AES.KeySize keySize, int?numBytes)
        {
            var key = Symmetric_AES.GenerateRandomKey(keySize);

            byte[] input = null;
            if (numBytes.HasValue)
            {
                input = GetRandomBytes(numBytes.Value, numBytes.Value);
            }
            byte[] encrypted, decrypted;
            using (var algorithm = new Symmetric_AES_CBC())
            {
                encrypted = algorithm.EncryptBytes(input, key);
            }
            using (var algorithm = new Symmetric_AES_CBC())
            {
                decrypted = algorithm.DecryptBytes(encrypted, key);
            }
            if (input == null)
            {
                Assert.IsNull(encrypted, "encrypted");
                Assert.IsNull(decrypted, "decrypted");
            }
            else
            {
                Assert.IsTrue(encrypted.Length >= numBytes, "encrypted.Length");
                Assert.IsTrue(decrypted.SequenceEqual(input), "Decrypted does not match original.");
            }
        }
Пример #2
0
        public void TestIncorrectKeyFails(Symmetric_AES.KeySize keySize)
        {
            var key = Symmetric_AES.GenerateRandomKey(keySize);

            byte[] input = GetRandomBytes(1000, 5000);
            byte[] encrypted;
            using (var algorithm = new Symmetric_AES_CBC())
            {
                encrypted = algorithm.EncryptBytes(input, key);
            }
            key = Symmetric_AES.GenerateRandomKey(keySize);
            using (var algorithm = new Symmetric_AES_CBC())
            {
                algorithm.DecryptBytes(encrypted, key);
            }
        }
Пример #3
0
        public void TestSameInputProducesDifferentOutput(Symmetric_AES.KeySize keySize)
        {
            var key = Symmetric_AES.GenerateRandomKey(keySize);

            for (int counter = 0; counter < 10; ++counter)
            {
                var encryptedAsBase64 = new HashSet <string>();
                using (var algorithm = new Symmetric_AES_CBC())
                {
                    var inputBytes = GetRandomBytes(1024, 4096);
                    for (int i = 0; i < 100; ++i)
                    {
                        encryptedAsBase64.Add(Convert.ToBase64String(algorithm.EncryptBytes(inputBytes, key)));
                    }
                }
                Assert.AreEqual(100, encryptedAsBase64.Count, "Should be 100 distinct values");
            }
        }