The free SQLite Open stream code only provides two functions, only implement these two functions to achieve the overall database encryption. And then he took a little effort to find a well-realized open Source Library from the Internet http://sourceforge.net/projects/wxcode/files/Components/wxSQLite3/
Then write about how to integrate it into your so library.
First download the source code, find Sqlite3/secure/src, all of the files (except Sqlite3.def) copied to the JNI directory under Android project, and then open the Sqlite3.c file, add a line of code at the front:
#define SQLITE_HAS_CODEC
Create a new Android.mk file under the JNI directory and enter the following:
Local_path: =$(Call My-dir) include$(Clear_vars) local_module:= libsqlite3 local_src_files:= sqlite3secure.c #这里写的是相对路径include$(Build_static_library) # From here is the declaration of so library, I use an automatic load script here, only need to specify # My_files_path can automatically load all CPP and C file include$(Clear_vars) Local_module: = Nativemy_files_path: =$(Local_path)/classesmy_files_suffix: =%.Cpp%.C # recursively iterates through all the files in the directory rwildcard=$(Wildcard $ $)$(foreach D,$(Wildcard $*),$(Call Rwildcard,$d/, $) # get the corresponding source file my_all_files: =$(foreach Src_path,$(My_files_path),$(Call Rwildcard,$(Src_path),*.*)) My_all_files: =$(My_all_files:$(My_cpp_path)/./%=$(My_cpp_path)%)My_src_list: =$(Filter $(My_files_suffix),$(my_all_files)) My_src_list: =$(My_src_list:$(Local_path)/%=%)# Remove the repeated words of the string define UNIQ =$(EvalSeen: =)$(foreach _, $,$(if $(Filter $_,${seen}),,$(EvalSeen + =$_)))${seen}endef# recursive traversal gets all directories my_all_dirs: =$(Dir$(foreach Src_path,$(My_files_path),$(Call Rwildcard,$(Src_path),*/)) My_all_dirs: =$(Call Uniq,$(My_all_dirs)) #MY_ALL_DIRS: = # assigned to the NDK compiler system Local_src_files: =$(my_src_list) Local_c_includes: =$(My_all_dirs) Local_ldlibs: =-llog-ldllocal_static_libraries: = libsqlite3 # Add static library include here$(Build_shared_library)
When you use it, you only need to
#include "sqlite3.h"
Then after the open database, call the following two functions:
Sqlite_apiintSqlite3_key (Sqlite3 *db,/ * Database to be rekeyed * / Const void*pkey,/ * The key * / intNkey/ * The key length * /); Sqlite_apiintSQLITE3_KEY_V2 (Sqlite3 *db,/ * Database to be rekeyed * / Const Char*zdbname,/ * Name of the database * / Const void*pkey,/ * The key * / intNkey/ * The key length * /);
You can encrypt the database.
It is important to note that any one of these two functions can be called immediately after the database is created to encrypt the database. When you need to manipulate an encrypted database, you only have to call the function once after the open database to operate the database normally.
Introducing an encryption module to SQLite in the Android JNI environment