public void Test_Decrypt_WithAdditionalData_Success() { using (var key = XChaChaKey.Generate()) { var aeadCipher = new XChaChaAeadCipher(); var nonce = XChaChaNonce.Generate(); var additionalData = Encoding.UTF8.GetBytes(DateTime.Now.ToString()); const int messageLength = 1024 * 1024; var message = RandomBytesGenerator.NextBytes(messageLength); var ciphertext = aeadCipher.Encrypt(message, key, nonce, additionalData); var result = aeadCipher.Decrypt(ciphertext, key, nonce, additionalData); Assert.Equal(message, result); } }
public void Test_Decrypt_WithInvalidAdditionalData_Fails() { using (var key = XChaChaKey.Generate()) { var aeadCipher = new XChaChaAeadCipher(); var nonce = XChaChaNonce.Generate().ToArray(); var additionalData = Encoding.UTF8.GetBytes(DateTime.Now.ToString()); const int messageLength = 1024 * 1024; var message = RandomBytesGenerator.NextBytes(messageLength); var ciphertext = aeadCipher.Encrypt(message, key, new XChaChaNonce(nonce), additionalData); var invalidAdditionalData = Encoding.UTF8.GetBytes("banana"); Action action = () => { aeadCipher.Decrypt(ciphertext, key, new XChaChaNonce(nonce), invalidAdditionalData); }; var exception = Assert.Throws <CryptographicException>(action); Assert.Equal("decryption failed", exception.Message); } }