SQLite is an open-source lightweight database that is now used by both Android and iOS to store structured data, but the encrypted version is not open source. Compromise only open source solution, Sqlcipher is a good choice, it can be encrypted with sqlite, and there are corresponding libraries in both Android and iOS to decrypt the read, but the introduction of the library will increase the size of the app. SQLCipher is a open source library that provides transparent, secure 256-bit AES encryption of SQLite database files.
1. Download the source code
Official Source code: Https://github.com/sqlcipher/sqlcipher
2. Related dependencies
Installing OpenSSL and Tcl, these two dependencies must be installed, or it would be tragic
OpenSSL installs with Yum Openssl-devel, Tcl installs with Yum Tcl-devel
3. Compiling
./configure–enable-tempstore=yes cflags= "-dsqlite_has_codec" ldflags= "-lcrypto"
Make
4. Verify that the compilation is successful
Create an encrypted data, the password is AAA:
$ sqlcipher test.sqlite
sqlite version 3.7.14.1 2012-10-04 19:37:12
enter ". Help" for INSTRUCTIONS
enter SQL statements terminated with a"; "
sqlite> PRAGMA key = ' AAA ';
sqlite> CREATE TABLE A (ind int);
sqlite>. TABLES
a
sqlite>. QUIT
try not to enter a password, directly read the database, in theory, cannot read the data, or error:
$ sqlcipher test.sqlite
sqlite version 3.7.14.1 2012-10-04 19:37:12
enter ". Help" for INSTRUCTIONS
enter SQL statements terminated with a"; "
sqlite>. TABLES
sqlite> quit
Try to enter the password correctly, it should be read successfully:
$ sqlcipher test.sqlite
sqlite version 3.7.14.1 2012-10-04 19:37:12
enter ". Help" for INSTRUCTIONS
enter SQL statements terminated with a"; "
sqlite> PRAGMA key = ' AAA ';
sqlite>. TABLES
a
sqlite>. Quit
Encrypt existing data
How to encrypt an existing SQLite file, there is no other easy way:
1. Export the data first:
$ sqlite3 Ifood.sqlite
>.output Ifood.sql
>.dump
2. Create a new encrypted database:
$ sqlcipher Ifood_lock.sqlite
sqlite> PRAGMA key = ' abcdef '; # Set Password
3. Import data
>.read Ifood.sql
SQLCipher for Android
Https://github.com/sqlcipher/sqlcipher
This is the source code need to compile, more trouble, can go to http://download.csdn.net/detail/wdxin1322/8378519
The use is very simple, you can refer to the Https://www.zetetic.net/sqlcipher/sqlcipher-for-android/, and then change the import of Sqllite in the project to the corresponding sqlcipher.
My personal blog Program Ape Diary--Wang new personal blog
Sqlite3 encryption Scheme Sqlcipher, and Sqlcipher User Guide