SQLite3 encryption

Recently, due to work reasons, I need to use the sqlite database. The sqlite database is small and easy to use, which feels pretty good. But there is a shortcoming that the database is not encrypted, but the good thing is that sqlite reserves an encrypted interface, we can call it directly. I also refer to the information on the Internet to encrypt the database:
The sqlite database has no encryption function by default, which is inconvenient for some occasions that need to keep the project files confidential. This article takes QT4.4.3 as an example,

>

Modify the sql module in the qt source code and add an encryption function to the qt integrated sqlite database.
1. The files in the /wxsqlite3_prj/sqlite3 directory are compiled to generate sqlite3.lib < /div>

2 Copy sqlite3.h and sqlite3.lib generated in the previous step to the directory?:\Qt\4.4.3\src\plugins\sqldrivers\sqlite.
3 ?:\Qt\4.4.3\src\plugins\sqldrivers\sqlite The sqlite.pro file in the directory is modified to:
TARGET = qsqlite
# Do not use the built-in qt sqilte source code file, but use an externally generated library
win32:LIBS += sqlite3.lib
#
HEADERS = ../../. ./sql/drivers/sqlite/qsql_sqlite.h
SOURCES = smain.cpp \
../../../sql/drivers/sqlite/qsql_sqlite.cpp < /div>

!system-sqlite:!contains( LIBS, .*sqlite.*) {
CONFIG(release, debug|release):DEFINES *= NDEBUG
DEFINES += SQLITE_CORE SQLITE_OMIT_LOAD_EXTENSION SQLITE_OMIT_COMPLETE
# INCLUDEPATH += ../../../3rdparty/sqlite
# SOURCES += ../../../3rdparty /sqlite /sqlite3.c
} else {
LIBS *= $$QT_LFLAGS_SQLITE
QMAKE_CXXFLAGS *= $$QT_CFLAGS_SQLITE
}

include(../qsqldriverbase.pri)
4 Modification?: The file qsql_sqlite.cpp under the directory \Qt\4.4.3\src\sql\drivers\sqlite
The function to be modified bool QSQLiteDriver::open(), the function body is modified to:
bool QSQLiteDriver::open(const QString & db, const QString &, const QString &, const QString &, int, const QString &conOpts)
{
if (isOpen())
close();
if (db.isEmpty ())
return false;
if (sqlite3_open16(db.constData(), &d->access) == SQLITE_OK) {
sqlite3_busy_timeout(d ->access, qGetSqliteTimeout(conOpts));
setOpen(true);
setOpenError(false);
//Add the encryption function “Trucc” as Encryption key, 5 is the key length
sqlite3_key( d->access, “Trucc”, 5);
return true;
} else {
setLastError(qMakeError(d->access, tr(” Error opening database”),
QSqlError::ConnectionError));
setOpenError(true);
return false;
}
}
5 Compile?:\Qt\4.4.3\src\plugins\sqldrivers\sqlite The project under the directory?:\Qt\4.4.3\plugins\ Generate the sqlite with encryption function in sqldrivers
Corresponding library files.
Extract from: http://apps.hi.baidu.com/share/detail/21604459

< div> But when I compile to the last step, the error message’sqlite3_key’ was not declared in this scope appears. I don’t know why, but this error only appears. The sqlite3.h file has also been introduced. Why is it still wrong? Today I calm down and read the sqlite3_key function of the sqlite3.h file, and found that there is a sentence on it

#ifdef SQLITE_HAS_CODEC
I suddenly thought about what this sentence means Isn’t it that the sqlite3_key function can only be used if SQLITE_HAS_CODEC is defined. In this case, we only need to define it in the document that raised the error. In this way, I will add the header of the qsql_sqlite.cpp file that cannot find the sqlite3_key function:
#if defined SQLITE_HAS_CODEC
Add at the end of the file:
#endif
Re-compile, compile and pass.
It seems that if you encounter problems, you still have to settle down. Slowly and carefully analyze, I believe that the problem can always be solved.

