Reputation: 167
I'm storing some sensitive information like passwords etc. in Core Data. I want that my app encrypts the entire SQLite database (it's not very big, < 1 MB) whenever the apps goes into the background or terminates. I figured out the encryption thing itself, but I'm having problems to correctly 'close' and re-open the Core Data stack with the store.
When my apps terminates/goes into the background I do this now:
When my app comes back, I do the following:
From what I understood from the docs this should be sufficient, but it doesn't work, as soon as the main view controller tries to do a fetch on the context again my app crashes.
Does anybody know of what is the best way to temporarily remove a store from core data and then add it again?
Upvotes: 0
Views: 194
Reputation: 17014
This isn't an answer to your question, but worth saying:
Your encryption strategy is fatally flawed and I seriously recommend that you think up a different scheme.
If data should be encrypted on disk, and is actually worth encrypting, it should never ever be written to disk in a unencrypted state, for these reasons:
If the app forcefully quits, or power is removed from the device, the file remains unencrypted on the disk. And then someone could go poking around the disk and find the unencrypted data.
Deleting the plain version of the store file is in all likelihood not securely deleting the file, and hence disk analysis/tools could find an unencrypted version of the file.
If you want to encrypt your data when it is written to disk, you have to write encrypted data to disk every time, no exceptions. "It's encrypted some of the time" is no use.
As an alternative strategy, you might want to consider encrypting the data that is stored in core data itself. For example, you might NSArchive your data and encrypt the resulting data bytes before storing them as BLOBs.
Upvotes: 1