public void TestDecryptToWithInvalidArgument() { Stream nullStream = null; using (IAxCryptDocument document = new V2AxCryptDocument()) { Assert.Throws <ArgumentNullException>(() => document.DecryptTo(nullStream)); } using (MemoryStream inputStream = new MemoryStream()) { byte[] text = Resolve.RandomGenerator.Generate(1000); inputStream.Write(text, 0, text.Length); inputStream.Position = 0; byte[] buffer = new byte[2500]; using (IAxCryptDocument document = new V2AxCryptDocument(new EncryptionParameters(new V2Aes256CryptoFactory().CryptoId, new Passphrase("passphrase")), 113)) { document.EncryptTo(inputStream, new MemoryStream(buffer), AxCryptOptions.EncryptWithCompression); using (V2AxCryptDocument decryptedDocument = new V2AxCryptDocument()) { Assert.That(decryptedDocument.Load(new Passphrase("incorrect"), new V2Aes256CryptoFactory().CryptoId, new MemoryStream(buffer)), Is.False); Assert.Throws <InternalErrorException>(() => decryptedDocument.DecryptTo(Stream.Null)); } } } }
private static void TestEncryptDecryptHelper(int length, AxCryptOptions options) { using (MemoryStream inputStream = new MemoryStream()) { byte[] text = Resolve.RandomGenerator.Generate(length); inputStream.Write(text, 0, text.Length); inputStream.Position = 0; byte[] buffer = new byte[5500 + length]; using (IAxCryptDocument document = new V2AxCryptDocument(new EncryptionParameters(new V2Aes256CryptoFactory().CryptoId, new Passphrase("passphrase")), 113)) { document.EncryptTo(inputStream, new MemoryStream(buffer), options); } using (V2AxCryptDocument decryptedDocument = new V2AxCryptDocument()) { Assert.That(decryptedDocument.Load(new Passphrase("passphrase"), new V2Aes256CryptoFactory().CryptoId, new MemoryStream(buffer)), Is.True); byte[] plain; using (MemoryStream decryptedStream = new MemoryStream()) { decryptedDocument.DecryptTo(decryptedStream); plain = decryptedStream.ToArray(); } Assert.That(plain.IsEquivalentTo(text)); } } }
public void TestDecryptToWithReaderWronglyPositioned() { using (MemoryStream inputStream = new MemoryStream()) { byte[] text = Resolve.RandomGenerator.Generate(1000); inputStream.Write(text, 0, text.Length); inputStream.Position = 0; byte[] buffer = new byte[2500]; using (IAxCryptDocument document = new V2AxCryptDocument(new EncryptionParameters(new V2Aes256CryptoFactory().CryptoId, new Passphrase("passphrase")), 113)) { document.EncryptTo(inputStream, new MemoryStream(buffer), AxCryptOptions.EncryptWithCompression); Headers headers = new Headers(); AxCryptReader reader = headers.CreateReader(new LookAheadStream(new MemoryStream(buffer))); using (V2AxCryptDocument decryptedDocument = new V2AxCryptDocument(reader)) { Assert.That(decryptedDocument.Load(new Passphrase("passphrase"), new V2Aes256CryptoFactory().CryptoId, headers), Is.True); reader.SetStartOfData(); Assert.Throws <InvalidOperationException>(() => decryptedDocument.DecryptTo(Stream.Null)); } } } }
public void TestDecryptWithInvalidHmac() { using (MemoryStream inputStream = new MemoryStream()) { byte[] text = Resolve.RandomGenerator.Generate(1000); inputStream.Write(text, 0, text.Length); inputStream.Position = 0; byte[] buffer = new byte[3000]; using (IAxCryptDocument document = new V2AxCryptDocument(new EncryptionParameters(new V2Aes256CryptoFactory().CryptoId, new Passphrase("passphrase")), 113)) { document.EncryptTo(inputStream, new MemoryStream(buffer), AxCryptOptions.EncryptWithoutCompression); buffer[1000] = (byte)(buffer[1000] + 1); using (V2AxCryptDocument decryptedDocument = new V2AxCryptDocument()) { Assert.That(decryptedDocument.Load(new Passphrase("passphrase"), new V2Aes256CryptoFactory().CryptoId, new MemoryStream(buffer)), Is.True); Assert.Throws <Axantum.AxCrypt.Core.Runtime.IncorrectDataException>(() => decryptedDocument.DecryptTo(Stream.Null)); } } } }