2

I've been having some trouble figuring out how to encrypt my sqlite database. I'm using core data and this following project:

https://cocoapods.org/?q=EncryptedCoreData

What I can't figure out is how I am suppose to use this project to encrypt my database. I've already install the project and I can import the library EncryptedCoreData. However I don't find any information regarding a pratical example with swift. In my appdelegate I have the following code

import UIKit
//import CoreData
//import SQLCipher
import EncryptedCoreData


lazy var persistentContainer: NSPersistentContainer = {
    // my attempt to initialize the container
    let modelURL = Bundle.main.url(forResource: "DbModel", withExtension: "momd")!
    var coordinator = NSPersistentStoreCoordinator.init(managedObjectModel: NSManagedObjectModel(contentsOf: modelURL)!)

    //originaly its
    let container = NSPersistentContainer(name: "DbModel")
    container.loadPersistentStores(completionHandler: { (storeDescription, error) in
        if let error = error as NSError? {
            fatalError("Unresolved error \(error), \(error.userInfo)")
        }
    })
    return container
}()

Can someone provide an example on how I'am suppose to initialize the container?

1 Answers1

2

I translated the Objective-C to Swift and it worked, I just added this lines

let container = NSPersistentContainer(name: "DbModel")
// Begin of my code
let cOpts : NSDictionary = [
            EncryptedStore.optionPassphraseKey() : "123deOliveira4", //your Key
            EncryptedStore.optionFileManager() : EncryptedStoreFileManager.default()
        ]
let desc = try! EncryptedStore.makeDescription(options: cOpts as! [AnyHashable : Any], configuration: nil)
container.persistentStoreDescriptions = [desc]
//End
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
        if let error = error as NSError? {
            fatalError("Unresolved error \(error), \(error.userInfo)")
        }
})
Chirag Kothiya
  • 955
  • 5
  • 12
  • 28
D Saggin
  • 403
  • 5
  • 12
  • I did the same. But I need to know that after adding above lines in my appdelegate file, my data encryption will start automatically? or I have to add some extra code while saving data or in saveContext() method? – Ali Jan 02 '19 at 11:54
  • For what I remember the Data file will be encrypted by doing just this, you can check it by trying to open the file created by the simulator. If it requests the passphrase key it is encrypted. – D Saggin Jan 03 '19 at 12:31
  • https://stackoverflow.com/questions/24133022/sqlite-file-location-core-data – D Saggin Jan 03 '19 at 12:38
  • This code causes error `Error Domain=Foundation._GenericObjCError Code=0 "(null)` at: `let DelTable = NSBatchDeleteRequest(fetchRequest: NSFetchRequest(entityName: "MyTable")) do { try managedContext.execute(DelTable) print("all data deleted from entity: MyTable ") } catch { let nserror = error as NSError print(nserror) } ` – Ali Jan 03 '19 at 13:28
  • Although it did lock my *.sqlite file. I tried opening it in DB browser app, it asks me for a passphrase. – Ali Jan 03 '19 at 13:41
  • I think I am getting it. I added my encryption code in appdelegate file. So, it locks instantly when app is initialized hence making all core data methods unable to interact with coredata. – Ali Jan 03 '19 at 13:47
  • The BatchDelete it is not supported by this framework. https://github.com/project-imas/encrypted-core-data/issues/235 Other Methods should work fine – D Saggin Jan 03 '19 at 15:14