adamhathcock / sharpcompress

SharpCompress is a fully managed C# library to deal with many compression types and formats.
MIT License
2.27k stars 480 forks source link

Can't extract encrypted 7z LZMA2 files #208

Open ciriousjoker opened 7 years ago

ciriousjoker commented 7 years ago

I have an encrypted 7zip archive (linked below) encrypted with the password "dontgoout", but whenever I try to extract it, it throws an error at MoveToNextEntry.

Here's a screenshot of the stack trace: https://gyazo.com/6fb0b0c0731d6e23d04a26e301cc2dbe

I created the archive using the 7zip client for Windows using these settings: https://gyazo.com/ca4e67e6a259d79cdf0675dc42fd8268

Original name was 0.tmp, but Github only allows zip files. 0.tmp.encrypted.zip

Extracting the same archive without any password works perfectly fine and I can extract the encrypted 7z archive using the 7zip desktop client, so this has to be a bug in the library (Or I'm just too dumb for life and I missed something I should have done)

Here's the code I used to extract that archive:

SharpCompress.Readers.ReaderOptions test = new SharpCompress.Readers.ReaderOptions();
test.LookForHeader = true;
test.LeaveStreamOpen = false;
test.Password = "dontgoout";

var archive = SevenZipArchive.Open(filename, test);

if (!archive.IsComplete)
{
    MessageBox.Show("One of the downloaded resources is corrupt, please try again.");
    return;
}

long totalbytes = archive.TotalUncompressSize;
long processedbytes = 0;

var reader = archive.ExtractAllEntries();

while (reader.MoveToNextEntry()) // <-- Here it crashes
{
    if (!reader.Entry.IsDirectory)
    {
adamhathcock commented 7 years ago

I was about to say "sharpcompress doesn't support encrypted 7zip files" but honestly I can't remember.

However, the error you got is fixable because it's using a stream that doesn't support seeking though 7zip requires seeking.

ciriousjoker commented 7 years ago

But if the library requires seekable streams (which totally makes sense the way 7z works), why did it work with unencrypted files? I mean they'd have to be seekable too, right?

ciriousjoker commented 7 years ago

Woops

adamhathcock commented 7 years ago

It must be that the decryption code just needs to know the length for some reason and it's using a different stream class.

The class with the error is one I wrote for the purpose of ensuring no seekable stream was used in other parts of the code.

I haven't done much with the 7zip code personally. I really hate the 7zip archive format.

ciriousjoker commented 7 years ago

Can you point me in the right direction please? I tried using your API examples, but they only seem to work with zip archives ..

(Aka replacing var archive = ...; with var archive = ArchiveFactory.Open(...); doesn't work either)

SevenZipArchive.Open(new FileStream(...)); also didn't work

adamhathcock commented 7 years ago

It's not going to work without a code change. You're going to get the exception you posted always with that encrypted 7zip file

ciriousjoker commented 7 years ago

So I'd have to fix your library? Or are you saying that my encrypted 7zip file is broken?

By the way, no matter what 7zip file I use (encrypted or not doesn't matter), I get this weird thing in the locals tab:

https://gyazo.com/363d6b37b9dd0ff79df69d602d04fa52

Not sure if it has something to do with this error, but it always throws that error. However, with unencrypted 7z files it still extracts the whole thing

adamhathcock commented 7 years ago

I have a feeling encrypted 7zip just isn't supported. I don't recall putting that in the supported formats matrix

ciriousjoker commented 7 years ago

Ye, I just assumed that encrypted 7zips wouldn't make a difference to the 7z.dll ...

Anyway, I "fixed" it now by encrypting the final 7z with AES. For my purposes that's acceptable

adamhathcock commented 7 years ago

This code doesn't used the 7z dll. That's why he behavior is different than other libraries as others do use the native dll.