public static void TestUnexpectedBlockType()
        {
            byte[] buffer = new byte[2000];
            using (V2AxCryptDataStream axCryptDataStreamWriter = V2AxCryptDataStream.Create(new MemoryStream(buffer)))
            {
                AxCrypt1Guid.Write(axCryptDataStreamWriter.Chained);
                new PreambleHeaderBlock().Write(axCryptDataStreamWriter.Chained);
                new DataHeaderBlock().Write(axCryptDataStreamWriter.Chained);

                byte[] bytes = Encoding.UTF8.GetBytes("This is a short text.");
                axCryptDataStreamWriter.Write(bytes, 0, bytes.Length);
                axCryptDataStreamWriter.Flush();
                new DataHeaderBlock().Write(axCryptDataStreamWriter.Chained);
            }
            using (AxCryptReader reader = new TestingAxCryptReader(new LookAheadStream(new MemoryStream(buffer))))
            {
                while (reader.Read())
                {
                    ;
                }
                reader.SetStartOfData();
                using (TextReader textReader = new StreamReader(V2AxCryptDataStream.Create(reader, Stream.Null), Encoding.UTF8))
                {
                    string text = null;
                    Assert.Throws <FileFormatException>(() => text = textReader.ReadToEnd());
                    Assert.That(text, Is.Null);
                }
            }
        }
예제 #2
0
        private Stream CreateEncryptedDataStream()
        {
            if (_reader.CurrentItemType != AxCryptItemType.Data)
            {
                throw new InvalidOperationException("An attempt was made to create an encrypted data stream when the reader is not positioned at the data.");
            }

            _reader.SetStartOfData();
            return(V2AxCryptDataStream.Create(_reader, V2HmacStream.Create(DocumentHeaders.HmacCalculator)));
        }
예제 #3
0
        /// <summary>
        /// Encrypt a stream with a given set of headers and write to an output stream. The caller is responsible for consistency and completeness
        /// of the headers. Headers that are not known until encryption and compression are added here.
        /// </summary>
        /// <param name="outputDocumentHeaders"></param>
        /// <param name="inputStream"></param>
        /// <param name="outputStream"></param>
        public void EncryptTo(Stream inputStream, Stream outputStream, AxCryptOptions options)
        {
            if (inputStream == null)
            {
                throw new ArgumentNullException("inputStream");
            }
            if (outputStream == null)
            {
                throw new ArgumentNullException("outputStream");
            }
            if (options.HasMask(AxCryptOptions.EncryptWithCompression) && options.HasMask(AxCryptOptions.EncryptWithoutCompression))
            {
                throw new ArgumentException("Invalid options, cannot specify both with and without compression.");
            }
            if (!options.HasMask(AxCryptOptions.EncryptWithCompression) && !options.HasMask(AxCryptOptions.EncryptWithoutCompression))
            {
                throw new ArgumentException("Invalid options, must specify either with or without compression.");
            }

            DocumentHeaders.IsCompressed = options.HasMask(AxCryptOptions.EncryptWithCompression);
            V2HmacCalculator      hmacCalculator   = new V2HmacCalculator(new SymmetricKey(DocumentHeaders.GetHmacKey()));
            V2HmacStream <Stream> outputHmacStream = V2HmacStream.Create(hmacCalculator, outputStream);

            CryptoStreamBase encryptingStream = New <CryptoStreamBase>().Initialize(V2AxCryptDataStream.Create(outputHmacStream), DocumentHeaders.DataCrypto().EncryptingTransform(), CryptoStreamMode.Write);

            DocumentHeaders.WriteStartWithHmac(outputHmacStream);
            if (DocumentHeaders.IsCompressed)
            {
                using (ZOutputStream deflatingStream = new ZOutputStream(encryptingStream, -1))
                {
                    deflatingStream.FlushMode = JZlib.Z_SYNC_FLUSH;
                    inputStream.CopyTo(deflatingStream);
                    deflatingStream.FlushMode = JZlib.Z_FINISH;
                    deflatingStream.Finish();

                    _plaintextLength           = deflatingStream.TotalIn;
                    _compressedPlaintextLength = deflatingStream.TotalOut;
                    encryptingStream.FinalFlush();
                    DocumentHeaders.WriteEndWithHmac(hmacCalculator, outputHmacStream, _plaintextLength, _compressedPlaintextLength);
                }
            }
            else
            {
                try
                {
                    _compressedPlaintextLength = _plaintextLength = StreamExtensions.CopyTo(inputStream, encryptingStream);
                    encryptingStream.FinalFlush();
                    DocumentHeaders.WriteEndWithHmac(hmacCalculator, outputHmacStream, _plaintextLength, _compressedPlaintextLength);
                }
                finally
                {
                    encryptingStream.Dispose();
                }
            }
        }
        public static void TestNotSupportedMethods()
        {
            using (V2AxCryptDataStream stream = V2AxCryptDataStream.Create(Stream.Null))
            {
                long position;
                Assert.Throws <NotSupportedException>(() => position        = stream.Position);
                Assert.Throws <NotSupportedException>(() => stream.Position = 0);

                Assert.That(stream.CanSeek, Is.False);

                long length;
                Assert.Throws <NotSupportedException>(() => length = stream.Length);
                Assert.Throws <NotSupportedException>(() => stream.SetLength(0));

                Assert.Throws <NotSupportedException>(() => position = stream.Seek(0, SeekOrigin.Begin));
            }
        }
        public static void TestLongerReadWrite()
        {
            byte[] bytesToWrite = new FakeRandomGenerator().Generate(V2AxCryptDataStream.WriteChunkSize + V2AxCryptDataStream.WriteChunkSize / 2);
            byte[] buffer       = new byte[bytesToWrite.Length + 2000];
            using (V2AxCryptDataStream axCryptDataStreamWriter = V2AxCryptDataStream.Create(new MemoryStream(buffer)))
            {
                AxCrypt1Guid.Write(axCryptDataStreamWriter.Chained);
                new PreambleHeaderBlock().Write(axCryptDataStreamWriter.Chained);
                new DataHeaderBlock().Write(axCryptDataStreamWriter.Chained);

                axCryptDataStreamWriter.Write(bytesToWrite, 0, bytesToWrite.Length);
                axCryptDataStreamWriter.Flush();
                new V2HmacHeaderBlock().Write(axCryptDataStreamWriter.Chained);
            }

            using (AxCryptReader reader = new TestingAxCryptReader(new LookAheadStream(new MemoryStream(buffer))))
            {
                while (reader.Read())
                {
                    ;
                }
                reader.SetStartOfData();
                using (V2AxCryptDataStream axCryptDataStreamReader = V2AxCryptDataStream.Create(reader, Stream.Null))
                {
                    byte[] bytesRead = new byte[bytesToWrite.Length];
                    int    offset    = 0;
                    int    count;
                    do
                    {
                        count   = axCryptDataStreamReader.Read(bytesRead, offset, 100);
                        offset += count;
                    } while (count > 0);
                    Assert.That(bytesRead, Is.EquivalentTo(bytesToWrite));
                }
            }
        }