Giter Club home page Giter Club logo

Comments (7)

devbean avatar devbean commented on September 24, 2024 2

The newest version (v1.1) of this plugin QtCipherSqlitePlugin does support SQLCipher. But I believe you need try the following connection options:

dbconn.setConnectOptions("QSQLITE_USE_CIPHER=sqlcipher; SQLCIPHER_LEGACY=1");

That's because current version of SQLCipher only supports legacy mode so you need set SQLCIPHER_LEGACY to 1.

I have opened the database encrypted by SQLiteStudio SQLCipher. Please let me know if this helps you.

from qtciphersqliteplugin.

Swerved avatar Swerved commented on September 24, 2024

SQLCipher != QCipherSqlite

You are getting confused as these are different encryption tools for SQLite Databases.

DB Browser will not work with this plugin, but it will for SQLCipher, which is different, developed by zetetic I believe and costs money for a compiled PC version of the driver (the Android version is available on their Community Edition).

But fear not, you CAN use this plugin with a database browser AND keep it encrypted. You have, the following options:

  1. Create your own DB Browser App with Qt. There is a example project to get you started in QtCreator.
  2. Make an app to decrypt the Db before you edit it in DB Browser.
  3. Don’t encrypt with a password. Instead, use QtAESEncryption, or QuaZip, or both, to encrypt the file you will need to decrypt the file and unzip before loading it into your App.

I use both 1 and 3, QuaZip enables me to store multiple files in an archive. Rather than just pass wording the zip file (can do this too if you want) I put it into a QtAESEncrypted QByteArray and write this to a QFile output of my choosing.

I also have written a function to decrypt my dbs if I need to use a more powerful DB Editing program than the one I wrote myself (ie. SQLite Browser).

SQLite Browser is Open Source and you can add a QtCipherSqlite plugin for that, but you’d need to write it. Something I’d like to look into at some point time depending.

from qtciphersqliteplugin.

SupIQ avatar SupIQ commented on September 24, 2024

By using your suggestion, I am now able to open a database encrypted with DB browser, so thanks for that.
However, the inverse process does not work: if I encrypt a database with the following command

db.setConnectOptions("QSQLITE_USE_CIPHER=sqlcipher; SQLCIPHER_LEGACY=1; QSQLITE_CREATE_KEY");

DB browser opens the database without asking for password first. It seems that the above command does not encrypt the database.

Which settings do I need to use for encrypting a database with your plugin, so that when the user tries to open it with DB browser or similar programs, he must insert the correct password first?

from qtciphersqliteplugin.

devbean avatar devbean commented on September 24, 2024

I have tested as you said. First created key using plugin then close Qt application. When I opened the database generated by the plugin with SQLiteDatabaseBrowser, DBBrowser required password. So could you please submit your code for testing? Thank you.

from qtciphersqliteplugin.

SupIQ avatar SupIQ commented on September 24, 2024

This is the code I have written for encrypting an already existing unencrypted database named localDB.db with your plugin:

#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("F:/Project_alpha/Data/localDB.db"); //localDB.db is already existing before running the application
    db.setPassword("pass");
    db.setConnectOptions("QSQLITE_USE_CIPHER=sqlcipher; SQLCIPHER_LEGACY=1; QSQLITE_CREATE_KEY");

    db.open();

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

    return 0;
}

After running the application, localDB.db is opened by DB Browser without asking for any password!
What you say works only if the database does not already exist, but if I want to encrypt a plaintext database, created before running the application, it does not work.

from qtciphersqliteplugin.

devbean avatar devbean commented on September 24, 2024

Thank you for your reply. I could comfirm this problem. Here is some solution.

First, you have some incorrect code:

db.open();

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

db.open() will return false but db.isOpen() will return true. I'll explain why this happen.

In QtCipherSqlitePlugin, open() function's implementation as following (some pseudo-code):

bool SQLiteCipherDriver::open()
{
...
if (sqlite3_open_v2(db, &d->access, openMode, nullptr) == SQLITE_OK) {
    setOpen(true);
    wxsqlite3_config(d->access, "cipher", CODEC_TYPE_SQLCIPHER);
    const int result = sqlite3_rekey(d->access, "pass", 4);
    if (SQLITE_OK != result) {
        return false;
    }
} else {
  setOpen(false);
}
...
}

As you can see, first the plugin need open SQLite then rekey (create password). In Qt SQL plugin, QSqlDriver::open() is used for opening a database. If we open database successfully, we should set isOpen() to true but because rekey is only an action after open and it fails so open() returns false. (But I'm not sure if this is correct for open(). Maybe this should be recode. Not sure for now. Please leave your suggestion if you wish. Thank you) So you need use open() instead of isOpen():

if (!db.open()) {
    qDebug() << "Can not open connection: " << db.lastError().driverText();
    exit(CONNECTION_FAILED);
}

Second, why rekey failed? I have tested sqlite3_rekey() result code is SQLITE_CORRUPT. After communicating with wxSQLite3 (which QtCipherSqlitePlugin based on) author, the reason seems to be that the default page size (of the plain database) is 4096 bytes, while the SQLCipher requested page size is 1024. All non-legacy test cases using the default page size worked flawlessly.

A workaround is to change the page size of the plain database first. To do this you can issue the following SQL commands:

PRAGMA page_size=1024;
VACUUM;

Thereafter rekeying in legacy SQLCipher mode works.

You could find out our communication at here.

from qtciphersqliteplugin.

houlang avatar houlang commented on September 24, 2024

Here are some instructions on cryptographic algorithms that may be helpful
Encryption Algorithm Description

from qtciphersqliteplugin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.