The MAC does not match

Mar 12, 2012 at 6:11 PM

Hello!

I have the next code but i recived the next issue:

"The MAC does not match"

Thank you for your help!!

Code:
public static void UnZipFile(string DirOr, string DirDest, string Pass)

       {
           using (var zip = ZipFile.Read(DirOr))

         {             foreach (ZipEntry e in zip)

            {                e.ExtractWithPassword(DirDest, Pass); 

                        }     

  }     

 }

Jan 16, 2013 at 2:26 PM

Same problem with 1.9.1.8 - "The MAC does not match"


I need a fix for this, otherwise this DotNETZip Library is completely unusable for me. I need to programmatically split large data files.

 

Is there a way for me to get my hands on the source code, debug it and fix it for everyone? I'm new to the whole CodePlex / open source thing... but I do have 20 years MS software development experience and am sure I could fix it in a flash... Any pointers would be appreciated. I don't want to spend weeks on this as I have a job. But if I can get involved in an evening or 2 of my spare time... then I'd be interested.

Jan 17, 2013 at 1:28 AM

 

OK... once I downloaded the source, downloaded WiX, Silverlight developer stuff!! and got it running, it took me a little while to step through the code. But I got there in the end...

 

Basically on line 946 of ZipEntry.Extract.cs (in the ZipDLL project), in a method named VerifyCrcAfterExtract, there is a call to WinZipAesCipherStream.ReadAndVerifyMac

 

The ReadAndVerifyMac method assumes the Zip file stream we have been reading from is positioned at the end of the encrypted data for the last file that has been read out (and decrypted and inflated most probably).

In my case, where I am only extracting one file from the Zip archive, the file stream was disposed of once the file data was readout. So the call below to ReadAndVerifyMac actually creates a whole new file stream that is positioned at the beginning of the file - and as you'd expect the call to ReadAndVerifyMac reads the completely wrong data from the file and fails.

WinZipAesCipherStream wzs = _inputDecryptorStream as WinZipAesCipherStream;

_aesCrypto_forExtract.CalculatedMac = wzs.FinalAuthentication;
_aesCrypto_forExtract.ReadAndVerifyMac(this.ArchiveStream); // throws if MAC is bad

The call above to this.ArchiveStream recreates the file stream from scratch, as it has already been disposed of.

A quick and dirty work around is to seek to the correct position in the file by executing the following line of code before the call to ReadAndVerifyMac -

this.ArchiveStream.Position = __FileDataPosition + _CompressedFileDataSize;

but a much better solution, avoiding the need to open a new file stream, is to read out and store the MAC from the file after the last encrypted/compressed data is read, but before the file stream is disposed of. For me that would be done in a method called

private Int32 ExtractOne(Stream output)

in the file ZipEntry.Extract.cs, just before the end of the try block at line 1073.................

 

Anyway, I have my fix and only have to worry about my own bugs now!