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 TestInvalidArguments()
        {
            using (Stream inputStream = FakeDataStore.ExpandableMemoryStream(Encoding.UTF8.GetBytes("AxCrypt is Great!")))
            {
                using (Stream outputStream = new MemoryStream())
                {
                    using (V1AxCryptDocument document = new V1AxCryptDocument())
                    {
                        Assert.Throws <ArgumentNullException>(() => { document.EncryptTo(inputStream, null, AxCryptOptions.EncryptWithCompression); });
                        Assert.Throws <ArgumentNullException>(() => { document.EncryptTo(null, outputStream, AxCryptOptions.EncryptWithCompression); });
                        Assert.Throws <ArgumentException>(() => { document.EncryptTo(inputStream, new NonSeekableStream(), AxCryptOptions.EncryptWithCompression); });
                        Assert.Throws <ArgumentException>(() => { document.EncryptTo(inputStream, outputStream, AxCryptOptions.EncryptWithCompression | AxCryptOptions.EncryptWithoutCompression); });
                        Assert.Throws <ArgumentException>(() => { document.EncryptTo(inputStream, outputStream, AxCryptOptions.None); });

                        Passphrase        passphrase = new Passphrase("a");
                        V1DocumentHeaders headers    = new V1DocumentHeaders(passphrase, 13);

                        Assert.Throws <ArgumentNullException>(() => { document.CopyEncryptedTo(null, outputStream); });
                        Assert.Throws <ArgumentNullException>(() => { document.CopyEncryptedTo(headers, null); });
                        Assert.Throws <ArgumentException>(() => { document.CopyEncryptedTo(headers, new NonSeekableStream()); });
                        Assert.Throws <InternalErrorException>(() => { document.CopyEncryptedTo(headers, outputStream); });
                    }
                }
            }
        }
        public void TestDoubleDispose()
        {
            V1AxCryptDocument document = new V1AxCryptDocument();

            document.Dispose();
            document.Dispose();
        }
        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 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); });
                    }
                }
            }
        }
Ejemplo n.º 6
0
        public override IAxCryptDocument Document(Passphrase passphrase, Guid cryptoId, Headers headers)
        {
            V1AxCryptDocument v1Document = new V1AxCryptDocument(this);

            v1Document.Load(passphrase, cryptoId, headers);
            return(v1Document);
        }
