From 7bed20f1bf0e67852f50d1230f2b76f7c69ed0e1 Mon Sep 17 00:00:00 2001 From: Martin Kleusberg Date: Fri, 14 Nov 2014 12:51:27 +0100 Subject: [PATCH] cipher: Proper fix for the is-it-encoded?-trial-and-error problem Apparently after querying an encrypted database the handle is left in an inconsistent state. So before setting any key or other encryption details make sure to close and reopen the database to get a new, valid handle. Thanks to Nick (developernotes) and Stephen (sjlombardo) from the SQLCipher forums for pointing this out! --- src/sqlitedb.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/sqlitedb.cpp b/src/sqlitedb.cpp index 0c39fc58..4f29bcd1 100644 --- a/src/sqlitedb.cpp +++ b/src/sqlitedb.cpp @@ -79,10 +79,19 @@ bool DBBrowserDB::open(const QString& db) CipherDialog cipher(0, false); if(cipher.exec()) { + // Close and reopen database first to be in a clean state after the failed read attempt from above + sqlite3_close(_db); + if(sqlite3_open_v2(db.toUtf8(), &_db, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK) + { + lastErrorMessage = QString::fromUtf8((const char*)sqlite3_errmsg(_db)); + return false; + } + + // Set key and, if it differs from the default value, the page size sqlite3_key(_db, cipher.password().toUtf8(), cipher.password().toUtf8().length()); - sqlite3_exec(_db, QString("PRAGMA cipher_page_size = 4096;").toUtf8(), NULL, NULL, NULL); - sqlite3_key(_db, cipher.password().toUtf8(), cipher.password().toUtf8().length()); - sqlite3_exec(_db, QString("PRAGMA cipher_page_size = %1;").arg(cipher.pageSize()).toUtf8(), NULL, NULL, NULL); + if(cipher.pageSize() != 1024) + sqlite3_exec(_db, QString("PRAGMA cipher_page_size = %1;").arg(cipher.pageSize()).toUtf8(), NULL, NULL, NULL); + isEncrypted = true; } else { sqlite3_close(_db);