1 つの非常に単純なデータベースを暗号化するために、アプリに 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));
}
暗号化なしですべてが正常に機能したため、残りのコードに問題はありません。コンソールは、クラッシュの前に「Incorrect Password :(」を出力します。クラッシュ ログは次のとおりTerminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error: failed to prepare statement with message 'file is encrypted or is not a database'.'
です。パスワードに明らかに問題があります。
ありがとう。