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.