How to check if mail file is protected in iOS?

I use ZipArchive to extract zip files in an iOS application, but I want to know before opening a file if it is password protected or not so that I can pass the password to the UnZipOpenFile function.

+4
source share
4 answers

the password of the zip file is not an entry in the header; it is written in separate files in the zip

so you need to check all files in zip

add this feature to ZipArchive

-(BOOL) UnzipIsEncrypted { int ret = unzGoToFirstFile( _unzFile ); if (ret == UNZ_OK) { do { ret = unzOpenCurrentFile( _unzFile ); if( ret!=UNZ_OK ) { return NO; } unz_file_info fileInfo ={0}; ret = unzGetCurrentFileInfo(_unzFile, &fileInfo, NULL, 0, NULL, 0, NULL, 0); if (ret!= UNZ_OK) { return NO; } else if((fileInfo.flag & 1) == 1) { return YES; } unzCloseCurrentFile( _unzFile ); ret = unzGoToNextFile( _unzFile ); } while( ret==UNZ_OK && UNZ_OK!=UNZ_END_OF_LIST_OF_FILE ); } return NO; } 
+4
source

I could not find a function in zipArchive that detects that the file is encrypted, so I checked the file header to see if the password is protected or not, as indicated in the following link:

http://secureartisan.wordpress.com/2008/11/04/analysis-of-encrypted-zip-files/

 -(BOOL) IsEncrypted:(NSString*)path { NSData* fileData = [NSData dataWithContentsOfFile:path]; NSData* generalBitFlag = [fileData subdataWithRange:NSMakeRange(6, 2)]; NSString* genralBitFlgStr = [generalBitFlag description]; if ([genralBitFlgStr characterAtIndex:2]!='0') { return true; } else { return false; } } 

thanks for all

+3
source

I myself have not used ZipArchive, but looking at the code, it can use the first version of UnzipOpenFile without a password argument and try to call UnzipFileTo . If this fails, you open again, but with a password and call UnzipFileTo again. The problem is that you cannot distinguish an invalid zip file and use an invalid password.

If you really need to know if the file is encrypted, you can probably add functionally yourself (unverified code):

Add this to unzip.c in minizip:

 extern int ZEXPORT unzIsEncrypted (file) unzFile file; { return ((unz_s*)file)->encrypted; } 

This is unzip.h value:

 extern int ZEXPORT unzIsEncrypted OF((unzFile file)); 

This is the value of ZipArchive.mm :

 - (BOOL)ZipIsEncrypted { return unzIsEncrypted(_unzFile); } 

This is the value of ZipArchive.h :

 - (BOOL)ZipIsEncrypted; 

And use it after calling UnzipFileTo .

+2
source

I unzipped over 50 MB of the encrypted file, so loading the full file into NSData was a problem for me. such a modified answer and I used this:

 -(BOOL) IsEncrypted:(NSString*)path { NSInteger chunkSize = 1024 //Read 1KB chunks. NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:path]; NSData *fileData = [handle readDataOfLength:chunkSize]; NSData* generalBitFlag = [fileData subdataWithRange:NSMakeRange(6, 2)]; NSString* genralBitFlgStr = [generalBitFlag description]; if ([genralBitFlgStr characterAtIndex:2]!='0') { return true; } else { return false; } } 
+1
source

Source: https://habr.com/ru/post/1416551/


All Articles