繁体   English   中英

在持久性存储中使用SQLCipher数据库

[英]Using an SQLCipher database in a persistent store

我已经到了能用SQLCipher创建数据库加密副本的地步,现在我正在尝试将它集成到我的项目中。 我尝试在我的app委托中使用以下代码来解密数据库...

   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]) {

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM