/// <summary>
        /// Perform the initial read on an entry which may include
        /// reading encryption headers and setting up inflation.
        /// </summary>
        /// <param name="destination">The destination to fill with data read.</param>
        /// <param name="offset">The offset to start reading at.</param>
        /// <param name="count">The maximum number of bytes to read.</param>
        /// <returns>The actual number of bytes read.</returns>
        int InitialRead(byte[] destination, int offset, int count)
        {
            if (!CanDecompressEntry)
            {
                throw new ZipException("Library cannot extract this entry. Version required is (" + entry.Version.ToString() + ")");
            }

            // Handle encryption if required.
            if (entry.IsCrypted)
            {
#if COMPACT_FRAMEWORK_V10
                throw new ZipException("Encyptiong not supported for Compact Framework 1.0");
#else
                if (password == null)
                {
                    throw new ZipException("No password set.");
                }

                // Generate and set crypto transform...
                PkzipClassicManaged managed = new PkzipClassicManaged();
                byte[] key = PkzipClassic.GenerateKeys(Encoding.ASCII.GetBytes(password));

                inputBuffer.CryptoTransform = managed.CreateDecryptor(key, null);

                byte[] cryptbuffer = new byte[ZipConstants.CryptoHeaderSize];
                inputBuffer.ReadClearTextBuffer(cryptbuffer, 0, ZipConstants.CryptoHeaderSize);

                if (cryptbuffer[ZipConstants.CryptoHeaderSize - 1] != entry.CryptoCheckValue)
                {
                    throw new ZipException("Invalid password");
                }

                if (csize >= ZipConstants.CryptoHeaderSize)
                {
                    csize -= ZipConstants.CryptoHeaderSize;
                }
#endif
            }
            else
            {
#if !COMPACT_FRAMEWORK_V10
                inputBuffer.CryptoTransform = null;
#endif
            }

            if ((method == (int)CompressionMethod.Deflated) && (inputBuffer.Available > 0))
            {
                inputBuffer.SetInflaterInput(inf);
            }

            internalReader = new ReaderDelegate(BodyRead);
            return(BodyRead(destination, offset, count));
        }
Example #2
0
        /// <summary>
        /// Perform the initial read on an entry which may include
        /// reading encryption headers and setting up inflation.
        /// </summary>
        /// <param name="destination">The destination to fill with data read.</param>
        /// <param name="offset">The offset to start reading at.</param>
        /// <param name="count">The maximum number of bytes to read.</param>
        /// <returns>The actual number of bytes read.</returns>
        private int InitialRead(byte[] destination, int offset, int count)
        {
            if (!CanDecompressEntry)
            {
                throw new ZipException("Library cannot extract this entry. Version required is (" + entry.Version + ")");
            }

            // Handle encryption if required.
            if (entry.IsCrypted)
            {
                if (password == null)
                {
                    throw new ZipException("No password set.");
                }

                // Generate and set crypto transform...
                var managed = new PkzipClassicManaged();
                var key     = PkzipClassic.GenerateKeys(ZipConstants.ConvertToArray(password));

                inputBuffer.CryptoTransform = managed.CreateDecryptor(key, null);

                var cryptbuffer = new byte[ZipConstants.CryptoHeaderSize];
                inputBuffer.ReadClearTextBuffer(cryptbuffer, 0, ZipConstants.CryptoHeaderSize);

                if (cryptbuffer[ZipConstants.CryptoHeaderSize - 1] != entry.CryptoCheckValue)
                {
                    throw new ZipException("Invalid password");
                }

                if (csize >= ZipConstants.CryptoHeaderSize)
                {
                    csize -= ZipConstants.CryptoHeaderSize;
                }
                else if ((entry.Flags & (int)GeneralBitFlags.Descriptor) == 0)
                {
                    throw new ZipException(string.Format("Entry compressed size {0} too small for encryption", csize));
                }
            }
            else
            {
                inputBuffer.CryptoTransform = null;
            }

            if ((method == (int)CompressionMethod.Deflated) && (inputBuffer.Available > 0))
            {
                inputBuffer.SetInflaterInput(inf);
            }

            internalReader = BodyRead;
            return(BodyRead(destination, offset, count));
        }
