/// <summary> /// Initializes encryption keys based on given <paramref name="password"/>. /// </summary> /// <param name="password">The password.</param> protected void InitializePassword(string password) { PkblubbClassicManaged pkManaged = new PkblubbClassicManaged(); byte[] key = PkblubbClassic.GenerateKeys(BlubbZipConstants.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 BlubbZipException("Library cannot extract this entry. Version required is (" + entry.Version.ToString() + ")"); } // Handle encryption if required. if (entry.IsCrypted) { if (password == null) { throw new BlubbZipException("No password set."); } // Generate and set crypto transform... PkblubbClassicManaged managed = new PkblubbClassicManaged(); byte[] key = PkblubbClassic.GenerateKeys(BlubbZipConstants.ConvertToArray(password)); inputBuffer.CryptoTransform = managed.CreateDecryptor(key, null); byte[] cryptbuffer = new byte[BlubbZipConstants.CryptoHeaderSize]; inputBuffer.ReadClearTextBuffer(cryptbuffer, 0, BlubbZipConstants.CryptoHeaderSize); if (cryptbuffer[BlubbZipConstants.CryptoHeaderSize - 1] != entry.CryptoCheckValue) { throw new BlubbZipException("Invalid password"); } if (csize >= BlubbZipConstants.CryptoHeaderSize) { csize -= BlubbZipConstants.CryptoHeaderSize; } else if ((entry.Flags & (int)GeneralBitFlags.Descriptor) == 0) { throw new BlubbZipException(string.Format("Entry compressed size {0} too small for encryption", csize)); } } else { inputBuffer.CryptoTransform = null; } 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); } }