public void TestInvalidHmacInCopyEncryptedTo()
        {
            Passphrase passphrase = new Passphrase("a");

            using (V1AxCryptDocument document = new V1AxCryptDocument())
            {
                bool keyIsOk = document.Load(passphrase, new V1Aes128CryptoFactory().CryptoId, FakeDataStore.ExpandableMemoryStream(Resources.helloworld_key_a_txt));
                Assert.That(keyIsOk, Is.True, "The passphrase provided is correct and should work!");

                Passphrase newPassphrase = new Passphrase("b");
                using (Stream changedStream = new MemoryStream())
                {
                    V1DocumentHeaders outputDocumentHeaders = new V1DocumentHeaders(document.DocumentHeaders);
                    outputDocumentHeaders.RewrapMasterKey(new V1DerivedKey(newPassphrase), 15);

                    byte[] modifiedHmacBytes = document.DocumentHeaders.Headers.Hmac.GetBytes();
                    modifiedHmacBytes[0] += 1;
                    document.DocumentHeaders.Headers.Hmac = new V1Hmac(modifiedHmacBytes);
                    Assert.Throws <Axantum.AxCrypt.Core.Runtime.IncorrectDataException>(() =>
                    {
                        document.CopyEncryptedTo(outputDocumentHeaders, changedStream);
                    });
                }
            }
        }
        public void TestChangePassphraseForSimpleFile()
        {
            Passphrase passphrase = new Passphrase("a");

            using (V1AxCryptDocument document = new V1AxCryptDocument())
            {
                bool keyIsOk = document.Load(passphrase, new V1Aes128CryptoFactory().CryptoId, FakeDataStore.ExpandableMemoryStream(Resources.helloworld_key_a_txt));
                Assert.That(keyIsOk, Is.True, "The passphrase provided is correct and should work!");

                Passphrase newPassphrase = new Passphrase("b");
                using (Stream changedStream = new MemoryStream())
                {
                    V1DocumentHeaders outputDocumentHeaders = new V1DocumentHeaders(document.DocumentHeaders);
                    outputDocumentHeaders.RewrapMasterKey(new V1DerivedKey(newPassphrase), 35);

                    document.CopyEncryptedTo(outputDocumentHeaders, changedStream);
                    changedStream.Position = 0;
                    using (V1AxCryptDocument changedDocument = new V1AxCryptDocument())
                    {
                        bool changedKeyIsOk = changedDocument.Load(newPassphrase, new V1Aes128CryptoFactory().CryptoId, changedStream);
                        Assert.That(changedKeyIsOk, Is.True, "The changed passphrase provided is correct and should work!");

                        using (MemoryStream plaintextStream = new MemoryStream())
                        {
                            changedDocument.DecryptTo(plaintextStream);
                            Assert.That(Encoding.ASCII.GetString(plaintextStream.GetBuffer(), 0, (int)plaintextStream.Length), Is.EqualTo("HelloWorld"), "Unexpected result of decryption.");
                            Assert.That(changedDocument.DocumentHeaders.PlaintextLength, Is.EqualTo(10), "'HelloWorld' should be 10 bytes uncompressed plaintext.");
                        }
                    }
                }
            }
        }
        public void TestDecryptOfTooNewFileVersion()
        {
            DateTime creationTimeUtc   = new DateTime(2012, 1, 1, 1, 2, 3, DateTimeKind.Utc);
            DateTime lastAccessTimeUtc = creationTimeUtc + new TimeSpan(1, 0, 0);
            DateTime lastWriteTimeUtc  = creationTimeUtc + new TimeSpan(2, 0, 0);;

            using (Stream inputStream = FakeDataStore.ExpandableMemoryStream(Encoding.UTF8.GetBytes("AxCrypt is Great!")))
            {
                using (Stream outputStream = new MemoryStream())
                {
                    Passphrase passphrase = new Passphrase("a");
                    using (V1AxCryptDocument document = new V1AxCryptDocument(passphrase, 101))
                    {
                        document.FileName          = "MyFile.txt";
                        document.CreationTimeUtc   = creationTimeUtc;
                        document.LastAccessTimeUtc = lastAccessTimeUtc;
                        document.LastWriteTimeUtc  = lastWriteTimeUtc;
                        VersionHeaderBlock versionHeaderBlock = document.DocumentHeaders.VersionHeaderBlock;
                        versionHeaderBlock.FileVersionMajor = (byte)(versionHeaderBlock.FileVersionMajor + 1);
                        document.EncryptTo(inputStream, outputStream, AxCryptOptions.EncryptWithoutCompression);
                    }
                    outputStream.Position = 0;
                    using (V1AxCryptDocument document = new V1AxCryptDocument())
                    {
                        Assert.Throws <FileFormatException>(() => { document.Load(passphrase, new V1Aes128CryptoFactory().CryptoId, outputStream); });
                    }
                }
            }
        }
        public override IAxCryptDocument Document(Passphrase passphrase, Guid cryptoId, Headers headers)
        {
            V1AxCryptDocument v1Document = new V1AxCryptDocument(this);

            v1Document.Load(passphrase, cryptoId, headers);
            return(v1Document);
        }
        public void TestNoMagicGuidFound()
        {
            byte[]     dummy      = Encoding.ASCII.GetBytes("This is a string that generates some bytes, none of which will match the magic GUID");
            Passphrase passphrase = new Passphrase("a");

            using (V1AxCryptDocument document = new V1AxCryptDocument())
            {
                Assert.Throws <FileFormatException>(() => { document.Load(passphrase, new V1Aes128CryptoFactory().CryptoId, FakeDataStore.ExpandableMemoryStream(dummy)); }, "Calling with dummy data that does not contain a GUID.");
            }
        }
        public void TestInvalidPassphraseWithSimpleFile()
        {
            Passphrase passphrase = new Passphrase("b");

            using (V1AxCryptDocument document = new V1AxCryptDocument())
            {
                bool keyIsOk = document.Load(passphrase, new V1Aes128CryptoFactory().CryptoId, FakeDataStore.ExpandableMemoryStream(Resources.helloworld_key_a_txt));
                Assert.That(keyIsOk, Is.False, "The passphrase provided is wrong!");
            }
        }
        public void TestHmacCalculationFromSimpleFile()
        {
            Passphrase passphrase = new Passphrase("a");

            using (V1AxCryptDocument document = new V1AxCryptDocument())
            {
                bool keyIsOk = document.Load(passphrase, new V1Aes128CryptoFactory().CryptoId, FakeDataStore.ExpandableMemoryStream(Resources.helloworld_key_a_txt));
                Assert.That(keyIsOk, Is.True, "The passphrase provided is correct!");
                document.DecryptTo(Stream.Null);
            }
        }
        public void TestIsCompressedFromLargerFile()
        {
            Passphrase passphrase = new Passphrase("Å ä Ö");

            using (V1AxCryptDocument document = new V1AxCryptDocument())
            {
                bool keyIsOk = document.Load(passphrase, new V1Aes128CryptoFactory().CryptoId, FakeDataStore.ExpandableMemoryStream(Resources.david_copperfield_key__aa_ae_oe__ulu_txt));
                Assert.That(keyIsOk, Is.True, "The passphrase provided is correct!");
                bool isCompressed = document.DocumentHeaders.IsCompressed;
                Assert.That(isCompressed, Is.True, "This file should be compressed.");
            }
        }
        public void TestIsCompressedFromSimpleFile()
        {
            Passphrase passphrase = new Passphrase("a");

            using (V1AxCryptDocument document = new V1AxCryptDocument())
            {
                bool keyIsOk = document.Load(passphrase, new V1Aes128CryptoFactory().CryptoId, FakeDataStore.ExpandableMemoryStream(Resources.helloworld_key_a_txt));
                Assert.That(keyIsOk, Is.True, "The passphrase provided is correct!");
                bool isCompressed = document.DocumentHeaders.IsCompressed;
                Assert.That(isCompressed, Is.False, "This file should not be compressed.");
            }
        }
 public void TestInputStreamTooShort()
 {
     using (MemoryStream testStream = new MemoryStream())
     {
         byte[] guid = AxCrypt1Guid.GetBytes();
         testStream.Write(guid, 0, guid.Length);
         testStream.Position = 0;
         using (V1AxCryptDocument document = new V1AxCryptDocument())
         {
             Assert.Throws <FileFormatException>(() => { document.Load(Passphrase.Empty, new V1Aes128CryptoFactory().CryptoId, testStream); }, "Calling with too short a stream, only containing a GUID.");
         }
     }
 }
        public void TestHmacFromSimpleFile()
        {
            V1Hmac     expectedHmac = new V1Hmac(new byte[] { 0xF9, 0xAF, 0x2E, 0x67, 0x7D, 0xCF, 0xC9, 0xFE, 0x06, 0x4B, 0x39, 0x08, 0xE7, 0x5A, 0x87, 0x81 });
            Passphrase passphrase   = new Passphrase("a");

            using (V1AxCryptDocument document = new V1AxCryptDocument())
            {
                bool keyIsOk = document.Load(passphrase, new V1Aes128CryptoFactory().CryptoId, FakeDataStore.ExpandableMemoryStream(Resources.helloworld_key_a_txt));
                Assert.That(keyIsOk, Is.True, "The passphrase provided is correct!");
                Hmac hmac = document.DocumentHeaders.Headers.Hmac;
                Assert.That(hmac.GetBytes(), Is.EqualTo(expectedHmac.GetBytes()), "Wrong HMAC");
            }
        }
 public void TestUnicodeFileNameFromSimpleFile()
 {
     using (Stream testStream = FakeDataStore.ExpandableMemoryStream(Resources.helloworld_key_a_txt))
     {
         Passphrase passphrase = new Passphrase("a");
         using (V1AxCryptDocument document = new V1AxCryptDocument())
         {
             bool keyIsOk = document.Load(passphrase, new V1Aes128CryptoFactory().CryptoId, testStream);
             Assert.That(keyIsOk, Is.True, "The passphrase provided is correct!");
             string fileName = document.DocumentHeaders.FileName;
             Assert.That(fileName, Is.EqualTo("HelloWorld-Key-a.txt"), "Wrong file name");
         }
     }
 }
        public void TestDecryptWithKeyFilePdfFile()
        {
            Passphrase passphrase = Passphrase.Create("b", Resources.My_Key_File);

            using (V1AxCryptDocument document = new V1AxCryptDocument())
            {
                bool keyIsOk = document.Load(passphrase, new V1Aes128CryptoFactory().CryptoId, FakeDataStore.ExpandableMemoryStream(Resources._2003_05_28_PcWorld_AxCrypt__b____My_Keyfile_pdf));
                Assert.That(keyIsOk, Is.True, "The passphrase provided is correct!");
                using (MemoryStream plaintextStream = new MemoryStream())
                {
                    document.DecryptTo(plaintextStream);
                    Assert.That(plaintextStream.Length, Is.EqualTo(132498), "Wrong length of decrypted PDF");
                }
            }
        }
        public void TestFailedHmacCalculationFromSimpleFile()
        {
            Passphrase passphrase = new Passphrase("a");

            using (V1AxCryptDocument document = new V1AxCryptDocument())
            {
                bool keyIsOk = document.Load(passphrase, new V1Aes128CryptoFactory().CryptoId, FakeDataStore.ExpandableMemoryStream(Resources.helloworld_key_a_txt));
                Assert.That(keyIsOk, Is.True, "The passphrase provided is correct!");
                document.DocumentHeaders.Headers.Hmac = new V1Hmac(new byte[V1Hmac.RequiredLength]);
                Assert.Throws <Axantum.AxCrypt.Core.Runtime.IncorrectDataException>(() =>
                {
                    document.DecryptTo(Stream.Null);
                });
            }
        }
        public void TestDecryptWithKeyFileImageFile()
        {
            Passphrase passphrase = Passphrase.Create("a", Resources.My_Key_File);

            using (V1AxCryptDocument document = new V1AxCryptDocument())
            {
                bool keyIsOk = document.Load(passphrase, new V1Aes128CryptoFactory().CryptoId, FakeDataStore.ExpandableMemoryStream(Resources.Foto_2015_05_19_23_19_08__a____My_Keyfile_jpg));
                Assert.That(keyIsOk, Is.True, "The passphrase provided is correct!");
                using (MemoryStream plaintextStream = new MemoryStream())
                {
                    document.DecryptTo(plaintextStream);
                    Assert.That(plaintextStream.Length, Is.EqualTo(260053), "Wrong length of decrypted JPG");
                }
            }
        }
        public void TestDecryptUncompressedFromSimpleFile()
        {
            Passphrase passphrase = new Passphrase("a");

            using (V1AxCryptDocument document = new V1AxCryptDocument())
            {
                bool keyIsOk = document.Load(passphrase, new V1Aes128CryptoFactory().CryptoId, FakeDataStore.ExpandableMemoryStream(Resources.helloworld_key_a_txt));
                Assert.That(keyIsOk, Is.True, "The passphrase provided is correct!");
                using (MemoryStream plaintextStream = new MemoryStream())
                {
                    document.DecryptTo(plaintextStream);
                    Assert.That(Encoding.ASCII.GetString(plaintextStream.GetBuffer(), 0, (int)plaintextStream.Length), Is.EqualTo("HelloWorld"), "Unexpected result of decryption.");
                    Assert.That(document.DocumentHeaders.PlaintextLength, Is.EqualTo(10), "'HelloWorld' should be 10 bytes uncompressed plaintext.");
                }
            }
        }
        public void TestReaderNotPositionedAtData()
        {
            MemoryStream  encryptedFile = new MemoryStream(Resources.david_copperfield_key__aa_ae_oe__ulu_txt);
            Headers       headers       = new Headers();
            AxCryptReader reader        = headers.CreateReader(new LookAheadStream(encryptedFile));

            using (V1AxCryptDocument document = new V1AxCryptDocument(reader))
            {
                Passphrase key     = new Passphrase("Å ä Ö");
                bool       keyIsOk = document.Load(key, new V1Aes128CryptoFactory().CryptoId, headers);
                Assert.That(keyIsOk, Is.True);

                reader.SetStartOfData();
                Assert.Throws <InvalidOperationException>(() => document.DecryptTo(Stream.Null));
            }
        }
        public void TestDecryptWithKeyFileShortTextFile()
        {
            Passphrase passphrase = Passphrase.Create("p", Resources.My_Key_File);

            using (V1AxCryptDocument document = new V1AxCryptDocument())
            {
                bool keyIsOk = document.Load(passphrase, new V1Aes128CryptoFactory().CryptoId, FakeDataStore.ExpandableMemoryStream(Resources.Passphrase__p____My_Keyfile_txt));
                Assert.That(keyIsOk, Is.True, "The passphrase provided is correct!");
                using (MemoryStream plaintextStream = new MemoryStream())
                {
                    document.DecryptTo(plaintextStream);
                    string text = Encoding.UTF8.GetString(plaintextStream.GetBuffer(), 0, (int)plaintextStream.Length);
                    Assert.That(text, Is.EqualTo("asdfasdfasdf"), "Unexpected content of file.");
                }
            }
        }