Example #3
0
 private int InitialRead(byte[] destination, int offset, int count)
 {
     if (!CanDecompressEntry)
     {
         throw new ZipException("Library cannot extract this entry. Version required is ("
                                + _entry.Version + ")");
     }
     if (_entry.IsCrypted)
     {
         if (_password == null)
         {
             throw new ZipException("No password set.");
         }
         var managed = new PkzipClassicManaged();
         var rgbKey  = PkzipClassic.GenerateKeys(ZipConstants.ConvertToArray(_password));
         InputBuffer.CryptoTransform = managed.CreateDecryptor(rgbKey, null);
         var outBuffer = new byte[12];
         InputBuffer.ReadClearTextBuffer(outBuffer, 0, 12);
         if (outBuffer[11] != _entry.CryptoCheckValue)
         {
             throw new ZipException("Invalid password");
         }
         if (Csize < 12L)
         {
             if ((_entry.Flags & 8) == 0)
             {
                 throw new ZipException($"Entry compressed size {Csize} too small for encryption");
             }
         }
         else
         {
             Csize -= 12L;
         }
     }
     else
     {
         InputBuffer.CryptoTransform = null;
     }
     if ((Csize > 0L) || ((_flags & 8) != 0))
     {
         if ((_method == 8) && (InputBuffer.Available > 0))
         {
             InputBuffer.SetInflaterInput(Inf);
         }
         _internalReader = BodyRead;
         return(BodyRead(destination, offset, count));
     }
     _internalReader = ReadingNotAvailable;
     return(0);
 }
 private int InitialRead(byte[] destination, int offset, int count)
 {
     if (!this.CanDecompressEntry)
     {
         throw new ZipException("Library cannot extract this entry. Version required is (" + this.entry.Version + ")");
     }
     if (this.entry.IsCrypted)
     {
         if (this.password == null)
         {
             throw new ZipException("No password set.");
         }
         PkzipClassicManaged pkzipClassicManaged = new PkzipClassicManaged();
         byte[] rgbKey = PkzipClassic.GenerateKeys(ZipConstants.ConvertToArray(this.password));
         this.inputBuffer.CryptoTransform = pkzipClassicManaged.CreateDecryptor(rgbKey, null);
         byte[] array = new byte[12];
         this.inputBuffer.ReadClearTextBuffer(array, 0, 12);
         if (array[11] != this.entry.CryptoCheckValue)
         {
             throw new ZipException("Invalid password");
         }
         if (this.csize >= 12L)
         {
             this.csize -= 12L;
         }
         else if ((this.entry.Flags & 8) == 0)
         {
             throw new ZipException(string.Format("Entry compressed size {0} too small for encryption", this.csize));
         }
     }
     else
     {
         this.inputBuffer.CryptoTransform = null;
     }
     if (this.csize > 0L || (this.flags & 8) != 0)
     {
         if (this.method == 8 && this.inputBuffer.Available > 0)
         {
             this.inputBuffer.SetInflaterInput(this.inf);
         }
         this.internalReader = new ZipInputStream.ReadDataHandler(this.BodyRead);
         return(this.BodyRead(destination, offset, count));
     }
     this.internalReader = new ZipInputStream.ReadDataHandler(this.ReadingNotAvailable);
     return(0);
 }
