Can anyone help me to make the contents of my Documents directory secure?
5 Answers
Use:
- (BOOL)writeToFile:(NSString *)path options:(NSDataWritingOptions)mask error:(NSError **)errorPtr
with one of the file protection options:
NSDataWritingFileProtectionComplete (iOS 4.0)
NSDataWritingFileProtectionCompleteUnlessOpen (iOS 5.0)
NSDataWritingFileProtectionCompleteUntilFirstUserAuthentication (iOS 5.0)
See: Apple Documentation
NSDataWritingFileProtectionComplete
In this case, the file is stored in an encrypted format and may be read from or written to only while the device is unlocked. At all other times, attempts to read and write the file result in failure.
Note: Doing your own encryption raises the problem of key storage and the best answer there is to use the Keychain. Key handling is the biggest problem and the NSData methods handle that.

- 111,848
- 21
- 189
- 228
-
I am wirte .jpg file to documents directory. still i am able to open it. using above your suggestion.. can you give me some sample code? – Jatin Patel - JP Sep 20 '11 at 07:05
-
How do you want to control access, what are you trying to accomplish by encrypting? I i added a description to the NSDataWritingFileProtectionComplete key. – zaph Sep 20 '11 at 11:32
-
I want process so the contents of documents directory will hide or store in encryption format so no buddy read it.this all things need to do using programming. is it possible? Thanks – Jatin Patel - JP Sep 21 '11 at 09:59
-
YOu really need to think through and write down exactly what you are protecting and from what kind of access. With `NSDataWritingFileProtectionComplete` only your program can read the file. TO go above that would require the used to enter a password for each use, probably not what you want. Encrypting and hard coding the encryption key in you program is a possibility but reduces the effectiveness--which may be OK, you have to decide what kind of attacks you want to protect against. – zaph Sep 21 '11 at 10:43
We cannot secure the file in documents directory. We can store the file in temp folder of the device. This cannot be accessed by anyone

- 29
- 3
use encryption and decryption for making and reading those files on iphone take an idea from here http://iphonedevelopment.blogspot.com/2009/02/strong-encryption-for-cocoa-cocoa-touch.html

- 2,301
- 5
- 31
- 54
Alan Quatermain provides a toolkit that has some helpful wrappers around the cryptography libraries to make encryption/decryption really straightforward.
Here's the link:
Whatever you do, just make sure that you don't store the document on the device unencrypted, even for a short time. Always store it encrypted. Perform any encryption operation in memory.
Be sure that for any file operations you do not cache. So for example, any downloading, etc. you want to make sure that no data is written temporarily to disk.
Finally, for your encrypted documents, do not store the key on the device in any format. Do not store it on the keychain either.

- 26,115
- 13
- 104
- 132