示例#19
0
        public void TestUncompressedEncryptedDecryptAxCrypt17()
        {
            IDataStore sourceRuntimeFileInfo      = New <IDataStore>(_uncompressedAxxPath);
            IDataStore destinationRuntimeFileInfo = New <IDataStore>(Path.Combine(Path.GetDirectoryName(_uncompressedAxxPath), "Uncompressed.zip"));
            Passphrase passphrase = new Passphrase("Uncompressable");

            using (V1AxCryptDocument document = new V1AxCryptDocument())
            {
                bool isOk = document.Load(passphrase, new V1Aes128CryptoFactory().CryptoId, sourceRuntimeFileInfo.OpenRead());
                Assert.That(isOk, Is.True, "The document should load ok.");
                using (FileLock destinationFileLock = New <FileLocker>().Acquire(destinationRuntimeFileInfo))
                {
                    New <AxCryptFile>().Decrypt(document, destinationFileLock, AxCryptOptions.None, new ProgressContext());
                }
                Assert.That(document.DocumentHeaders.UncompressedLength, Is.EqualTo(0), "Since the data is not compressed, there should not be a CompressionInfo, but in 1.x there is, with value zero.");
            }
        }
        public void TestFileTimesFromSimpleFile()
        {
            Passphrase passphrase = new Passphrase("a");

            using (V1AxCryptDocument document = new V1AxCryptDocument())
            {
                bool keyIsOk = document.Load(passphrase, new V1Aes128CryptoFactory().CryptoId, FakeDataStore.ExpandableMemoryStream(Resources.helloworld_key_a_txt));
                Assert.That(keyIsOk, Is.True, "The passphrase provided is correct!");

                string creationTime = document.DocumentHeaders.CreationTimeUtc.ToString(CultureInfo.InvariantCulture);
                Assert.That(creationTime, Is.EqualTo("01/13/2012 17:17:18"), "Checking creation time.");
                string lastAccessTime = document.DocumentHeaders.LastAccessTimeUtc.ToString(CultureInfo.InvariantCulture);
                Assert.That(lastAccessTime, Is.EqualTo("01/13/2012 17:17:18"), "Checking last access time.");
                string lastWriteTime = document.DocumentHeaders.LastWriteTimeUtc.ToString(CultureInfo.InvariantCulture);
                Assert.That(lastWriteTime, Is.EqualTo("01/13/2012 17:17:45"), "Checking last modify time.");
            }
        }
 public void TestDecryptAfterFailedLoad()
 {
     using (Stream testStream = new MemoryStream())
     {
         AxCrypt1Guid.Write(testStream);
         testStream.Position = 0;
         Passphrase passphrase = new Passphrase("Å ä Ö");
         using (V1AxCryptDocument document = new V1AxCryptDocument())
         {
             Assert.Throws <FileFormatException>(() => { document.Load(passphrase, new V1Aes128CryptoFactory().CryptoId, testStream); });
             using (MemoryStream plaintextStream = new MemoryStream())
             {
                 Assert.Throws <InternalErrorException>(() => { document.DecryptTo(plaintextStream); });
             }
         }
     }
 }
        public void TestDecryptCompressedFromLegacy0B6()
        {
            Passphrase passphrase = new Passphrase("åäö");

            using (V1AxCryptDocument document = new V1AxCryptDocument())
            {
                bool keyIsOk = document.Load(passphrase, new V1Aes128CryptoFactory().CryptoId, FakeDataStore.ExpandableMemoryStream(Resources.tst_0_0b6_key__aaaeoe__medium_html));
                Assert.That(keyIsOk, Is.True, "A correct passphrase was provided, but it was not accepted.");
                Assert.That(document.DocumentHeaders.IsCompressed, Is.True, "The file is compressed.");
                Assert.That(document.DocumentHeaders.FileName, Is.EqualTo("readme.html"), "The file name should be 'readme.html'.");
                using (MemoryStream plaintextStream = new MemoryStream())
                {
                    document.DecryptTo(plaintextStream);
                    Assert.That(document.DocumentHeaders.PlaintextLength, Is.EqualTo(3736), "The compressed content should be recorded as 3736 bytes in the headers.");
                    Assert.That(plaintextStream.Length, Is.EqualTo(9528), "The file should be 9528 bytes uncompressed plaintext in actual fact.");
                }
            }
        }
        public void TestDecryptCompressedWithTruncatedFile()
        {
            Passphrase passphrase = new Passphrase("Å ä Ö");

            using (V1AxCryptDocument document = new V1AxCryptDocument())
            {
                using (MemoryStream encryptedFile = FakeDataStore.ExpandableMemoryStream(Resources.david_copperfield_key__aa_ae_oe__ulu_txt))
                {
                    encryptedFile.SetLength(encryptedFile.Length / 2);
                    encryptedFile.Position = 0;
                    bool keyIsOk = document.Load(passphrase, new V1Aes128CryptoFactory().CryptoId, encryptedFile);
                    Assert.That(keyIsOk, Is.True, "The passphrase provided is correct!");
                    using (MemoryStream plaintextStream = new MemoryStream())
                    {
                        Assert.That(() => { document.DecryptTo(plaintextStream); }, Throws.InstanceOf <Exception>());
                    }
                }
            }
        }
        public void TestDecryptCompressedFromLargerFile()
        {
            Passphrase passphrase = new Passphrase("Å ä Ö");

            using (V1AxCryptDocument document = new V1AxCryptDocument())
            {
                bool keyIsOk = document.Load(passphrase, new V1Aes128CryptoFactory().CryptoId, FakeDataStore.ExpandableMemoryStream(Resources.david_copperfield_key__aa_ae_oe__ulu_txt));
                Assert.That(keyIsOk, Is.True, "The passphrase provided is correct!");
                using (MemoryStream plaintextStream = new MemoryStream())
                {
                    document.DecryptTo(plaintextStream);
                    string text = Encoding.UTF8.GetString(plaintextStream.GetBuffer(), 0, (int)plaintextStream.Length);
                    Assert.That(text.StartsWith("The Project Gutenberg EBook of David Copperfield, by Charles Dickens"), "Unexpected start of David Copperfield.");
                    Assert.That(text.EndsWith("subscribe to our email newsletter to hear about new eBooks." + (Char)13 + (Char)10), "Unexpected end of David Copperfield.");
                    Assert.That(text.Length, Is.EqualTo(1992490), "Wrong length of full text of David Copperfield.");
                    Assert.That(document.DocumentHeaders.PlaintextLength, Is.EqualTo(795855), "Wrong expected length of compressed text of David Copperfield.");
                }
            }
        }
        public void TestDecryptCompressedWithCancel()
        {
            Passphrase passphrase = new Passphrase("Å ä Ö");

            using (V1AxCryptDocument document = new V1AxCryptDocument())
            {
                IProgressContext progress = new CancelProgressContext(new ProgressContext(new TimeSpan(0, 0, 0, 0, 100)));
                bool             keyIsOk  = document.Load(passphrase, new V1Aes128CryptoFactory().CryptoId, new ProgressStream(FakeDataStore.ExpandableMemoryStream(Resources.david_copperfield_key__aa_ae_oe__ulu_txt), progress));
                Assert.That(keyIsOk, Is.True, "The passphrase provided is correct!");
                using (MemoryStream plaintextStream = new MemoryStream())
                {
                    progress.Progressing += (object sender, ProgressEventArgs e) =>
                    {
                        progress.Cancel = true;
                    };
                    FakeRuntimeEnvironment environment = (FakeRuntimeEnvironment)OS.Current;
                    environment.CurrentTiming.CurrentTiming = new TimeSpan(0, 0, 0, 0, 100);
                    Assert.Throws <OperationCanceledException>(() => { document.DecryptTo(plaintextStream); });
                }
            }
        }
        public void TestSimpleEncryptToWithoutCompression()
        {
            DateTime creationTimeUtc   = new DateTime(2012, 1, 1, 1, 2, 3, DateTimeKind.Utc);
            DateTime lastAccessTimeUtc = creationTimeUtc + new TimeSpan(1, 0, 0);
            DateTime lastWriteTimeUtc  = creationTimeUtc + new TimeSpan(2, 0, 0);;

            using (Stream inputStream = FakeDataStore.ExpandableMemoryStream(Encoding.UTF8.GetBytes("AxCrypt is Great!")))
            {
                using (Stream outputStream = new MemoryStream())
                {
                    Passphrase passphrase = new Passphrase("a");
                    using (V1AxCryptDocument document = new V1AxCryptDocument(passphrase, 53))
                    {
                        document.DocumentHeaders.FileName          = "MyFile.txt";
                        document.DocumentHeaders.CreationTimeUtc   = creationTimeUtc;
                        document.DocumentHeaders.LastAccessTimeUtc = lastAccessTimeUtc;
                        document.DocumentHeaders.LastWriteTimeUtc  = lastWriteTimeUtc;
                        document.EncryptTo(inputStream, outputStream, AxCryptOptions.EncryptWithoutCompression);
                    }
                    outputStream.Position = 0;
                    using (V1AxCryptDocument document = new V1AxCryptDocument())
                    {
                        bool keyIsOk = document.Load(passphrase, new V1Aes128CryptoFactory().CryptoId, outputStream);
                        Assert.That(keyIsOk, Is.True, "The passphrase provided is correct!");
                        Assert.That(document.DocumentHeaders.FileName, Is.EqualTo("MyFile.txt"));
                        Assert.That(document.DocumentHeaders.CreationTimeUtc, Is.EqualTo(creationTimeUtc));
                        Assert.That(document.DocumentHeaders.LastAccessTimeUtc, Is.EqualTo(lastAccessTimeUtc));
                        Assert.That(document.DocumentHeaders.LastWriteTimeUtc, Is.EqualTo(lastWriteTimeUtc));
                        using (MemoryStream plaintextStream = new MemoryStream())
                        {
                            document.DecryptTo(plaintextStream);
                            Assert.That(document.DocumentHeaders.UncompressedLength, Is.EqualTo(-1), "'AxCrypt is Great!' should not return a value at all for uncompressed, since it was not compressed.");
                            Assert.That(document.DocumentHeaders.PlaintextLength, Is.EqualTo(17), "'AxCrypt is Great!' is 17 bytes plaintext length.");
                            Assert.That(Encoding.ASCII.GetString(plaintextStream.GetBuffer(), 0, (int)plaintextStream.Length), Is.EqualTo("AxCrypt is Great!"), "Unexpected result of decryption.");
                        }
                    }
                }
            }
        }
        public void TestDecryptUncompressedWithPaddingError()
        {
            Passphrase passphrase = new Passphrase("a");

            using (V1AxCryptDocument document = new V1AxCryptDocument())
            {
                using (MemoryStream encryptedFile = FakeDataStore.ExpandableMemoryStream((byte[])Resources.helloworld_key_a_txt.Clone()))
                {
                    encryptedFile.Seek(-1, SeekOrigin.End);
                    byte lastByte = (byte)encryptedFile.ReadByte();
                    ++lastByte;
                    encryptedFile.Seek(-1, SeekOrigin.End);
                    encryptedFile.WriteByte(lastByte);
                    encryptedFile.Position = 0;
                    bool keyIsOk = document.Load(passphrase, new V1Aes128CryptoFactory().CryptoId, encryptedFile);
                    Assert.That(keyIsOk, Is.True, "The passphrase provided is correct!");
                    using (MemoryStream plaintextStream = new MemoryStream())
                    {
                        Assert.Throws <CryptoException>(() => { document.DecryptTo(plaintextStream); });
                    }
                }
            }
        }
        public async Task TestEncryptFileWithDefaultEncryptionKey()
        {
            TypeMap.Register.New <ICryptoPolicy>(() => new LegacyCryptoPolicy());
            await Resolve.KnownIdentities.SetDefaultEncryptionIdentity(new LogOnIdentity("default"));

            FileOperationsController controller     = new FileOperationsController();
            bool queryEncryptionPassphraseWasCalled = false;

            controller.QueryEncryptionPassphrase += (object sender, FileOperationEventArgs e) =>
            {
                queryEncryptionPassphraseWasCalled = true;
            };
            string destinationPath = String.Empty;

            controller.Completed += (object sender, FileOperationEventArgs e) =>
            {
                destinationPath = e.SaveFileFullName;
            };

            FileOperationContext status = await controller.EncryptFileAsync(New <IDataStore>(_davidCopperfieldTxtPath));

            Assert.That(status.ErrorStatus, Is.EqualTo(ErrorStatus.Success), "The status should indicate success.");
            Assert.That(!queryEncryptionPassphraseWasCalled, "No query of encryption passphrase should be needed since there is a default set.");

            IDataStore destinationInfo = New <IDataStore>(destinationPath);

            Assert.That(destinationInfo.IsAvailable, "After encryption the destination file should be created.");
            using (V1AxCryptDocument document = new V1AxCryptDocument())
            {
                using (Stream stream = destinationInfo.OpenRead())
                {
                    document.Load(new Passphrase("default"), new V1Aes128CryptoFactory().CryptoId, stream);
                    Assert.That(document.PassphraseIsValid, "The encrypted document should be valid and encrypted with the default passphrase given.");
                }
            }
        }
        public void TestHmacThrowsWhenTooLittleData()
        {
            using (MemoryStream plaintext = new MemoryStream(Resources.uncompressable_zip))
            {
                MemoryStream encryptedFile = new MemoryStream();
                using (V1AxCryptDocument encryptingDocument = new V1AxCryptDocument(new Passphrase("a"), 10))
                {
                    encryptingDocument.EncryptTo(plaintext, encryptedFile, AxCryptOptions.EncryptWithoutCompression);
                }

                encryptedFile.Position = 0;
                Headers       headers = new Headers();
                AxCryptReader reader  = headers.CreateReader(new LookAheadStream(encryptedFile));
                using (V1AxCryptDocument document = new V1AxCryptDocument(reader))
                {
                    Passphrase key     = new Passphrase("a");
                    bool       keyIsOk = document.Load(key, new V1Aes128CryptoFactory().CryptoId, headers);
                    Assert.That(keyIsOk, Is.True);

                    reader.InputStream.Read(new byte[16], 0, 16);
                    Assert.Throws <InvalidOperationException>(() => document.DecryptTo(Stream.Null));
                }
            }
        }