Android Tutorial Android database encryption with SQLCipher library

Status
Not open for further replies.
SQLCipher is an open source project that extends SQLite and adds full database encryption.
License: https://www.zetetic.net/sqlcipher/open-source/

B4A SQLCipher 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.

The only difference between SQL API and SQLCipher API is the Initialize method.
SQLCipher.Initialize expects two additional values: Password and a second parameter that is not used (it was used in the past).

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.

Code changes required to convert from SQL to SQLCipher
- Declare the SQL object as SQLCipher.
- Change the initialize code to:
B4X:
SQL1.Initialize(File.DirRootExternal, "1.db", True, DB_PASSWORD, "")

V1.70
V1.60
V1.50
  • Based on SQLCipher v3.59
  • Supports targetSdkVersion 26.
  • The icu.zip file is no longer required. You can delete it from the Files folder.
  • It is no longer required to disable the debugger virtual assets feature.
    Remove this line: #DebuggerForceStandardAssets: True
  • Old version: www.b4x.com/android/files/SQLCipher150.zip

Depends on: https://repo1.maven.org/maven2/net/...er/4.5.4/android-database-sqlcipher-4.5.4.aar
You should download and copy it to the additional libraries folder.
 

Attachments

  • SQLCipher.zip
    37.2 KB · Views: 426
Last edited:

hypergreatthing

Member
Licensed User
Longtime User
SQLCipher v1.10 is released. This update is based on the native SQLCipher v2.1.1 library.

Note that there might be backwards compatibility issues with databases created with the previous versions. This update should be used by new projects only.
Just fyi. I really wanted to try and leverage the batch queries support 1.10 gives. I was willing to recreate the databases using the new updated library. The problem was that my app kept on crashing on initialize with the following error:
java.lang.NoClassDefFoundError: info/guardianproject/database/CursorWindow

So i created a new test program and found that worked fine.
I'm not sure why, but after about 15 minutes i found that uninstalling the program on the device and then compiling and installing cleaned out whatever else was refering to the old sqlcipher library and everything works again.
So just putting this here to save some time in case someone else runs into the same problem.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
SQLCipher v1.20 is now available. The previous version crashes on Android 4.4.

Starting from v1.20 the native libraries are included in the jar file. This means that you should delete sqlcipher_native.zip from the files tab. However you should add instead a file named icudt46l.zip.

The last parameter in the Initialize method is not used anymore.
 

FabioG

Active Member
Licensed User
Longtime User
SQLCipher v1.20 is now available. The previous version crashes on Android 4.4.

Starting from v1.20 the native libraries are included in the jar file. This means that you should delete sqlcipher_native.zip from the files tab. However you should add instead a file named icudt46l.zip.

The last parameter in the Initialize method is not used anymore.

Hello Erel,

I can not find the link for download the 1.20 version
 
Last edited:

JohnD

Active Member
Licensed User
Longtime User
You may have components from the old SQLCipher Lib still loaded in your project. Here's what I had to do for an old project:

sqlcipher_native.zip needs to be removed from your project files tab. Save your project and unload SQLCipher. Reload your project and select SQLCipher in the libs tab. You should see Version 1.2 come up. Make sure you have added icudt46l.zip at your files tab - just putting it in your files folder is not enough.

SQL1.Initialize(Directory, dbfile , true, pwd, "") is the correct new initialization string.
 

alwaysbusy

Expert
Licensed User
Longtime User
@JohnD Thanks for the reply! I never had the old version on my PC. (Both SQL and SQLCipher are 1.20) This is the first time I used this library, I just downloaded it from the first page. icudt46l.zip is the only one in the files folder, no sqlcipher_native.zip . It is also in the files tab. Cleaned the project and files folder but no result. When I decompile the generated apk, all files seem to be in it. Weird. So, no solution yet, but we'll keep searching :)

btw. I moved this question to Questions (seemed more suitable)
 

hypergreatthing

Member
Licensed User
Longtime User
just fyi, if i'm reading this right: https://groups.google.com/forum/#!topic/sqlcipher/H9XirICbZUw
There is an issue with sqlcipher and android 4.4 kitkat where this error appears:
java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol "_ZN7android10MemoryBaseC1ERKNS_2spINS_11IMemoryHeapEEElj" referenced by "libdatabase_sqlcipher.so"...
which i believe they have possibly fixed in the latest build of sqlcipher.

Thank you in advanced for a possible version update Erel =)
 

tufanv

Expert
Licensed User
Longtime User
I read all the posts for this topic. I cant still understand how to convert old normal databases to new passwrod protected databases. Can anyone help

do we still need to write a small program to convert or new version can convert it directly =?
 
Last edited:

MotoMusher

Active Member
Licensed User
Longtime User
I read all the posts for this topic. I cant still understand how to convert old normal databases to new passwrod protected databases. Can anyone help

do we still need to write a small program to convert or new version can convert it directly =?
I believe you still need to convert with code. That is what I did.
 

hypergreatthing

Member
Licensed User
Longtime User
So FYI:
I was having a problem with sqlcipher 1.20. The it wouldn't read any databases generated by my linux build. This was probably due to version differences. When i read the databases on the linux side i had to migrate them first.
I downloaded the new build of sqlcipher for android (3.1.0), replaced the jar files in 1.20 with the ones in the build and recompiled the SQLCipher.jar file with the new .so files in the build.
Works well far. It's not compatible with files generated under 1.20. If you're interested Erel, i can post it here.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
So FYI:
I was having a problem with sqlcipher 1.20. The it wouldn't read any databases generated by my linux build. This was probably due to version differences. When i read the databases on the linux side i had to migrate them first.
I downloaded the new build of sqlcipher for android (3.1.0), replaced the jar files in 1.20 with the ones in the build and recompiled the SQLCipher.jar file with the new .so files in the build.
Works well far. It's not compatible with files generated under 1.20. If you're interested Erel, i can post it here.
Please upload it. It will be useful.
 

hypergreatthing

Member
Licensed User
Longtime User
Please upload it. It will be useful.
Ok. I changed the version to be 3.10 to coincide with the sqlcipher version.
Also, in reading the forums there might be some performance differences with this new version. It seems noticeably slower when first opening a file.
I'm having trouble uploading (max 512k it says). I'll try a private message or hosting it somewhere.
 
Status
Not open for further replies.
Top