2013-02-27 68 views
1

我已经到了可以使用SQLCipher创建数据库的加密副本的地步,现在我正在尝试将它集成到我的项目中。我用我的应用程序代理下面的代码来解密方法数据库尝试...在持久性存储中使用SQLCipher数据库

NSString *databasePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES) objectAtIndex:0] 
          stringByAppendingPathComponent: @"encrypted.db"]; 

if (sqlite3_open([databasePath UTF8String], &db) == SQLITE_OK) { 
    const char* key = [@"BIGSecret" UTF8String]; 
    sqlite3_key(db, key, strlen(key)); 
    if (sqlite3_exec(db, (const char*) "SELECT count(*) FROM sqlite_master;", NULL, NULL, NULL) == SQLITE_OK) { 
     // password is correct, or, database has been initialized 
     NSLog(@"correct password"); 

    } else { 
     // incorrect password! 
     NSLog(@"incorrect password"); 
    } 

再后来在持久性存储,我使用下面的代码。

if (__persistentStoreCoordinator != nil) { 
    return __persistentStoreCoordinator; 
} 

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"encrypted.db"]; 


NSError *error = nil; 
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; 
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) { 

我第一次创建数据库之后加载程序,我会得到一个“正确的密码”登录,但之后的任何时间,我得到一个“密码不正确”,但数据库仍然是可用的,这导致我相信数据库正在被覆盖或什么。

+0

好的,这绝对是在持久性商店协调员(PSC)中出现混乱,如果我在PSC之前插入代码来打开数据库,那么如果我在代码行之后立即输入它, PSC的定义,它给出了不正确的密码日志 – 2013-02-27 03:48:02

+0

好吧,现在我倾向于尝试像这样的数据库实施经理级: https://github.com/sqlcipher/SQLCipherManager 除非有一个更简单的方法来修复持久存储协调器。 – 2013-02-27 04:13:21

回答

3

CoreData不直接与SQLCipher一起工作,因为它直接从设备使用SQLite。您可以查看加密核心数据项目(https://github.com/project-imas/encrypted-core-data),该项目使用SQLCipher和自定义NSIncrementalStore提供类似的功能。

相关问题