예제 #1
0
        public void ZipinputStreamShouldGracefullyFailWithAESStreams()
        {
            string password = "******";

            using (var memoryStream = new MemoryStream())
            {
                // Try to create a zip stream
                WriteEncryptedZipToStream(memoryStream, password, 256);

                // reset
                memoryStream.Seek(0, SeekOrigin.Begin);

                // Try to read
                using (var inputStream = new ZipInputStream(memoryStream))
                {
                    inputStream.Password = password;
                    var entry = inputStream.GetNextEntry();
                    Assert.That(entry.AESKeySize, Is.EqualTo(256), "Test entry should be AES256 encrypted.");

                    // CanDecompressEntry should be false.
                    Assert.That(inputStream.CanDecompressEntry, Is.False, "CanDecompressEntry should be false for AES encrypted entries");

                    // Should throw on read.
                    Assert.Throws <ZipException>(() => inputStream.ReadByte());
                }
            }
        }
예제 #2
0
        public void PasswordCheckingWithDateInExtraData()
        {
            var ms        = new MemoryStream();
            var checkTime = new DateTimeOffset(2010, 10, 16, 0, 3, 28, new TimeSpan(1, 0, 0));

            using (ZipOutputStream zos = new ZipOutputStream(ms))
            {
                zos.IsStreamOwner = false;
                zos.Password      = "******";
                var ze = new ZipEntry("uno");
                ze.DateTime = new DateTime(1998, 6, 5, 4, 3, 2);

                var zed = new ZipExtraData();

                zed.StartNewEntry();

                zed.AddData(1);

                TimeSpan delta   = checkTime.UtcDateTime - new DateTime(1970, 1, 1, 0, 0, 0).ToUniversalTime();
                var      seconds = (int)delta.TotalSeconds;
                zed.AddLeInt(seconds);
                zed.AddNewEntry(0x5455);

                ze.ExtraData = zed.GetEntryData();
                zos.PutNextEntry(ze);
                zos.WriteByte(54);
            }

            ms.Position = 0;
            using (ZipInputStream zis = new ZipInputStream(ms))
            {
                zis.Password = "******";
                ZipEntry uno     = zis.GetNextEntry();
                var      theByte = (byte)zis.ReadByte();
                Assert.AreEqual(54, theByte);
                Assert.AreEqual(-1, zis.ReadByte());                 // eof
                Assert.AreEqual(checkTime.DateTime, uno.DateTime);
            }
        }
예제 #3
0
        /// <summary>
        /// Reads a byte from the stream and advances the position within the stream by one byte, or returns -1 if at the end of the stream.
        /// </summary>
        /// <returns>
        /// The unsigned byte cast to an Int32, or -1 if at the end of the stream.
        /// </returns>
        /// <exception cref="T:System.NotSupportedException">The stream does not support reading. </exception>
        /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
        public override int ReadByte()
        {
            if (_position >= Length)
            {
                return(-1);
            }

            int result = _zipStream.ReadByte();

            _position += result;

            return(result);
        }
예제 #4
0
        private void ZipOk(POIFSFileSystem fs, Decryptor d)
        {
            ZipInputStream zin = new ZipInputStream(d.GetDataStream(fs));

            while (true)
            {
                int pos = zin.ReadByte();
                if (pos == -1)
                {
                    break;
                }
                //    ZipEntry entry = zin.GetNextEntry();
                //    if (entry == null)
                //    {
                //        break;
                //    }

                //while (zin.available() > 0)
                //{
                //    zin.skip(zin.available());
                //}
            }
        }
예제 #5
0
        public void PasswordCheckingWithDateInExtraData()
        {
            var ms = new MemoryStream();
            var checkTime = new DateTime(2010, 10, 16, 0, 3, 28);

            using (var zos = new ZipOutputStream(ms))
            {
                zos.IsStreamOwner = false;
                zos.Password = "******";
                var ze = new ZipEntry("uno");
                ze.DateTime = new DateTime(1998, 6, 5, 4, 3, 2);

                var zed = new ZipExtraData();

                zed.StartNewEntry();

                zed.AddData(1);

                var delta = checkTime.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0).ToUniversalTime();
                var seconds = (int) delta.TotalSeconds;
                zed.AddLeInt(seconds);
                zed.AddNewEntry(0x5455);

                ze.ExtraData = zed.GetEntryData();
                zos.PutNextEntry(ze);
                zos.WriteByte(54);
            }

            ms.Position = 0;
            using (var zis = new ZipInputStream(ms))
            {
                zis.Password = "******";
                var uno = zis.GetNextEntry();
                var theByte = (byte) zis.ReadByte();
                Assert.AreEqual(54, theByte);
                Assert.AreEqual(-1, zis.ReadByte());
                Assert.AreEqual(checkTime, uno.DateTime);
            }
        }