Defines a wrapper object to access the Pkzip algorithm. This class cannot be inherited.
Inheritance: PkzipClassic
Beispiel #1
0
		// Perform the initial read on an entry which may include 
		// reading encryption headers and setting up inflation.
		int InitialRead(byte[] destination, int offset, int count)
		{
			if (entry.Version > ZipConstants.VERSION_MADE_BY) {
				throw new ZipException("Libray cannot extract this entry version required (" + entry.Version.ToString() + ")");
			}
			
			// test for encryption
			if (entry.IsCrypted) {
		
				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.CRYPTO_HEADER_SIZE];
				inputBuffer.ReadClearTextBuffer(cryptbuffer, 0, ZipConstants.CRYPTO_HEADER_SIZE);
					
				if ((flags & 8) == 0) {
					if (cryptbuffer[ZipConstants.CRYPTO_HEADER_SIZE - 1] != (byte)(entry.Crc >> 24)) {
						throw new ZipException("Invalid password");
					}
				}
				else {
					if (cryptbuffer[ZipConstants.CRYPTO_HEADER_SIZE - 1] != (byte)((entry.DosTime >> 8) & 0xff)) {
						throw new ZipException("Invalid password");
					}
				}
					
				if (csize >= ZipConstants.CRYPTO_HEADER_SIZE) {
					csize -= ZipConstants.CRYPTO_HEADER_SIZE;
				}
			} 
			else {
				inputBuffer.CryptoTransform = null;
			}
			
			if (method == (int)CompressionMethod.Deflated && inputBuffer.Available > 0) {
				inputBuffer.SetInflaterInput(inf);
			}
			
			internalReader = new ReaderDelegate(BodyRead);
			return BodyRead(destination, offset, count);
		}
Beispiel #2
0
		Stream CreateAndInitDecryptionStream(Stream baseStream, ZipEntry entry)
		{
			CryptoStream result = null;

			if ( (entry.Version < ZipConstants.VersionStrongEncryption)
				|| (entry.Flags & (int)GeneralBitFlags.StrongEncryption) == 0) {
				PkzipClassicManaged classicManaged = new PkzipClassicManaged();

				OnKeysRequired(entry.Name);
				if (HaveKeys == false) {
					throw new ZipException("No password available for encrypted stream");
				}

				result = new CryptoStream(baseStream, classicManaged.CreateDecryptor(key, null), CryptoStreamMode.Read);
				CheckClassicPassword(result, entry);
			}
			else {
#if !NET_1_1 && !NETCF_2_0
				if (entry.Version == ZipConstants.VERSION_AES) {
					//
					OnKeysRequired(entry.Name);
					if (HaveKeys == false) {
						throw new ZipException("No password available for AES encrypted stream");
					}
					int saltLen = entry.AESSaltLen;
					byte[] saltBytes = new byte[saltLen];
					int saltIn = baseStream.Read(saltBytes, 0, saltLen);
					if (saltIn != saltLen)
						throw new ZipException("AES Salt expected " + saltLen + " got " + saltIn);
					//
					byte[] pwdVerifyRead = new byte[2];
					baseStream.Read(pwdVerifyRead, 0, 2);
					int blockSize = entry.AESKeySize / 8;	// bits to bytes

					ZipAESTransform decryptor = new ZipAESTransform(rawPassword_, saltBytes, blockSize, false);
					byte[] pwdVerifyCalc = decryptor.PwdVerifier;
					if (pwdVerifyCalc[0] != pwdVerifyRead[0] || pwdVerifyCalc[1] != pwdVerifyRead[1])
						throw new ZipException("Invalid password for AES");
					result = new ZipAESStream(baseStream, decryptor, CryptoStreamMode.Read);
				}
				else
#endif
				{
					throw new ZipException("Decryption method not supported");
				}
			}

			return result;
		}
Beispiel #3
0
		Stream CreateAndInitEncryptionStream(Stream baseStream, ZipEntry entry)
		{
			CryptoStream result = null;
			if ( (entry.Version < ZipConstants.VersionStrongEncryption)
				|| (entry.Flags & (int)GeneralBitFlags.StrongEncryption) == 0) {
				PkzipClassicManaged classicManaged = new PkzipClassicManaged();

				OnKeysRequired(entry.Name);
				if (HaveKeys == false) {
					throw new ZipException("No password available for encrypted stream");
				}

				// Closing a CryptoStream will close the base stream as well so wrap it in an UncompressedStream
				// which doesnt do this.
				result = new CryptoStream(new UncompressedStream(baseStream),
					classicManaged.CreateEncryptor(key, null), CryptoStreamMode.Write);

				if ( (entry.Crc < 0) || (entry.Flags & 8) != 0) {
					WriteEncryptionHeader(result, entry.DosTime << 16);
				}
				else {
					WriteEncryptionHeader(result, entry.Crc);
				}
			}
			return result;
		}
		/// <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
				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
				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;
			}
		}
		/// <summary>
		/// Initializes encryption keys based on given 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
		}
