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 TestSameInputProducesDifferentOutput() { byte[] cryptoKey, authKey; SymmetricAuthenticated_AES_HMACSHA256.GenerateKeys(out cryptoKey, out authKey); for (int counter = 0; counter < 10; ++counter) { var encryptedAsBase64 = new HashSet <string>(); using (var algorithm = new SymmetricAuthenticated_AES_HMACSHA256()) { var inputBytes = GetRandomBytes(1024, 4096); for (int i = 0; i < 100; ++i) { encryptedAsBase64.Add(Convert.ToBase64String(algorithm.EncryptBytes(inputBytes, cryptoKey, authKey))); } } Assert.AreEqual(100, encryptedAsBase64.Count, "Should be 100 distinct values"); } }
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."); }