/// <summary> /// Constructor. /// </summary> /// <param name="key">Password string</param> /// <param name="saltBytes">Random bytes, length depends on encryption strength. /// 128 bits = 8 bytes, 192 bits = 12 bytes, 256 bits = 16 bytes.</param> /// <param name="blockSize">The encryption strength, in bytes eg 16 for 128 bits.</param> /// <param name="writeMode">True when creating a zip, false when reading. For the AuthCode.</param> /// public ZipAESTransform(string key, byte[] saltBytes, int blockSize, bool writeMode) { if (blockSize != 16 && blockSize != 32) // 24 valid for AES but not supported by Winzip { throw new Exception("Invalid blocksize " + blockSize + ". Must be 16 or 32."); } if (saltBytes.Length != blockSize / 2) { throw new Exception("Invalid salt len. Must be " + blockSize / 2 + " for blocksize " + blockSize); } // initialise the encryption buffer and buffer pos _blockSize = blockSize; _encryptBuffer = new byte[_blockSize]; _encrPos = ENCRYPT_BLOCK; // Performs the equivalent of derive_key in Dr Brian Gladman's pwd2key.c var pdb = new Rfc2898DeriveBytes(key, saltBytes, KEY_ROUNDS); var rm = new RijndaelImplementation(); rm.Mode = CipherMode.ECB; // No feedback from cipher for CTR mode _counterNonce = new byte[_blockSize]; byte[] byteKey1 = pdb.GetBytes(_blockSize); byte[] byteKey2 = pdb.GetBytes(_blockSize); _encryptor = rm.CreateEncryptor(byteKey1, byteKey2); _pwdVerifier = pdb.GetBytes(PWD_VER_LENGTH); // #if !OS_WINDOWS incrementalHash = IncrementalHash.CreateHMAC(HashAlgorithmName.SHA1, byteKey2); #else _hmacsha1 = new HMACSHA1(byteKey2); #endif _writeMode = writeMode; }
/// <summary> /// Constructor. /// </summary> /// <param name="key">Password string</param> /// <param name="saltBytes">Random bytes, length depends on encryption strength. /// 128 bits = 8 bytes, 192 bits = 12 bytes, 256 bits = 16 bytes.</param> /// <param name="blockSize">The encryption strength, in bytes eg 16 for 128 bits.</param> /// <param name="writeMode">True when creating a zip, false when reading. For the AuthCode.</param> /// public ZipAESTransform(string key, byte[] saltBytes, int blockSize, bool writeMode) { if (blockSize!=16&&blockSize!=32) // 24 valid for AES but not supported by Winzip throw new Exception("Invalid blocksize "+blockSize+". Must be 16 or 32."); if (saltBytes.Length!=blockSize/2) throw new Exception("Invalid salt len. Must be "+blockSize/2+" for blocksize "+blockSize); // initialise the encryption buffer and buffer pos _blockSize=blockSize; _encryptBuffer=new byte[_blockSize]; _encrPos=ENCRYPT_BLOCK; // Performs the equivalent of derive_key in Dr Brian Gladman's pwd2key.c var pdb=new Rfc2898DeriveBytes(key, saltBytes, KEY_ROUNDS); var rm=new RijndaelImplementation(); rm.Mode=CipherMode.ECB; // No feedback from cipher for CTR mode _counterNonce=new byte[_blockSize]; byte[] byteKey1=pdb.GetBytes(_blockSize); byte[] byteKey2=pdb.GetBytes(_blockSize); _encryptor=rm.CreateEncryptor(byteKey1, byteKey2); _pwdVerifier=pdb.GetBytes(PWD_VER_LENGTH); // #if !OS_WINDOWS incrementalHash = IncrementalHash.CreateHMAC(HashAlgorithmName.SHA1, byteKey2); #else _hmacsha1=new HMACSHA1(byteKey2); #endif _writeMode=writeMode; }