Example #5
0
 private int InitialRead(byte[] destination, int offset, int count)
 {
     if (!CanDecompressEntry)
     {
         throw new ZipException("Library cannot extract this entry. Version required is (" + entry.Version + ")");
     }
     if (entry.IsCrypted)
     {
         if (password == null)
         {
             throw new ZipException("No password set.");
         }
         PkzipClassicManaged pkzipClassicManaged = new PkzipClassicManaged();
         byte[] array = PkzipClassic.GenerateKeys(ZipConstants.ConvertToArray(password));
         inputBuffer.CryptoTransform = ((SymmetricAlgorithm)pkzipClassicManaged).CreateDecryptor(array, (byte[])null);
         byte[] array2 = new byte[12];
         inputBuffer.ReadClearTextBuffer(array2, 0, 12);
         if (array2[11] != entry.CryptoCheckValue)
         {
             throw new ZipException("Invalid password");
         }
         if (csize >= 12)
         {
             csize -= 12L;
         }
         else if ((entry.Flags & 8) == 0)
         {
             throw new ZipException($"Entry compressed size {csize} too small for encryption");
         }
     }
     else
     {
         inputBuffer.CryptoTransform = null;
     }
     if (csize > 0 || ((uint)flags & 8u) != 0)
     {
         if (method == 8 && inputBuffer.Available > 0)
         {
             inputBuffer.SetInflaterInput(inf);
         }
         internalReader = BodyRead;
         return(BodyRead(destination, offset, count));
     }
     internalReader = ReadingNotAvailable;
     return(0);
 }
 private int InitialRead(byte[] destination, int offset, int count)
 {
     if (this.entry.Version > 20)
     {
         throw new ZipException("Libray cannot extract this entry version required (" + this.entry.Version.ToString() + ")");
     }
     if (this.entry.IsCrypted)
     {
         if (this.password == null)
         {
             throw new ZipException("No password set.");
         }
         PkzipClassicManaged managed = new PkzipClassicManaged();
         byte[] rgbKey = PkzipClassic.GenerateKeys(Encoding.ASCII.GetBytes(this.password));
         base.inputBuffer.CryptoTransform = managed.CreateDecryptor(rgbKey, null);
         byte[] outBuffer = new byte[12];
         base.inputBuffer.ReadClearTextBuffer(outBuffer, 0, 12);
         if ((this.flags & 8) == 0)
         {
             if (outBuffer[11] != ((byte)(this.entry.Crc >> 0x18)))
             {
                 throw new ZipException("Invalid password");
             }
         }
         else if (outBuffer[11] != ((byte)((this.entry.DosTime >> 8) & 0xffL)))
         {
             throw new ZipException("Invalid password");
         }
         if (base.csize >= 12L)
         {
             base.csize -= 12L;
         }
     }
     else
     {
         base.inputBuffer.CryptoTransform = null;
     }
     if ((this.method == 8) && (base.inputBuffer.Available > 0))
     {
         base.inputBuffer.SetInflaterInput(base.inf);
     }
     this.internalReader = new ReaderDelegate(this.BodyRead);
     return(this.BodyRead(destination, offset, count));
 }
Example #7
0
 private int InitialRead(byte[] destination, int offset, int count)
 {
     if (!this.CanDecompressEntry)
     {
         throw new ZipException("Library cannot extract this entry. Version required is (" + this.entry.Version.ToString() + ")");
     }
     if (this.entry.IsCrypted)
     {
         if (this.password == null)
         {
             throw new ZipException("No password set.");
         }
         PkzipClassicManaged managed = new PkzipClassicManaged();
         byte[] rgbKey = PkzipClassic.GenerateKeys(ZipConstants.ConvertToArray(this.password));
         base.inputBuffer.CryptoTransform = managed.CreateDecryptor(rgbKey, null);
         byte[] outBuffer = new byte[12];
         base.inputBuffer.ReadClearTextBuffer(outBuffer, 0, 12);
         if (outBuffer[11] != this.entry.CryptoCheckValue)
         {
             throw new ZipException("Invalid password");
         }
         if (base.csize < 12L)
         {
             if ((this.entry.Flags & 8) == 0)
             {
                 throw new ZipException($"Entry compressed size {base.csize} too small for encryption");
             }
         }
         else
         {
             base.csize -= 12L;
         }
     }
     else
     {
         base.inputBuffer.CryptoTransform = null;
     }
     if ((this.method == 8) && (base.inputBuffer.Available > 0))
     {
         base.inputBuffer.SetInflaterInput(base.inf);
     }
     this.internalReader = new ReaderDelegate(this.BodyRead);
     return(this.BodyRead(destination, offset, count));
 }
