Exemple #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.");
            }
        }
Exemple #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);
            }
        }
Exemple #3
0
        public void TestUnicodeString(Symmetric_AES.KeySize keySize)
        {
            var    key   = Symmetric_AES.GenerateRandomKey(keySize);
            string input = Convert.ToBase64String(SecureRandomizer.GetRandomBytes(100)) + "\u01e2\u01f0\u020e\u0229";

            byte[] encrypted;
            string decrypted;

            using (var algorithm = new Symmetric_AES_CBC())
            {
                encrypted = algorithm.EncryptString(input, key);
            }
            using (var algorithm = new Symmetric_AES_CBC())
            {
                Assert.IsTrue(encrypted.Length > 100, "encrypted.Length");
                decrypted = algorithm.DecryptBytes(encrypted, key).AsString();
            }
            Assert.AreEqual(input, decrypted, string.Format("{0} | {1}", input, decrypted));
        }