Ejemplo n.º 7
0
        public void TestInvalidArguments()
        {
            IDataStore       sourceFileInfo      = New <IDataStore>(_testTextPath);
            IDataStore       destinationFileInfo = sourceFileInfo.CreateEncryptedName();
            IAxCryptDocument document            = new V1AxCryptDocument();
            IDataStore       decryptedFileInfo   = New <IDataStore>(Path.Combine(_rootPath, "decrypted test.txt"));

            IAxCryptDocument     nullDocument             = null;
            IDataStore           nullFileInfo             = null;
            LogOnIdentity        nullKey                  = null;
            ProgressContext      nullProgress             = null;
            EncryptionParameters nullEncryptionParameters = null;
            Stream nullStream = null;
            string nullString = null;
            Func <Stream, Task> nullStreamAction = null;
            FileLock            nullFileLock     = null;

            Assert.Throws <ArgumentNullException>(() => { new AxCryptFile().Encrypt(nullFileInfo, destinationFileInfo, new EncryptionParameters(new V1Aes128CryptoFactory().CryptoId, new Passphrase("axcrypt")), AxCryptOptions.EncryptWithCompression, new ProgressContext()); });
            Assert.Throws <ArgumentNullException>(() => { new AxCryptFile().Encrypt(sourceFileInfo, nullFileInfo, new EncryptionParameters(new V1Aes128CryptoFactory().CryptoId, new Passphrase("axcrypt")), AxCryptOptions.EncryptWithCompression, new ProgressContext()); });
            Assert.Throws <ArgumentNullException>(() => { new AxCryptFile().Encrypt(sourceFileInfo, destinationFileInfo, nullEncryptionParameters, AxCryptOptions.EncryptWithCompression, new ProgressContext()); });
            Assert.Throws <ArgumentNullException>(() => { new AxCryptFile().Encrypt(sourceFileInfo, destinationFileInfo, new EncryptionParameters(new V1Aes128CryptoFactory().CryptoId, new Passphrase("axcrypt")), AxCryptOptions.EncryptWithCompression, nullProgress); });

            Assert.Throws <ArgumentNullException>(() => { new AxCryptFile().Encrypt(nullFileInfo, new MemoryStream(), EncryptionParameters.Empty, AxCryptOptions.None, new ProgressContext()); });
            Assert.Throws <ArgumentNullException>(() => { new AxCryptFile().Encrypt(sourceFileInfo, nullStream, EncryptionParameters.Empty, AxCryptOptions.None, new ProgressContext()); });
            Assert.Throws <ArgumentNullException>(() => { new AxCryptFile().Encrypt(sourceFileInfo, new MemoryStream(), nullEncryptionParameters, AxCryptOptions.None, new ProgressContext()); });
            Assert.Throws <ArgumentNullException>(() => { new AxCryptFile().Encrypt(sourceFileInfo, new MemoryStream(), EncryptionParameters.Empty, AxCryptOptions.None, nullProgress); });

            using (FileLock fileInfoLock = New <FileLocker>().Acquire(New <IDataStore>(_testTextPath)))
            {
                Assert.Throws <ArgumentNullException>((TestDelegate)(() => { New <AxCryptFile>().Decrypt(nullDocument, fileInfoLock, AxCryptOptions.SetFileTimes, new ProgressContext()); }));
                Assert.Throws <ArgumentNullException>((TestDelegate)(() => { New <AxCryptFile>().Decrypt(document, nullFileLock, AxCryptOptions.SetFileTimes, new ProgressContext()); }));
                Assert.Throws <ArgumentNullException>((TestDelegate)(() => { New <AxCryptFile>().Decrypt(document, fileInfoLock, AxCryptOptions.SetFileTimes, nullProgress); }));
            }

            Assert.Throws <ArgumentNullException>((TestDelegate)(() => { New <AxCryptFile>().Decrypt(nullFileInfo, decryptedFileInfo, LogOnIdentity.Empty, AxCryptOptions.SetFileTimes, new ProgressContext()); }));
            Assert.Throws <ArgumentNullException>((TestDelegate)(() => { New <AxCryptFile>().Decrypt(sourceFileInfo, nullFileInfo, LogOnIdentity.Empty, AxCryptOptions.SetFileTimes, new ProgressContext()); }));
            Assert.Throws <ArgumentNullException>((TestDelegate)(() => { New <AxCryptFile>().Decrypt(sourceFileInfo, decryptedFileInfo, nullKey, AxCryptOptions.SetFileTimes, new ProgressContext()); }));
            Assert.Throws <ArgumentNullException>((TestDelegate)(() => { New <AxCryptFile>().Decrypt(sourceFileInfo, decryptedFileInfo, LogOnIdentity.Empty, AxCryptOptions.SetFileTimes, nullProgress); }));

            Assert.Throws <ArgumentNullException>((TestDelegate)(() => { New <AxCryptFile>().Decrypt(nullFileInfo, Path.Combine(_rootPath, "Directory"), LogOnIdentity.Empty, AxCryptOptions.SetFileTimes, new ProgressContext()); }));
            Assert.Throws <ArgumentNullException>((TestDelegate)(() => { New <AxCryptFile>().Decrypt(sourceFileInfo, nullString, LogOnIdentity.Empty, AxCryptOptions.SetFileTimes, new ProgressContext()); }));
            Assert.Throws <ArgumentNullException>((TestDelegate)(() => { New <AxCryptFile>().Decrypt(sourceFileInfo, Path.Combine(_rootPath, "Directory"), nullKey, AxCryptOptions.SetFileTimes, new ProgressContext()); }));
            Assert.Throws <ArgumentNullException>((TestDelegate)(() => { New <AxCryptFile>().Decrypt(sourceFileInfo, Path.Combine(_rootPath, "Directory"), LogOnIdentity.Empty, AxCryptOptions.SetFileTimes, nullProgress); }));

            Assert.Throws <ArgumentNullException>((TestDelegate)(() => { New <AxCryptFile>().Document(nullFileInfo, LogOnIdentity.Empty, new ProgressContext()); }));
            Assert.Throws <ArgumentNullException>((TestDelegate)(() => { New <AxCryptFile>().Document(sourceFileInfo, nullKey, new ProgressContext()); }));
            Assert.Throws <ArgumentNullException>((TestDelegate)(() => { New <AxCryptFile>().Document(sourceFileInfo, LogOnIdentity.Empty, nullProgress); }));

            Assert.ThrowsAsync <ArgumentNullException>((async() => { await New <AxCryptFile>().EncryptToFileWithBackupAsync(null, async(Stream stream) => { await Task.Delay(0); }, new ProgressContext()); }));
            using (FileLock fileInfoLock = New <FileLocker>().Acquire(New <IDataStore>(_testTextPath)))
            {
                Assert.ThrowsAsync <ArgumentNullException>((async() => { await New <AxCryptFile>().EncryptToFileWithBackupAsync(fileInfoLock, nullStreamAction, new ProgressContext()); }));
            }

            Assert.Throws <ArgumentNullException>(() => { AxCryptFile.MakeAxCryptFileName(nullFileInfo); });
            Assert.Throws <ArgumentNullException>((TestDelegate)(() => { New <AxCryptFile>().Wipe(nullFileLock, new ProgressContext()); }));
        }
 public void TestDecryptWithoutLoadFirstFromEmptyFile()
 {
     using (V1AxCryptDocument document = new V1AxCryptDocument())
     {
         using (MemoryStream plaintextStream = new MemoryStream())
         {
             Assert.Throws <InternalErrorException>(() => { document.DecryptTo(plaintextStream); });
         }
     }
 }
        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 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 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 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 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 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 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 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 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 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.");
                }
            }
        }
        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.");
                }
            }
        }
Ejemplo n.º 23
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 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 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 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.");
                        }
                    }
                }
            }
        }