【问题标题】:Using an SQLCipher database in a persistent store在持久存储中使用 SQLCipher 数据库
【发布时间】:2013-02-27 03:23:37
【问题描述】:

我已经到了可以使用 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]) {

创建数据库后第一次加载程序时,我会得到一个“密码正确”的日志,但之后的任何时候我都会得到一个“密码错误”,但数据库仍然可用,这让我相信数据库被覆盖什么的。

【问题讨论】:

  • 好吧,如果我在 PSC 工作之前插入代码以打开数据库,那肯定是在持久存储协调器 (PSC) 处,如果我在此之后立即输入它定义 PSC 的代码行,它给出了不正确的密码日志
  • 好吧,现在我倾向于尝试为数据库实现一个像这样的管理器类:github.com/sqlcipher/SQLCipherManager,除非有更简单的方法来修复持久存储协调器搞砸的事情。

标签: objective-c xcode sqlcipher


【解决方案1】:

CoreData 不能直接与 SQLCipher 一起使用,因为它直接使用设备中的 SQLite。您可以查看 Encrypted Core Data 项目 (https://github.com/project-imas/encrypted-core-data),它使用 SQLCipher 和自定义 NSIncrementalStore 来提供类似的功能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2014-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多