Beispiel #6
0
		Stream CreateAndInitDecryptionStream(Stream baseStream, ZipEntry entry)
		{
			CryptoStream result = null;

			if ( (entry.Version < ZipConstants.VersionStrongEncryption)
				|| (entry.Flags & (int)GeneralBitFlags.StrongEncryption) == 0) {
				PkzipClassicManaged classicManaged = new PkzipClassicManaged();

				OnKeysRequired(entry.Name);
				if (HaveKeys == false) {
					throw new ZipException("No password available for encrypted stream");
				}

				result = new CryptoStream(baseStream, classicManaged.CreateDecryptor(key, null), CryptoStreamMode.Read);
				CheckClassicPassword(result, entry);
			}
			else {
				throw new ZipException("Decryption method not supported");
			}

			return result;
		}
		/// <summary>
		/// Initializes encryption keys based on given <paramref name="password"/>.
		/// </summary>
		/// <param name="password">The password.</param>
		protected void InitializePassword(string password)
		{
			PkzipClassicManaged pkManaged = new PkzipClassicManaged();
			byte[] key = PkzipClassic.GenerateKeys(ZipConstants.ConvertToArray(password));
			cryptoTransform_ = pkManaged.CreateEncryptor(key, null);

		}
        /// <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);
        }
Beispiel #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 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(string.Format("Entry compressed size {0} too small for encryption", base.csize));
             }
         }
         else
         {
             base.csize -= 12L;
         }
     }
     else
     {
         base.inputBuffer.CryptoTransform = null;
     }
     if ((base.csize > 0L) || ((this.flags & 8) != 0))
     {
         if ((this.method == 8) && (base.inputBuffer.Available > 0))
         {
             base.inputBuffer.SetInflaterInput(base.inf);
         }
         this.internalReader = new ReadDataHandler(this.BodyRead);
         return this.BodyRead(destination, offset, count);
     }
     this.internalReader = new ReadDataHandler(this.ReadingNotAvailable);
     return 0;
 }
Beispiel #10
0
		Stream CreateAndInitEncryptionStream(Stream baseStream, ZipEntry entry)
		{
			CryptoStream result = null;
			if (entry.Version < ZipConstants.VERSION_STRONG_ENCRYPTION 
			    || (entry.Flags & (int)GeneralBitFlags.StrongEncryption) == 0) {
				PkzipClassicManaged classicManaged = new PkzipClassicManaged();

				OnKeysRequired(entry.Name);
				if (HaveKeys == false) {
					throw new ZipException("No password available for encrypted stream");
				}

				result = new CryptoStream(baseStream, classicManaged.CreateEncryptor(key, iv), CryptoStreamMode.Write);
				if (entry.Crc < 0 || (entry.Flags & 8) != 0) {
					WriteEncryptionHeader(result, entry.DosTime << 16);
				}
				else {
					WriteEncryptionHeader(result, entry.Crc);
				}
			}
			return result;
		}
Beispiel #11
0
 private Stream CreateAndInitEncryptionStream(Stream baseStream, ZipEntry entry)
 {
     CryptoStream stream = null;
     if ((entry.Version < 50) || ((entry.Flags & 0x40) == 0))
     {
         PkzipClassicManaged managed = new PkzipClassicManaged();
         this.OnKeysRequired(entry.Name);
         if (!this.HaveKeys)
         {
             throw new ZipException("No password available for encrypted stream");
         }
         stream = new CryptoStream(new UncompressedStream(baseStream), managed.CreateEncryptor(this.key, null), CryptoStreamMode.Write);
         if ((entry.Crc < 0L) || ((entry.Flags & 8) != 0))
         {
             WriteEncryptionHeader(stream, entry.DosTime << 0x10);
             return stream;
         }
         WriteEncryptionHeader(stream, entry.Crc);
     }
     return stream;
 }
Beispiel #12
0
 private Stream CreateAndInitDecryptionStream(Stream baseStream, ZipEntry entry)
 {
     CryptoStream classicCryptoStream = null;
     if ((entry.Version < 50) || ((entry.Flags & 0x40) == 0))
     {
         PkzipClassicManaged managed = new PkzipClassicManaged();
         this.OnKeysRequired(entry.Name);
         if (!this.HaveKeys)
         {
             throw new ZipException("No password available for encrypted stream");
         }
         classicCryptoStream = new CryptoStream(baseStream, managed.CreateDecryptor(this.key, null), CryptoStreamMode.Read);
         CheckClassicPassword(classicCryptoStream, entry);
         return classicCryptoStream;
     }
     if (entry.Version != 0x33)
     {
         throw new ZipException("Decryption method not supported");
     }
     this.OnKeysRequired(entry.Name);
     if (!this.HaveKeys)
     {
         throw new ZipException("No password available for AES encrypted stream");
     }
     int aESSaltLen = entry.AESSaltLen;
     byte[] buffer = new byte[aESSaltLen];
     int num2 = baseStream.Read(buffer, 0, aESSaltLen);
     if (num2 != aESSaltLen)
     {
         throw new ZipException(string.Concat(new object[] { "AES Salt expected ", aESSaltLen, " got ", num2 }));
     }
     byte[] buffer2 = new byte[2];
     baseStream.Read(buffer2, 0, 2);
     int blockSize = entry.AESKeySize / 8;
     ZipAESTransform transform = new ZipAESTransform(this.rawPassword_, buffer, blockSize, false);
     byte[] pwdVerifier = transform.PwdVerifier;
     if ((pwdVerifier[0] != buffer2[0]) || (pwdVerifier[1] != buffer2[1]))
     {
         throw new Exception("Invalid password for AES");
     }
     return new ZipAESStream(baseStream, transform, CryptoStreamMode.Read);
 }