FMDB and encryption

I use FMDB to work with sqlite, and I would rather avoid dependency on SQLCipher. How can I just use the DataProtection features built into iOS? This is possible - the only requirement is to protect data in the event of a phone theft.

If the phone is unlocked with a PIN code, it is wonderful that the user can access the database - this is their data.

+4
source share
2 answers

Find the line where you are running databaseWithPath: (or initWithPath: , then add:

 FMDatabase *db = [FMDatabase databaseWithPath:path]; NSDictionary *attributes = @{NSFileProtectionKey: NSFileProtectionCompleteUnlessOpen}; NSError *error; BOOL success = [[NSFileManager defaultManager] setAttributes:attributes ofItemAtPath:path error:&error]; if (!success) { NSLog(@"File protection failed: %@", error); } 

possible values for the NSFileProtectionKey key:

  • NSFileProtectionNone : The file does not have specific protections associated with it. It can be read or written at any time.
  • NSFileProtectionComplete : The file is stored in an encrypted format on disk and cannot be read or written while the device is locked or loaded.
  • NSFileProtectionCompleteUnlessOpen : The file is encrypted on disk. Files can be created when the device is locked, but after closing it cannot be opened again until the device is unlocked. If the file opens when unlocking, you can continue to access the file in normal mode, even if the user locks the device. There is a slight performance limitation when creating and opening a file, although not when writing or reading. This can be mitigated by changing the file protection to NSFileProtectionComplete when the device is unlocked.
  • NSFileProtectionCompleteUntilFirstUserAuthentication : The file is encrypted on disk and cannot be accessed until the device boots. After the user first opens the device, your application can access the file and continue to access it, even if the user subsequently locks the device.

The correct type of protection may depend on the version of iOS (the last two are not available on iOS 4) and whether you use your database when the device is locked.

+5
source

The easiest way is to enable data protection for the entire application. Go to Application Names , click "Change" and set "Sharing and Permissions" to "Full Protection".

enter image description here

Update Xcode with information about your new application identifier, and from there it will be automatically processed for your application.

+3
source

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


All Articles