我試圖使用SQLCipher將未加密sqlite3數據庫的內容添加到加密數據庫。我已經基於我想要做的this和this。然而,有些事情對我來說還不清楚。使用SQLCipher將加密數據庫附加到未加密數據庫
在線路
ATTACH DATABASE
,不加密的數據庫必須是.db
類型?它可以是.sqlite
匹配我的原始數據庫?請問加密數據庫是否已經存在?如果是這樣,它應該在應用程序中的位置?我是否必須提供文件路徑(文檔目錄等)?
我在哪裏可以找到成功加密的數據庫?它將在哪裏得到保存?
這裏是我的代碼:
+ (void)encryptDB
{
sqlite3 *unencrypted_DB;
NSString *path_u = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
stringByAppendingPathComponent:@"dict.sqlite"];
if (sqlite3_open([path_u UTF8String], &unencrypted_DB) == SQLITE_OK) {
NSLog(@"Database Opened");
// Attach empty encrypted database to unencrypted database
sqlite3_exec(unencrypted_DB, "ATTACH DATABASE 'dict_encrypted.sqlite' AS encrypted KEY '1234';", NULL, NULL, NULL);
// Create new tables within encrypted database to match those in unencrypted database
sqlite3_exec(unencrypted_DB, "CREATE TABLE encrypted.t1(A,B,C);", NULL, NULL, NULL);
// Copy items from unencrypted database into encrypted database
sqlite3_exec(unencrypted_DB, "INSERT INTO encrypted.t1 SELECT * FROM t1;", NULL, NULL, NULL);
// Detach encrypted database
sqlite3_exec(unencrypted_DB, "DETACH DATABASE encrypted;", NULL, NULL, NULL);
NSLog (@"End database copying");
sqlite3_close(unencrypted_DB);
}
else {
sqlite3_close(unencrypted_DB);
NSAssert1(NO, @"Failed to open database with message '%s'.", sqlite3_errmsg(unencrypted_DB));
}
}
我想我很好奇,爲什麼你第二個參考文獻說要使用這種技術而不是僅僅用「REKEY」來加密現有的數據庫。我們的SQLCipher實現可以很好地處理現有數據庫的「REKEY」。 –