2012-01-18 109 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