project-imas / encrypted-core-data

v2.0 - iOS Core Data encrypted SQLite store using SQLCipher
Other
785 stars 236 forks source link

Migration Old database to Encrypted Core Data base. #293

Open xpd54 opened 6 years ago

xpd54 commented 6 years ago

Hi, I am trying to use Encrypted Core Data in one of our existing project. In Our existing production app we have few MBs of data, right now we are trying to migrate our existing database to encrypted database.

Following are the steps for Demo Project.

  1. Install SQLcipher to to create another encrypted database (copy of existing sqlite file ). (https://www.zetetic.net/sqlcipher/ios-tutorial/)
  2. Set encryption key for sqlcipher.
  3. Point persistentStoreCoordinator to the encrypted database.
  4. Set encryption key for EncryptedStore (encrypted core data) so it can read data from the new encrypted store.

Problem we are facing

After pointing to the encrypted database it seems it’s reading from different tables.

Reading from

2018-01-05 20:17:44.029262+0800 test[61445:4402622] SQL DEBUG: BEGIN EXCLUSIVE; 2018-01-05 20:17:44.029499+0800 test[61445:4402622] SQL DEBUG: PRAGMA case_sensitive_like = true; 2018-01-05 20:17:44.029768+0800 test[61445:4402622] SQL DEBUG: SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='meta'; 2018-01-05 20:17:44.029993+0800 test[61445:4402622] SQL DEBUG: CREATE TABLE meta(plist); 2018-01-05 20:17:44.030798+0800 test[61445:4402622] SQL DEBUG: CREATE TABLE ecdUser ('__objectid' integer primary key, 'admin', 'age', 'name'); 2018-01-05 20:17:44.031233+0800 test[61445:4402622] SQL DEBUG: DELETE FROM meta; 2018-01-05 20:17:44.031349+0800 test[61445:4402622] SQL DEBUG: INSERT INTO meta (plist) VALUES(?);

Written In CoreData: sql: INSERT INTO ZUSER(Z_PK, Z_ENT, Z_OPT, ZADMIN, ZAGE, ZNAME) VALUES(?, ?, ?, ?, ?, ?) CoreData: sql: INSERT INTO ZUSER(Z_PK, Z_ENT, Z_OPT, ZADMIN, ZAGE, ZNAME) VALUES(?, ?, ?, ?, ?, ?) CoreData: sql: INSERT INTO ZUSER(Z_PK, Z_ENT, Z_OPT, ZADMIN, ZAGE, ZNAME) VALUES(?, ?, ?, ?, ?, ?) CoreData: sql: INSERT INTO ZUSER(Z_PK, Z_ENT, Z_OPT, ZADMIN, ZAGE, ZNAME) VALUES(?, ?, ?, ?, ?, ?) CoreData: sql: INSERT INTO ZUSER(Z_PK, Z_ENT, Z_OPT, ZADMIN, ZAGE, ZNAME) VALUES(?, ?, ?, ?, ?, ?)

You can see it’s written in different table but it’s reading from some different table Which is appended by ecd

Please let me know if I am doing something wrong here.

Or If you have a way how to Use Encrypted Core Data in the already existing project. (How to do migration from old data to new)

Izulle commented 6 years ago

Encrypted Core Data generates table and column names which are different from ones generated by Core Data. Looks like you have to write you own migration.

longfanshotmailcom commented 4 years ago

Hi, I'm facing the same problem. I tried both encrypted core data and also command line (sqlcipher only), both methods generated new tables like ecdUsers and ZUSERS.

To answer sjlombardo's question, encrypted.db not exists when I run export.

longfanshotmailcom commented 4 years ago

By the way, the original db file contains only ZUSERS, the new ecdUsers table has no record, and sqlcipher is trying to read from ecdUsers so no result can be fetched.

longfanshotmailcom commented 4 years ago

I also created a new Xcode project with nothing but a core data setup. I can reproduce the issue so it's not because my existing project.