Example #8
0
        /// <summary>
        /// Initializes encryption keys based on given <paramref name="password"/>.
        /// </summary>
        /// <param name="password">The password.</param>
        protected void InitializePassword(string password)
        {
#if NETCF_1_0
            keys = new uint[] {
                0x12345678,
                0x23456789,
                0x34567890
            };

            byte[] rawPassword = ZipConstants.ConvertToArray(password);

            for (int i = 0; i < rawPassword.Length; ++i)
            {
                UpdateKeys((byte)rawPassword[i]);
            }
#else
            PkzipClassicManaged pkManaged = new PkzipClassicManaged();
            byte[] key = PkzipClassic.GenerateKeys(ZipConstants.ConvertToArray(password));
            cryptoTransform_ = pkManaged.CreateEncryptor(key, null);
#endif
        }
Example #9
0
 private int InitialRead(byte[] destination, int offset, int count)
 {
     if (!this.CanDecompressEntry)
     {
         throw new ZipException("Library cannot extract this entry. Version required is (" + this.entry.Version.ToString() + ")");
     }
     if (this.entry.IsCrypted)
     {
         if (this.password == null)
         {
             throw new ZipException("No password set.");
         }
         PkzipClassicManaged pkzipClassicManaged = new PkzipClassicManaged();
         byte[] rgbKey = PkzipClassic.GenerateKeys(Encoding.ASCII.GetBytes(this.password));
         this.inputBuffer.CryptoTransform = pkzipClassicManaged.CreateDecryptor(rgbKey, null);
         byte[] array = new byte[12];
         this.inputBuffer.ReadClearTextBuffer(array, 0, 12);
         if (array[11] != this.entry.CryptoCheckValue)
         {
             throw new ZipException("Invalid password");
         }
         if (this.csize >= 12L)
         {
             this.csize -= 12L;
         }
     }
     else
     {
         this.inputBuffer.CryptoTransform = null;
     }
     if (this.method == 8 && this.inputBuffer.Available > 0)
     {
         this.inputBuffer.SetInflaterInput(this.inf);
     }
     this.internalReader = new ZipInputStream.ReaderDelegate(this.BodyRead);
     return(this.BodyRead(destination, offset, count));
 }
Example #10
0
        /// <summary>
        /// Perform the initial read on an entry which may include
        /// reading encryption headers and setting up inflation.
        /// </summary>
        /// <param name="destination">The destination to fill with data read.</param>
        /// <param name="offset">The offset to start reading at.</param>
        /// <param name="count">The maximum number of bytes to read.</param>
        /// <returns>The actual number of bytes read.</returns>
        private int InitialRead(byte[] destination, int offset, int count)
        {
            var usesDescriptor = (entry.Flags & (int)GeneralBitFlags.Descriptor) != 0;

            // Handle encryption if required.
            if (entry.IsCrypted)
            {
                if (password == null)
                {
                    throw new ZipException("No password set.");
                }

                // Generate and set crypto transform...
                var    managed = new PkzipClassicManaged();
                byte[] key     = PkzipClassic.GenerateKeys(ZipStrings.ConvertToArray(password));

                inputBuffer.CryptoTransform = managed.CreateDecryptor(key, null);

                byte[] cryptbuffer = new byte[ZipConstants.CryptoHeaderSize];
                inputBuffer.ReadClearTextBuffer(cryptbuffer, 0, ZipConstants.CryptoHeaderSize);

                if (cryptbuffer[ZipConstants.CryptoHeaderSize - 1] != entry.CryptoCheckValue)
                {
                    throw new ZipException("Invalid password");
                }

                if (csize >= ZipConstants.CryptoHeaderSize)
                {
                    csize -= ZipConstants.CryptoHeaderSize;
                }
                else if (!usesDescriptor)
                {
                    throw new ZipException($"Entry compressed size {csize} too small for encryption");
                }
            }
            else
            {
                inputBuffer.CryptoTransform = null;
            }

            if (csize > 0 || usesDescriptor)
            {
                if (method == CompressionMethod.Deflated && inputBuffer.Available > 0)
                {
                    inputBuffer.SetInflaterInput(inf);
                }

                // It's not possible to know how many bytes to read when using "Stored" compression (unless using encryption)
                if (!entry.IsCrypted && method == CompressionMethod.Stored && usesDescriptor)
                {
                    internalReader = StoredDescriptorEntry;
                    return(StoredDescriptorEntry(destination, offset, count));
                }

                if (!CanDecompressEntry)
                {
                    internalReader = ReadingNotSupported;
                    return(ReadingNotSupported(destination, offset, count));
                }

                internalReader = BodyRead;
                return(BodyRead(destination, offset, count));
            }


            internalReader = ReadingNotAvailable;
            return(0);
        }
