2012-01-18 55 views
1

我刚刚在我的应用中实现了SQLCipher来加密一个相当简单的数据库。我在this教程中仔细地遵循了所有设置说明,并且正在构建项目并且该应用程序正在成功运行。但是,当我使用他们的示例代码来加密我的数据库时,我的密码有些不正确,我现在无法打开我的数据库。以下是代码:SQLCipher工作但密码不正确

NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] 
         stringByAppendingPathComponent: @"dict.sqlite"]; 
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) { 
     const char* key = [@"BIGSecret" UTF8String]; 
     sqlite3_key(database, key, strlen(key)); 
     if (sqlite3_exec(database, (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 :("); 
     } 
    } 
    else { 
     sqlite3_close(database); 
     NSAssert1(NO, @"Failed to open database with message '%s'.", sqlite3_errmsg(database)); 
    } 
} 

sqlite3 *database;在我的界面中声明。我的应用程序崩溃在这条线:

if (sqlite3_prepare_v2(database, sql, -1, &init_statement, NULL) != SQLITE_OK) { 
    NSAssert1(NO, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database)); 
} 

一切工作就好了没有加密,所以用我的代码的其余部分没有问题。控制台打印“密码不正确:(”崩溃之前崩溃日志是:?Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error: failed to prepare statement with message 'file is encrypted or is not a database'.'有明确的密码问题的任何帮助

感谢

回答

1

最有可能的问题是你。尝试在尚未加密的现有数据库dict.sqlite上设置密钥sqlite3_key函数不加密现有数据库如果要加密现有数据库,您需要安装一个新的加密数据库如下所述:

http://zetetic.net/blog/2009/12/29/how-to-encrypt-a-plaintext-sqlite-database-to-use-sqlcipher/

或者,使用SQLCipher 2,你可以使用sqlcipher_export它提供了一种简单的方法:数据库

http://groups.google.com/group/sqlcipher/msg/76d5b03426419761

+0

我应该在一个单独的程序加密的数据库,然后将加密的数据库添加到之间移动数据我读取数据库的应用程序? – 2012-01-19 17:10:29

+0

这将是一个很好的方式来做到这一点,并且可以让您不必执行附加/导出操作。 – 2012-02-07 14:03:05