Exemplo n.º 1
0
        private void LoadHeader(ZipFileEntry entryHeader, Stream stream)
        {
            if (FlagUtility.HasFlag(entryHeader.Flags, HeaderFlags.Encrypted))
            {
                if (!entryHeader.IsDirectory &&
                    entryHeader.CompressedSize == 0 &&
                    FlagUtility.HasFlag(entryHeader.Flags, HeaderFlags.UsePostDataDescriptor))
                {
                    throw new NotSupportedException("SharpCompress cannot currently read non-seekable Zip Streams with encrypted data that has been written in a non-seekable manner.");
                }
                if (password == null)
                {
                    throw new CryptographicException("No password supplied for encrypted zip.");
                }
                if (entryHeader.CompressionMethod != ZipCompressionMethod.WinzipAes)
                {
                    byte[] buffer = new byte[12];
                    stream.Read(buffer, 0, 12);
                    entryHeader.PkwareTraditionalEncryptionData = PkwareTraditionalEncryptionData.ForRead(password, entryHeader, buffer);
                    entryHeader.CompressedSize -= 12;
                }
                else
                {
                    throw new NotSupportedException("Cannot decrypt Winzip AES with Silverlight or WP7.");
                }
            }
            if (entryHeader.IsDirectory)
            {
                return;
            }
            //if (FlagUtility.HasFlag(entryHeader.Flags, HeaderFlags.UsePostDataDescriptor))
            //{
            //    entryHeader.PackedStream = new ReadOnlySubStream(stream);
            //}
            //else
            //{
            switch (mode)
            {
            case StreamingMode.Seekable:
            {
                entryHeader.DataStartPosition = stream.Position;
                stream.Position += entryHeader.CompressedSize;
            }
            break;

            case StreamingMode.Streaming:
            {
                entryHeader.PackedStream = stream;
            }
            break;

            default:
            {
                throw new InvalidFormatException("Invalid StreamingMode");
            }
            }
            //}
        }
        public static PkwareTraditionalEncryptionData ForWrite(string password, uint crc, out byte[] encryptionHeader)
        {
            var encryptor = new PkwareTraditionalEncryptionData(password);

            var random          = new Random();
            var plainTextHeader = new byte[12];

            random.NextBytes(plainTextHeader);

            plainTextHeader[11] = (byte)((crc >> 24) & 0xff);

            encryptionHeader = encryptor.Encrypt(plainTextHeader, plainTextHeader.Length);
            return(encryptor);
        }
        public static PkwareTraditionalEncryptionData ForWrite(string password, DateTime?modificationTime, out byte[] encryptionHeader)
        {
            var encryptor = new PkwareTraditionalEncryptionData(password);

            var random          = new Random();
            var plainTextHeader = new byte[12];

            random.NextBytes(plainTextHeader);

            uint timeBlob = modificationTime.DateTimeToDosTime();

            plainTextHeader[11] = (byte)((timeBlob >> 8) & 0xff);

            encryptionHeader = encryptor.Encrypt(plainTextHeader, plainTextHeader.Length);
            return(encryptor);
        }
 public static PkwareTraditionalEncryptionData ForRead(string password, ZipFileEntry header, byte[] encryptionHeader)
 {
     var encryptor = new PkwareTraditionalEncryptionData(password);
     byte[] plainTextHeader = encryptor.Decrypt(encryptionHeader, encryptionHeader.Length);
     if (plainTextHeader[11] != (byte)((header.Crc >> 24) & 0xff))
     {
         if (!FlagUtility.HasFlag(header.Flags, HeaderFlags.UsePostDataDescriptor))
         {
             throw new CryptographicException("The password did not match.");
         }
         if (plainTextHeader[11] != (byte)((header.LastModifiedTime >> 8) & 0xff))
         {
             throw new CryptographicException("The password did not match.");
         }
     }
     return encryptor;
 }
 public static PkwareTraditionalEncryptionData ForRead(string password, ZipFileEntry header, byte[] encryptionHeader)
 {
     PkwareTraditionalEncryptionData data = new PkwareTraditionalEncryptionData(password);
     byte[] buffer = data.Decrypt(encryptionHeader, encryptionHeader.Length);
     if (buffer[11] != ((byte) ((header.Crc >> 0x18) & 0xff)))
     {
         if (!FlagUtility.HasFlag<HeaderFlags>(header.Flags, HeaderFlags.UsePostDataDescriptor))
         {
             throw new CryptographicException("The password did not match.");
         }
         if (buffer[11] != ((byte) ((header.LastModifiedTime >> 8) & 0xff)))
         {
             throw new CryptographicException("The password did not match.");
         }
     }
     return data;
 }
Exemplo n.º 6
0
        public static PkwareTraditionalEncryptionData ForRead(string password, ZipFileEntry header, byte[] encryptionHeader)
        {
            var encryptor = new PkwareTraditionalEncryptionData(password);

            byte[] plainTextHeader = encryptor.Decrypt(encryptionHeader, encryptionHeader.Length);
            if (plainTextHeader[11] != (byte)((header.Crc >> 24) & 0xff))
            {
                if (!FlagUtility.HasFlag(header.Flags, HeaderFlags.UsePostDataDescriptor))
                {
                    throw new CryptographicException("The password did not match.");
                }
                if (plainTextHeader[11] != (byte)((header.LastModifiedTime >> 8) & 0xff))
                {
                    throw new CryptographicException("The password did not match.");
                }
            }
            return(encryptor);
        }
Exemplo n.º 7
0
        private void LoadHeader(ZipFileEntry entryHeader, Stream stream)
        {
            if (FlagUtility.HasFlag <HeaderFlags>(entryHeader.Flags, HeaderFlags.Encrypted))
            {
                if ((!entryHeader.IsDirectory && (entryHeader.CompressedSize == 0)) && FlagUtility.HasFlag <HeaderFlags>(entryHeader.Flags, HeaderFlags.UsePostDataDescriptor))
                {
                    throw new NotSupportedException("SharpCompress cannot currently read non-seekable Zip Streams with encrypted data that has been written in a non-seekable manner.");
                }
                if (this.password == null)
                {
                    throw new CryptographicException("No password supplied for encrypted zip.");
                }
                if (entryHeader.CompressionMethod == ZipCompressionMethod.WinzipAes)
                {
                    throw new NotSupportedException("Cannot decrypt Winzip AES with Silverlight or WP7.");
                }
                byte[] buffer = new byte[12];
                stream.Read(buffer, 0, 12);
                entryHeader.PkwareTraditionalEncryptionData = PkwareTraditionalEncryptionData.ForRead(this.password, entryHeader, buffer);
                entryHeader.CompressedSize -= 12;
            }
            if (!entryHeader.IsDirectory)
            {
                switch (this.mode)
                {
                case StreamingMode.Streaming:
                    entryHeader.PackedStream = stream;
                    return;

                case StreamingMode.Seekable:
                    entryHeader.DataStartPosition = new long?(stream.Position);
                    stream.Position += entryHeader.CompressedSize;
                    return;
                }
                throw new InvalidFormatException("Invalid StreamingMode");
            }
        }
Exemplo n.º 8
0
 public PkwareTraditionalCryptoStream(Stream stream, PkwareTraditionalEncryptionData encryptor, CryptoMode mode)
 {
     this.encryptor = encryptor;
     this.stream    = stream;
     this.mode      = mode;
 }
Exemplo n.º 9
0
        private void LoadHeader(ZipFileEntry entryHeader, Stream stream)
        {
            if (FlagUtility.HasFlag(entryHeader.Flags, HeaderFlags.Encrypted))
            {
                if (!entryHeader.IsDirectory &&
                    entryHeader.CompressedSize == 0 &&
                    FlagUtility.HasFlag(entryHeader.Flags, HeaderFlags.UsePostDataDescriptor))
                {
                    throw new NotSupportedException(
                              "SharpCompress cannot currently read non-seekable Zip Streams with encrypted data that has been written in a non-seekable manner.");
                }
                if (password == null)
                {
                    throw new CryptographicException("No password supplied for encrypted zip.");
                }
                if (entryHeader.CompressionMethod != ZipCompressionMethod.WinzipAes)
                {
                    byte[] buffer = new byte[12];
                    stream.Read(buffer, 0, 12);
                    entryHeader.PkwareTraditionalEncryptionData = PkwareTraditionalEncryptionData.ForRead(password,
                                                                                                          entryHeader,
                                                                                                          buffer);
                    entryHeader.CompressedSize -= 12;
                }
                else
                {
#if NO_CRYPTO
                    throw new NotSupportedException("Cannot decrypt Winzip AES with Silverlight or WP7.");
#else
                    var data = entryHeader.Extra.SingleOrDefault(x => x.Type == ExtraDataType.WinZipAes);
                    WinzipAesKeySize keySize = (WinzipAesKeySize)data.DataBytes[4];

                    byte[] salt = new byte[WinzipAesEncryptionData.KeyLengthInBytes(keySize) / 2];
                    byte[] passwordVerifyValue = new byte[2];
                    stream.Read(salt, 0, salt.Length);
                    stream.Read(passwordVerifyValue, 0, 2);
                    entryHeader.WinzipAesEncryptionData = new WinzipAesEncryptionData(keySize, salt, passwordVerifyValue,
                                                                                      password);
                    entryHeader.CompressedSize -= (uint)(salt.Length + 2);
#endif
                }
            }
            if (entryHeader.IsDirectory)
            {
                return;
            }
            //if (FlagUtility.HasFlag(entryHeader.Flags, HeaderFlags.UsePostDataDescriptor))
            //{
            //    entryHeader.PackedStream = new ReadOnlySubStream(stream);
            //}
            //else
            //{
            switch (mode)
            {
            case StreamingMode.Seekable:
            {
                entryHeader.DataStartPosition = stream.Position;
                stream.Position += entryHeader.CompressedSize;
            }
            break;

            case StreamingMode.Streaming:
            {
                entryHeader.PackedStream = stream;
            }
            break;

            default:
            {
                throw new InvalidFormatException("Invalid StreamingMode");
            }
            }
            //}
        }
 public PkwareTraditionalCryptoStream(Stream stream, PkwareTraditionalEncryptionData encryptor, CryptoMode mode)
 {
     this.encryptor = encryptor;
     this.stream = stream;
     this.mode = mode;
 }