Example #11
0
        /// <summary>
        /// Perform the initial read on an entry which may include
        /// reading encryption headers and setting up inflation.
        /// </summary>
        /// <param name="destination">The destination to fill with data read.</param>
        /// <param name="offset">The offset to start reading at.</param>
        /// <param name="count">The maximum number of bytes to read.</param>
        /// <returns>The actual number of bytes read.</returns>
        int InitialRead(byte[] destination, int offset, int count)
        {
            if (!CanDecompressEntry)
            {
                throw new ZipException("Library cannot extract this entry. Version required is (" + entry.Version.ToString() + ")");
            }

            // Handle encryption if required.
            if (entry.IsCrypted)
            {
#if NETCF_1_0 || UNITY_WINRT
                throw new ZipException("Encryption not supported for Compact Framework 1.0");
#else
                if (password == null)
                {
                    throw new ZipException("No password set.");
                }

                // Generate and set crypto transform...
                PkzipClassicManaged managed = new PkzipClassicManaged();
                byte[] key = PkzipClassic.GenerateKeys(ZipConstants.ConvertToArray(password));

                inputBuffer.CryptoTransform = managed.CreateDecryptor(key, null);

                byte[] cryptbuffer = new byte[ZipConstants.CryptoHeaderSize];
                inputBuffer.ReadClearTextBuffer(cryptbuffer, 0, ZipConstants.CryptoHeaderSize);

                if (cryptbuffer[ZipConstants.CryptoHeaderSize - 1] != entry.CryptoCheckValue)
                {
                    throw new ZipException("Invalid password");
                }

                if (csize >= ZipConstants.CryptoHeaderSize)
                {
                    csize -= ZipConstants.CryptoHeaderSize;
                }
                else if ((entry.Flags & (int)GeneralBitFlags.Descriptor) == 0)
                {
                    throw new ZipException(string.Format("Entry compressed size {0} too small for encryption", csize));
                }
#endif
            }
            else
            {
#if !NETCF_1_0 && !UNITY_WINRT
                inputBuffer.CryptoTransform = null;
#endif
            }

            if ((csize > 0) || ((flags & (int)GeneralBitFlags.Descriptor) != 0))
            {
                if ((method == (int)CompressionMethod.Deflated) && (inputBuffer.Available > 0))
                {
                    inputBuffer.SetInflaterInput(inf);
                }

                internalReader = new ReadDataHandler(BodyRead);
                return(BodyRead(destination, offset, count));
            }
            else
            {
                internalReader = new ReadDataHandler(ReadingNotAvailable);
                return(0);
            }
        }
Example #12
0
 protected void InitializePassword(string password)
 {
     this.cryptoTransform_ = new PkzipClassicManaged().CreateEncryptor(PkzipClassic.GenerateKeys(ZipConstants.ConvertToArray(password)), null);
 }