2013-01-19 61 views
0

我正在使用SQLCipher加密SQLite數據庫。我似乎遇到了麻煩,要使用自定義cipher_page_size(與默認值1024不同)來加密數據庫。SQLCipher ATTACH加密數據庫與自定義cipher_page_size

這裏是我的代碼

- (void) database:(sqlite3*) database execute:(NSString*) sql { 
    if (sqlite3_exec(database, [sql UTF8String], NULL, NULL, NULL) == SQLITE_OK) { 
     NSLog(@"\"%@\" successfully executed", sql); 
    } else { 
     NSLog(@"Could not execute \"%@\" (%s)", sql, sqlite3_errmsg(database)); 
    } 
} 

- (void)test { 

    sqlite3 *database1; 
    sqlite3 *database2; 

    NSString *path1 = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] 
         stringByAppendingPathComponent:@"database1.sqlite"]; 
    NSString *path2 = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] 
         stringByAppendingPathComponent:@"database2.sqlite"]; 

    if (sqlite3_open([path1 UTF8String], &database1) == SQLITE_OK) { 
     NSLog(@"database1 opened successfully"); 
     [self database:database1 execute:@"PRAGMA key = 'password1';"]; 
     //[self database:database1 execute:@"PRAGMA cipher_page_size = 2048;"]; 
     [self database:database1 execute:@"CREATE TABLE IF NOT EXISTS table1 (id INTEGER PRIMARY KEY, name TEXT);"]; 
     [self database:database1 execute:@"INSERT INTO table1 (name) VALUES ('bob');"]; 
     sqlite3_close(database1); 
    } else { 
     sqlite3_close(database1); 
     NSLog(@"Failed to open database1 with message '%s'.", sqlite3_errmsg(database1)); 
    } 

    if (sqlite3_open([path2 UTF8String], &database2) == SQLITE_OK) { 
     NSLog(@"database2 opened successfully"); 
     [self database:database2 execute:@"PRAGMA key = 'password2';"]; 
     //[self database:database2 execute:@"PRAGMA cipher_page_size = 2048;"]; 
     [self database:database2 execute:@"CREATE TABLE IF NOT EXISTS table2 (id INTEGER PRIMARY KEY, name TEXT);"]; 
     [self database:database2 execute:@"INSERT INTO table2 (name) VALUES ('john');"]; 

     [self database:database2 execute:[NSString stringWithFormat:@"ATTACH DATABASE '%@' AS database1 KEY 'password1';", path1]]; 
     [self database:database2 execute:@"DETACH DATABASE database1;"]; 

     sqlite3_close(database2); 
    } else { 
     sqlite3_close(database2); 
     NSLog(@"Failed to open database2 with message '%s'.", sqlite3_errmsg(database2)); 
    } 
} 

當有關更改cipher_page_size該行被註釋掉的代碼按預期工作。當它們被註釋掉時,我得到錯誤SQLITE_NOTADB(數據庫被加密或者不是數據庫文件)。

回答

0

你應該明確地爲您安裝後立即附加的數據庫,即:

ATTACH DATABASE 'db1.db' AS database1 KEY 'password1'; 
PRAGMA database1.cipher_page_size = 2048; 
+0

頁面大小不幸的是作爲錯誤在附加聲明本身出現不工作。 – quentinadam

+0

這是一個錯誤,核心庫在附加期間使用默認頁面大小。我已經記錄下來了,我們會仔細研究一下。目前,您是否可以選擇使用1024的默認頁面大小? –

+0

謝謝。是的,我通過輕微改變數據結構來解決問題,現在不需要ATTACH。感謝您提交錯誤。 – quentinadam

相關問題