Frage

I have an iOS application, which stores all downloaded *.pdf files in its cache. Is there a way to prevent this data from extracting? Encryption or something else? Thanks in advance.

War es hilfreich?

Lösung

You can protect PDF files with a password. I assume you create the PDF files not within the application but externally. For example you can use Preview.app in Mac OS X to secure existing PDF files with a password (Hit Cmd-P, then select PDF in the print menu and there you can set security options. Or even more simple: in the menu choose Export...).

In iOS you can then open the PDF files like this:

CGPDFDocumentRef documentRef = CGPDFDocumentCreateWithURL((__bridge CFURLRef)[NSURL fileURLWithPath:filePath]);
if (!CGPDFDocumentIsUnlocked(documentRef))
    CGPDFDocumentUnlockWithPassword(documentRef, password);
...

Andere Tipps

There are quite a few ways to encrypt files, and I'm sure everyone will have an opinion on the best way to do so.

In a project I've recently been working on, we've been using CommonCrypto (https://github.com/AlanQuatermain/aqtoolkit). Just take any NSData, encrypt it, and save it to a file, and vice versa. You can even write an easy Transformer by subclassing NSValueTransformer, which abstracts all of the encryption to one spot and you will never have to worry about it again.

There are actually 2 Documents folders in which your app can store content. One can be extracted, and one is private. Check the accepted answer in this ticket.

Access files in "private Documents" folder transferred with iTunes

Assuming you want the PDF files from getting extracted on jailbroken devices, the most straight forward approach would be along the following lines:

  • generate a random string during the first launch of the app
  • save the random string either in NSUserDefaults in state file inside your own app's sandbox
  • using this random string create a secret key using a deterministic but hard to figure out algorithm
  • use this secret key, which you don't store anywhere but always generate on demand, symmetrically encrypt your buffer with AES or something similar

You would probably find the source code here very helpful.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top