public void TestRandomBytes(int?numBytes)
 {
     byte[] cryptoKey, authKey;
     SymmetricAuthenticated_AES_HMACSHA256.GenerateKeys(out cryptoKey, out authKey);
     byte[] input = null;
     if (numBytes.HasValue)
     {
         input = GetRandomBytes(numBytes.Value, numBytes.Value);
     }
     byte[] encrypted, decrypted;
     using (var algorithm = new SymmetricAuthenticated_AES_HMACSHA256())
     {
         encrypted = algorithm.EncryptBytes(input, cryptoKey, authKey);
     }
     using (var algorithm = new SymmetricAuthenticated_AES_HMACSHA256())
     {
         decrypted = algorithm.DecryptBytes(encrypted, cryptoKey, authKey);
     }
     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.");
     }
 }
 public void TestIncorrectKeyFails()
 {
     byte[] cryptoKey, authKey;
     SymmetricAuthenticated_AES_HMACSHA256.GenerateKeys(out cryptoKey, out authKey);
     byte[] input = GetRandomBytes(1000, 5000);
     byte[] encrypted;
     using (var algorithm = new SymmetricAuthenticated_AES_HMACSHA256())
     {
         encrypted = algorithm.EncryptBytes(input, cryptoKey, authKey);
     }
     SymmetricAuthenticated_AES_HMACSHA256.GenerateKeys(out cryptoKey, out authKey);
     using (var algorithm = new SymmetricAuthenticated_AES_HMACSHA256())
     {
         algorithm.DecryptBytes(encrypted, cryptoKey, authKey);
     }
 }
        public void TestUnicodeString()
        {
            byte[] cryptoKey, authKey;
            SymmetricAuthenticated_AES_HMACSHA256.GenerateKeys(out cryptoKey, out authKey);
            string input = Convert.ToBase64String(SecureRandomizer.GetRandomBytes(100)) + "\u01e2\u01f0\u020e\u0229";

            byte[] encrypted;
            string decrypted;

            using (var algorithm = new SymmetricAuthenticated_AES_HMACSHA256())
            {
                encrypted = algorithm.EncryptString(input, cryptoKey, authKey);
            }
            using (var algorithm = new SymmetricAuthenticated_AES_HMACSHA256())
            {
                Assert.IsTrue(encrypted.Length > 100, "encrypted.Length");
                decrypted = algorithm.DecryptBytes(encrypted, cryptoKey, authKey).AsString();
            }
            Assert.AreEqual(input, decrypted, string.Format("{0} | {1}", input, decrypted));
        }
        public void TestInvalidAuthentication()
        {
            byte[] cryptoKey, authKey;
            SymmetricAuthenticated_AES_HMACSHA256.GenerateKeys(out cryptoKey, out authKey);
            byte[] input = GetRandomBytes(500, 1000);
            byte[] encrypted, decrypted;
            using (var algorithm = new SymmetricAuthenticated_AES_HMACSHA256())
            {
                encrypted = algorithm.EncryptBytes(input, cryptoKey, authKey);
            }

            // Change the last byte in the file, which is the
            encrypted[encrypted.GetUpperBound(0)] = (byte)(~encrypted[encrypted.GetUpperBound(0)]);

            using (var algorithm = new SymmetricAuthenticated_AES_HMACSHA256())
            {
                decrypted = algorithm.DecryptBytes(encrypted, cryptoKey, authKey);
            }

            Assert.IsTrue(decrypted.SequenceEqual(input), "Decrypted does not match original.");
        }