Recently, due to work reasons, I need to use the sqlite database. The sqlite database is small and easy to use, which feels pretty good. But there is a shortcoming that the database is not encrypted, but the good thing is that sqlite reserves an encrypted interface, we can call it directly. I also refer to the information on the Internet to encrypt the database:

The SQLite database has no encryption function by default, which is inconvenient for some occasions that need to keep the project files confidential. This article takes QT4.4.3 as an example.

>

Modify the sql module in the qt source code and add an encryption function to the qt integrated sqlite database.

1. Compile the files in the /wxsqlite3_prj/sqlite3 directory to generate sqlite3.lib< /p>

2 Copy sqlite3.h and sqlite3.lib generated in the previous step to the directory?:\Qt\4.4.3\src\plugins\sqldrivers\sqlite.

3 ?:\Qt\4.4.3\src\plugins\sqldrivers\sqlite The sqlite.pro file in the directory is modified to:

TARGET = qsqlite

# Do not use the built-in qt sqilte source code file, but use an externally generated library

win32:LIBS += sqlite3.lib

#

HEADERS = ../../. ./sql/drivers/sqlite/qsql_sqlite.h

SOURCES = smain.cpp \

../../../sql/drivers/sqlite/qsql_sqlite.cpp< /p>

!system-sqlite:!contains( LIBS, .*sqlite.*) {

CONFIG(release, debug|release):DEFINES *= NDEBUG

DEFINES += SQLITE_CORE SQLITE_OMIT_LOAD_EXTENSION SQLITE_OMIT_COMPLETE

# INCLUDEPATH += ../../../3rdparty/sqlite

# SOURCES += ../../../3rdparty /sqlite/sqlite3.c

} else {

LIBS *= $$QT_LFLAGS_SQLITE

QMAKE_CXXFLAGS *= $$QT_CFLAGS_SQLITE

}

include(../qsqldrive rbase.pri)

4 Modify?: The file qsql_sqlite.cpp in the \Qt\4.4.3\src\sql\drivers\sqlite directory

The function to be modified bool QSQLiteDriver: :open(), modify the function body to:

bool QSQLiteDriver::open(const QString & db, const QString &, const QString &, const QString &, int, const QString &conOpts)

{

if (isOpen())

close();

if (db.isEmpty())

return false;

if (sqlite3_open16(db.constData(), &d->access) == SQLITE_OK) {

sqlite3_busy_timeout(d->access, qGetSqliteTimeout(conOpts));

setOpen(true);

setOpenError(false);

//Add encryption function “Trucc” is the encryption key, 5 is the key length< /p>

sqlite3_key( d->access, “Trucc”, 5);

return true;

} else {

setLastError(qMakeError (d->access, tr(“Error opening database”),

QSqlError::ConnectionError));

setOpenError(true);

return false ;

}

}

5 Compile?:\Qt\4.4.3\src\plugins\sqldrivers\sqlite The project under the directory?: \Qt\4.4.3\plugins\sqldrivers generates sqlite with encryption function

corresponding library files.

Excerpt from: http://apps.hi.baidu.com/share /detail/21604459

Can be When I compile to the last step, the error message’sqlite3_key’ was not declared in this scope appeared. I don’t know why, but this error only appears. The sqlite3.h file has also been introduced, but why is it still wrong? Woolen cloth? I calm down today and read the sqlite3_key function of the sqlite3.h file, and found that there is a sentence on it

#ifdef SQLITE_HAS_CODEC

I suddenly thought about what this sentence means Isn’t it that the sqlite3_key function can only be used if SQLITE_HAS_CODEC is defined. In this case, we only need to define it in the document that raised the error. In this way, I will appear in the header of the qsql_sqlite.cpp file where the sqlite3_key function cannot be found, add:

#if defined SQLITE_HAS_CODEC

Add at the end of the file:

#endif

Re-compile, and compile passes.

It seems that if you encounter problems, you still have to stop. Slowly and carefully analyze, I believe that the problem can always be solved.

Leave a Comment

Your email address will not be published.