Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4android > Basic4android Getting started & Tutorials
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Basic4android Getting started & Tutorials Android development starts here. Please do not post questions in this sub-forum.

Android database encryption with SQLCipher library

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-13-2012, 03:47 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default Android database encryption with SQLCipher library

The native SQLite engine included in the OS doesn't support encryption.
SQLCipher is an open source project that extends SQLite and adds full database encryption.

B4A SQLCipher object is a special subtype of SQL object. There is almost no need to change any code in order to switch from regular SQL to SQLCipher.

SQLCipher depends on several native resources that are packaged inside a file named sqlitecipher_native.zip. The file size is 3mb.
The first time the program runs after installation it needs to unzip this file and copy the resources to the internal folder.

On slower devices the copying action can be a long action.
It is recommended to initialize the object with code such as:
Code:
ProgressDialogShow("Initializing database...")
SQL1.Initialize(
File.DirRootExternal, "1.db"True, DB_PASSWORD, File.DirRootExternal)
ProgressDialogHide
It is only slow on the first time that the application runs after installation.

Preferably you should use a real device when developing as the emulator is too slow to handle the native resources.

The only difference between SQL API and SQLCipher API is the Initialize method.
SQLCipher.Initialize expects two additional values: Password and NativeLibsFolder.

Password is the database password. You can pass an empty string if there is no password. Note that it is not possible to change the password (or set a new password) to an existing database.
NativeLibsFolder is the folder where sqlitecipher_native.zip is located. Usually you will add this file to the Files tab and then set this value to File.DirAssets. During development, in order to make the installation process faster you can manually copy this file to the storage card and then set it to File.DirRootExternal for example (make sure to remove and delete it from the Files folder).

Code changes required to convert from SQL to SQLCipher
- Declare the SQL object as SQLCipher.
- Change the initialize code to:
Code:
ProgressDialogShow("Initializing database...")
SQL1.Initialize(
File.DirRootExternal, "1.db"True, DB_PASSWORD, File.DirAssets)
ProgressDialogHide
Note that there is no need to change anything in DBUtils module.

The library can be downloaded from here:
http://www.basic4ppc.com/android/files/SQLCipher.zip

Installation instructions:
- Unzip the file.
- Copy all files from the InternalLibrariesFolder to the internal libraries folder.
- Add sqlcipher_native.zip from the NativeResources folder to your project as described above.
- You should reference both SQL and SQLCipher in your project.
Reply With Quote
  #2 (permalink)  
Old 02-13-2012, 04:33 PM
Basic4ppc Expert
 
Join Date: May 2008
Posts: 550
Default

Thank you Erel. I wished for this library.
__________________
I'm not good at English, please understand. Thank you.
Reply With Quote
  #3 (permalink)  
Old 02-13-2012, 05:38 PM
Junior Member
 
Join Date: Dec 2011
Posts: 12
Default SQLite db created outside of the application?

This is great news, however I do have a question:

How would I encrypt/set a password for a SQLite database that has been created/populated outside of a B4A application which is then to be used in the application?
Reply With Quote
  #4 (permalink)  
Old 02-13-2012, 06:56 PM
Knows the basics
 
Join Date: Mar 2011
Location: Greece
Posts: 54
Default

+1 Erel!
Reply With Quote
  #5 (permalink)  
Old 02-14-2012, 06:01 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Quote:
How would I encrypt/set a password for a SQLite database that has been created/populated outside of a B4A application which is then to be used in the application?
You have two options:
- Use the desktop version of SQLCipher. It may require purchasing a license.
- Create a program that reads the data from the existing databases and writes it to a password protected database.
Reply With Quote
  #6 (permalink)  
Old 02-14-2012, 07:18 PM
Junior Member
 
Join Date: Dec 2011
Posts: 12
Default

Thanks Erel...

How is a password protected database created in the first instance?

I have been using SQLite Database Browser to create and subsequently populate my database.
Reply With Quote
  #7 (permalink)  
Old 02-15-2012, 06:30 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

You should create it with a tool that supports SQLCipher. You can also build a small Android program that converts your existing database to an encrypted database.
Reply With Quote
  #8 (permalink)  
Old 02-15-2012, 03:33 PM
Junior Member
 
Join Date: Dec 2011
Posts: 12
Default

For those who are looking for an application that will enable the setting of a password on an SQLite database...

I have discovered Navicat supports this functionality.
Reply With Quote
  #9 (permalink)  
Old 02-16-2012, 11:03 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Are you sure that they support SQLCipher encryption? There are several encryption modules.
Reply With Quote
  #10 (permalink)  
Old 02-18-2012, 01:25 PM
alfcen's Avatar
Basic4ppc Expert
 
Join Date: Apr 2007
Location: Okinawa, Ryukyu Islands
Posts: 810
Send a message via Skype™ to alfcen
Awards Showcase
Beta Tester 
Total Awards: 1
Default

If I am not mistaken this library does not perform encryption but addresses databases that have been previously ciphered using SQLCipher or similar tools?
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Base64 and Encryption library agraham Additional libraries and official updates 35 05-21-2012 12:46 PM
Encrypte SQLite database with SQLCipher for Android? susu Basic4android Updates and Questions 1 02-13-2012 03:54 PM
Help on Encryption library please. Eugenio Serrano Basic4android Updates and Questions 4 11-22-2011 09:39 PM
BadPaddingException with Encryption Library pavan Basic4android Updates and Questions 1 08-30-2011 05:17 PM
android database protect slowtime Basic4android Updates and Questions 4 02-28-2011 09:54 AM


All times are GMT. The time now is 10:11 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0