1
votes

I have manually created a SQLite database (named "localDB.db") and protected it with a password, by using DB browser. If I am not mistaken, DB browser uses SQLCipher (with a default page size of 1024), for encrypting a database. However, if I try to open the database with the QT plugin QtCipherSqlitePlugin (see following code), I always get the error that "the password is invalid or cipher does not match", although I obviously provide the correct password and have set the cipher as "sqlcipher".

#include <QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QSqlError>
#include <QtSql/QSqlDatabase>

#ifdef Q_OS_IOS

#include <QtPlugin>

Q_IMPORT_PLUGIN(SqliteCipherDriverPlugin)

#endif

#define CONNECTION_FAILED -1

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    qDebug() << QSqlDatabase::drivers();

    QString dir = QCoreApplication::applicationDirPath();
    QString db_file_path = dir + "/Data/localDB.db";
    qDebug() << "DB file path is:" << db_file_path;

    QSqlDatabase db = QSqlDatabase::addDatabase("SQLITECIPHER");
    db.setDatabaseName(db_file_path);
    db.setPassword("test");
    db.setConnectOptions("QSQLITE_USE_CIPHER=sqlcipher");
    db.open();

    if (!db.isOpen())
    {
        qDebug() << "Connection failed: " << db.lastError().driverText();
        exit(CONNECTION_FAILED);
    }

    return 0;
}

The same happens for the inverse process: if I encrypt a database by using this plugin, then I am unable to manually open the database with DB Browser or other similar programs, because it does not accept the password.

How can I solve this problem? I need to access an encrypted SQLite database through both my software and a third-party application like DB browser. Thanks in advance.

2

2 Answers

1
votes

Since April 2018 wxSQLite3 supports the selection of various cipher schemes at runtime, among them the SQLCipher scheme. However, it is necessary to select the SQLCipher legacy mode in wxSQLite3 to be able to access database files that were encrypted with the original SQLCipher library. That is, in addition to the connection option QSQLITE_USE_CIPHER=sqlcipher of QtCipherSqlitePlugin one has to use the additional connection option SQLCIPHER_LEGACY=1.

0
votes

According to the QtCipherSqlitePlugin site, the cipher algorithm is taken from wxSQLite3 in wxWidget, which probably is different from SQLCipher, thus the incompatibility that you experience. If it were me, I'd ditch QtCipherSqlitePlugin and instead just use the SQLite3 C interface. You'll probably have to build the SQLite3 library yourself with SQLCipher built in.