adamhathcock / sharpcompress

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

Extract rar with invalid password does not raise exception #25

Open JJWR opened 9 years ago

JJWR commented 9 years ago

Hello, when I extract a rar file with password everything works as expected if the password is ok. However, when the password is wrong the extraction runs without raising any exception and the files are extracted. Of course the files are corrupted. This is the code I am using:

string password = "wrongpass";
string inpath = @"D:\Test\File_with_pass.rar";
RarArchive rar = RarArchive.Open(inpath, SharpCompress.Common.Options.LookForHeader, password);
rar.WriteToDirectory(@"D:\Test\out");
rar.Dispose();

Since we can not ensure that the extraction went well, this issue is making the library useless for rar protected files.

Strachu commented 9 years ago

I am not sure whether there is a better way to do it nor if it can be done without making some modification to the library, but maybe extracting some small file to memory, calculating CRC for extracted content and comparing it with CRC from header will be enough? If CRCs are equal == password is correct. If CRCs are different == incorrect password or damaged archive.

adamhathcock commented 9 years ago

CRC is the only way to check. It's unfortunate but I don't think there's anyway to throw an exception or something unless there's an encrypted header to check.

JJWR commented 9 years ago

CRC should anyway always be checked even if the file is not password protected to ensure that the file is all right. Is is not checked now?

adamhathcock commented 9 years ago

I really haven't done much with CRC checking. I don't believe any of it is done currently.

ArunaVignesh commented 3 years ago

@adamhathcock

When extracting rar file protected with password, files extracted done successfully when password is correct. how do i check the password correct or not? Tried above @Strachu CRC checking method. its not worked correctly in my file. @Strachu can you please explain it elaborately. If anyone knows some other method, please help