Android uses an open-source SQLite database, the database itself is not encrypted, there are usually two of encryption ideas:
1. Use the encryption algorithm for several key fields and then deposit the database
2. Encrypt the entire database
SQLite database encryption tool: Pay-per-tool:
- SSE (SQLite encryption Extension)
Free tools:
Sqlcipher using:
Sqlcipher is fully open source software that provides 256-bit AES encryption
Source Code compilation:
1. OpenSSL compilation
Sqlcipher source compilation needs to rely on the libcrypto provided by OpenSSL
Download the OpenSSL source, here choose Stable version 1.0.1h
1 openssl-1.0. 1h admin$./config--prefix=/usr/local--openssldir=/usr/local/OpenSSL 2 openssl-1.0 make3 openssl-1.0 make test4 openssl-1.0makeinstall
2. Sqlcipher Source Code compilation
: Https://github.com/sqlcipher/sqlcipher
1 sqlcipher admin$/configure--enable-tempstore=yes cflags="-dsqlite_has_codec" ldflags="/usr/local/lib/libcrypto.a"2 make
The command line uses:
1. Create an encrypted database
1$ sqlcipherencrypted.db2SQLCipher version3.8.4.3 the-Geneva-Geneva -: -: A3Enter". Help" forInstructions4Enter SQL statements terminated with a";"5sqlite> PRAGMA key ='Thisiskey';6Sqlite> CREATE TABLE encrypted (IDinteger, name text);7Sqlite>. Schema8CREATE TABLE Encrypted (IDinteger, name text);9Sqlite>. Q
2. Open the encrypted database
1$ sqlcipherencrypted.db2SQLCipher version3.8.4.3 the-Geneva-Geneva -: -: A3Enter". Help" forInstructions4Enter SQL statements terminated with a";"5sqlite> PRAGMA key ='Thisiskey';6Sqlite>. Schema7CREATE TABLE Encrypted (IDInteger, name text);
3. Modify the database password
1 ' Newkey ';
4. Encrypt an existing database
1 $ sqlcipher Banklist.sqlite32SQLCipher version3.8.4.3 the-Geneva-Geneva -: -: A3Enter". Help" forInstructions4Enter SQL statements terminated with a";"5Sqlite> ATTACH DATABASE'encrypted.db'As encrypted KEY'Thisiskey';6Sqlite> SELECT Sqlcipher_export ('Encrypted');7sqlite> DETACH DATABASE encrypted;
5. Decrypting the database
1 $ sqlcipher encrypted.db2SQLCipher version3.8.4.3 the-Geneva-Geneva -: -: A3Enter". Help" forInstructions4Enter SQL statements terminated with a";"5sqlite> PRAGMA key ='Thisiskey';6Sqlite> ATTACH DATABASE'plaintext.db'As plaintext KEY"';7Sqlite> SELECT Sqlcipher_export ('plaintext');8sqlite> DETACH DATABASE plaintext;
Android version Sqlcipher use
Android version of the source code, the compiler needs to rely on a lot of things, too lazy to try, you can directly download the compiled binary, the official website to download, or here is 3.1.0 version
Note: Binary on GitHub is 2.1.1, the actual test is not available on Android 4.4 KitKat, please download the latest 3.1.0 version from the official website
1. Add the extracted Libs and asserts to the project:
2. Replace all android.database.sqlite.* in the project with Net.sqlcipher.database.*, the original android.database.Cursor can be retained
3. In the activity or other place where the database is called, be careful to add the following before using the database:
1 sqlitedatabase.loadlibs (this);
Note: After using the Sqlcipher command line to encrypt the original database, the new database may have a version of 0, causing the database to be rebuilt in Sqliteopenhelper into OnCreate ().
Workaround, change the database version back to the original version:
1 ;