Add encryption to Qt SQLite

Source: Internet
Author: User
Tags sqlite


Integrate SQLite code
    • There is no encryption function in the open source SQLite, so if you need encryption, you need to implement Sqlite3_keySqlite3_rekey and other related functions.
    • However, the open source Wxsqlite3 has been implemented in the encryption, so as long as the code here to integrate into QT , the main is to implement the Sqlite3_key sqlite3_rekey and other functions to add to In the sqlite3.c of qt
    • Here is a code that has been integrated qt_sqlite_driver.zip
      • Just unzip it to the qtbase\src\3rdparty .
      • The main changes to the consolidation are as follows:
        • Modified the sqlite.pri compilation configuration file
        • Modified the sqlite\sqlite3.c file
        • Added sqlite\codec.c (. h ) sqlite\rijndael.c ( . h) sqlite\sha2.c ( . h)
Modify QT Source code

  • Open Qtbase\src\sql\kernel\qsqldriver.h, declaring two new interfaces


     
    / **
      * @brief Set database password
      * @param key-password
      * /
    virtual bool setKey (const QString & key);
    
    / **
      * @brief reset database password
      * @param key-password
      * /
    virtual bool resetKey (const QString & key);
    

  • Open   qtbase\src\sql\kernel\qsqldriver.cpp to add a default implementation to the two new interfaces


     
    
    bool QSqlDriver::setKey(const QString&)
    {
        return false;
    }
    
    bool QSqlDriver::resetKey(const QString&)
    {
        return false;
    }

  • Open Qtbase\src\sql\drivers\sqlite\qsql_sqlite_p.h, inherit those two new interfaces


    / **
      * @brief Set database password
      * @param key-password
      * /
    bool setKey (const QString & key);
    
    / **
      * @brief reset database password
      * @param key-password
      * /
    bool resetKey (const QString & key);


  • Open Qtbase\src\sql\drivers\sqlite\qsql_sqlite_p.cpp, inherit those two new interfaces


     
     
    
     
    
    bool QSQLiteDriver::setKey(const QString& key)
    {
        Q_D(QSQLiteDriver);
        if (d->access)
        {
            return (SQLITE_OK == sqlite3_key(d->access, key.toStdString().c_str(), key.toStdString().size()));
        }
    
        return false;
    }
    
    bool QSQLiteDriver::resetKey(const QString& key)
    {
        Q_D(QSQLiteDriver);
        if (d->access)
        {
            return (SQLITE_OK == sqlite3_rekey(d->access, key.toStdString().c_str(), key.toStdString().size()));
        }
    
        return false;
    }
Compiling QT


Dynamically compiling Qt 5.6
Statically compiling Qt 5.6






http://wangjie.rocks/2016/05/10/qt-sqlite-cipher/



Add encryption to Qt SQLite


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.