Introduce the encryption module to SQLite in the Android JNI Environment

Source: Internet
Author: User

Introduce the encryption module to SQLite in the Android JNI Environment

Free SQLite Open Source Code provides only two functions. Only these two functions can achieve the overall database encryption. Then waste a little effort from the Internet to find a good open source library http://sourceforge.net/projects/wxcode/files/Components/wxSQLite3/ has been achieved
Then, write down how to integrate it into your own so library.

First download the source code, find sqlite3/secure/src, copy all the files (except sqlite3.def) to the jni directory of the Android project, and then open the sqlite3.c file, add a line of code at the beginning:

#define SQLITE_HAS_CODEC

Create an Android. mk file in the jni directory and enter the following content:

LOCAL_PATH: = $ (call my-dir) include $ (CLEAR_VARS) LOCAL_MODULE: = libsqlite3 LOCAL_SRC_FILES: = sqlite3secure. c # The relative path include $ (BUILD_STATIC_LIBRARY) is written here # the declaration of the so library starts from here. I used an automatic loading script here, you only need to specify # MY_FILES_PATH to automatically load all cpp and c files include $ (CLEAR_VARS) LOCAL_MODULE: = nativeMY_FILES_PATH: = $ (LOCAL_PATH)/ClassesMY_FILES_SUFFIX: = %. cpp %. c # recursively traverse all files in the directory rwildcard =$ (wildcard $1 $2) $ (foreach d, $ (wildcard $1 *), $ (call rwildcard, $ d/, $2) # obtain 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 word define uniq =$ (eval seen: =) $ (foreach _, $1, $ (if $ (filter $ _, $ {seen}), $ (eval seen + =$ _) $ {seen} endef # recursively traverse all directories to get 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: = # assign a value to the NDK compilation system LOCAL_SRC_FILES: =$ (MY_SRC_LIST) LOCAL_C_INCLUDES :=$ (MY_ALL_DIRS) LOCAL_LDLIBS: =-llog-ldlLOCAL_STATIC_LIBRARIES: = libsqlite3 # Add the static Library include $ (BUILD_SHARED_LIBRARY)

In use, you only need

#include "sqlite3.h"

Then, after the open Database, call the following two functions:

SQLITE_API int sqlite3_key(  sqlite3 *db,                   /* Database to be rekeyed */  const void *pKey,              /* The key */  int nKey                       /* The key length */);SQLITE_API int sqlite3_key_v2(  sqlite3 *db,                   /* Database to be rekeyed */  const char *zDbName,           /* Name of the database */  const void *pKey,              /* The key */  int nKey                       /* The key length */);

You can encrypt the database.
Note that the database can be encrypted only when any of the above two functions is called immediately after the database is created. To operate an encrypted database, you only need to call this function once after the open Database.

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.