Android SQLite Database Encryption

Source: Internet
Author: User
Tags sqlite database

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
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 ;

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.