2
votes

What is the best approach for encrypting values in CoreData?

Why do I need to encrypt the data when Apple is already encrypting the App Container? I want to sync the data to iCloud. Data in iCloud is also encrypted but it is not end to end encrypted. [iCloud Security].
Apple encrypts the data with their own key. CoreData internally uses SQLite which stores the database in plaintext. If this data is synced with iCloud, It is still vulnerable before being encrypted by Apple's own key. What is the possible way to keep the data in iCloud also encrypted without worrying about Apple's own encryption?

One of the Approach I have tried was using ValueTransformers:
I have tried using ValueTransformers to encrypt fields such as DateOfBirth and user Passwords. Now since the information is written in Binary into the SQLite database it solves one of the problem. but the problem with ValueTransformer is the attribute's dataType becomes Transformable and I am unable to use NSPredicate on that field. For example I can't search users born in certain year. Looks like NSPredicate doesn't work with transformable data since it is stored as binary data in the CoreData SQL.

Is it possible to encrypt the data before being sync to the iCloud and decrypt it back when receiving back from the iCloud? This year at WWDC 2019 Apple announced support for iCloud with CloudKit. Is there any way to do the encryption before the data is being synced with CloudKit? Any other resources will be helpful.

1
Hey I am facing Problem. That transformable attributes does not sync with iCloud stackoverflow.com/questions/61276524/…. Please look on thisShourob Datta

1 Answers

1
votes

I believe you need to encrypt your data before writing it to CoreData.

In iOS 13 Apple introduced CryptoKit which can really help with this. There is also a sample project. It is extremely fast and unless you have very significant data it won't affect performance in my experience.

The high-level approach is to generate a key and store it in the keychain and then use the key to encrypt and decrypt the data.