Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I encrypt CoreData contents on an iPhone

I have some information I'd like to store statically encrypted on an iPhone application. I'm new to iPhone development, some I'm not terribly familiar with CoreData and how it integrates with the views. I have the data as JSON, though I can easily put it into a SQLITE3 database or any other backing data format. I'll take whatever is easiest (a) to encrypt and (b) to integrate with the iPhone view layer.

The user will need to enter the password to decrypt the data each time the app is launched. The purpose of the encryption is to keep the data from being accessible if the user loses the phone.

For speed reasons, I would prefer to encrypt and decrypt the entire file at once rather than encrypting each individual field in each row of the database.

Note: this isn't the same idea as Question 929744, in which the purpose is to keep the user from messing with or seeing the data. The data should be perfectly transparent when in use.

Also note: I'm willing to use SQLCipher to store the data, but would prefer to use things that already exist on the iPhone/CoreData framework rather than go through the lengthy build/integration process involved.

like image 693
James A. Rosen Avatar asked Oct 29 '09 16:10

James A. Rosen


People also ask

Can you encrypt data on iPhone?

Yes. Apple's iPhone, iPod touch, and iPad smart devices all support basic built-in encryption while a passcode is enabled. Macs also support their own form of data encryption. The encryption on Apple's iOS and iPadOS devices, such as the iPhone, iPod touch, and iPad, is called Data Protection.

How do you encrypt messages on iPhone?

Enable message encryptionOpen the Settings app. Choose Mail > Accounts. Select the account that has messages that you want to encrypt by default. Choose Account > Advanced > Encrypt by Default, then turn on Encrypt by Default.

How do I encrypt sensitive data?

The two most widely used methods for data encryption are public key, also known as asymmetric encryption and private key, or symmetric encryption. Both rely on key pairs, but they differ in the way the sending and receiving parties share the keys and handle the encrypt/decrypt process.


2 Answers

You can encrypt individual properties in your Core Data model entities by making them transformable properties, then creating an NSValueTransformer subclass which will encrypt and decrypt the data for that property. While this is not the whole-database decryption that you're looking for, it will have a much lower memory footprint than decrypting an entire database into memory. Additionally, it will allow the decryption to be done lazily, rather than all up front, so your application will load much faster. Depending on the encryption used, I would even expect that the on-disk data accesses for loading each entity would be slower than the decryption process for the properties, so you won't see that much of a performance penalty when accessing the properties.

Transformable properties like this are very easy to use, because you read and write to them as normal, while the encryption / decryption goes on behind the scenes.

like image 134
Brad Larson Avatar answered Oct 23 '22 00:10

Brad Larson


Do you need to encrypt? Newer iPhones (3Gs, 4, iPad...) encrypt all data on the device. With a single, hashed, salted password on your app, no one can get to the data without a password. Data is sandboxed from all other apps.

Data Protection on iOS

like image 23
Tom Andersen Avatar answered Oct 23 '22 02:10

Tom Andersen