Example #1
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)));
        }
        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 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));
                    }
                }
            }
        }