How to encrypt files in iPhone (PDF files)
i am developing a ebook reader and sending the ipa files to various people. The ipa files contains some PDF books. Is there any way in which i can encrypt the PDf files so that the user can see them 开发者_运维知识库only on device and not on PC... thanku
That's a general security question and the answer is: No. If one device (the iPhone) can decrypt the files without further data like a password or secret device key, another device (a desktop computer) can do this as well.
All you can do is obfuscate the files. That would keep people from simply unzipping the ipa and opening the PDFs. But any measure you take makes it only a little more difficult to access the files. There's no way to make it impossible for a skilled person to get at the data.
You could re-save the bundle pdf's using data protection, its not bulletproof but it makes is difficult to read the data (especially if the passcode is unknown), however it only works if the devices are pass-coded.
//There is probobly a quicker way to do this..ie..iterating the bundle programatically for pdf's
NSMutableArray * a = [[NSMutableArray alloc] init];
[a addObject:[NSString stringWithFormat:@"pdf1.pdf"]];
[a addObject:[NSString stringWithFormat:@"pdf2.pdf"]];
[a addObject:[NSString stringWithFormat:@"pdf3.pdf"]];
[self resaveFilesWithProtection:a];
[a release];
-(void)resaveFilesWithProtection:(NSArray*)fileNameArray
{
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * DocPath = [paths objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
for (NSString* s in fileNameArray) {
NSString * fullFilepath = [DocPath stringByAppendingPathComponent:s];//getting path to file
NSData *myData = [NSData dataWithContentsOfFile:filePath];//getting data out of old file
[fileManager removeItemAtPath:fullFilepath error:NULL];//deleting old file
NSError*er=nil;
[myData writeToFile:fullFilepath options:NSDataWritingFileProtectionComplete error:&er]; //saving back to disk with protection
}
NSLog(@"DONE");
}
You could also store the NSData in an SQLite database, or obscure the file extensions by re-saving them as .anything.
Edit:
If you don't want the user to be able to unzip the ipa and you think that extension obscuring isn't enough then your going to have to not put the pdf's in the bundle and pull them down from the network.
精彩评论