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:

gudino jose luis

Active Member
Licensed User
Longtime User
Hi Erel
works perfect, but my application to increase its size from 3 MB to 53 MB; now is too great that I can do to reduce their size?
 

gudino jose luis

Active Member
Licensed User
Longtime User
Resolved, Erel Thanks for your time, the poblem was that had moved some files .rar and .pds
the "File" folder and obviously was packing the apk,now only 2 meg increase
 
Last edited:

tsteward

Well-Known Member
Licensed User
Longtime User
I have just started a B4J version of my android app and was wondering before going down this path:
Does this library work with B4J?
And run on multiple platforms?
 

JohnD

Active Member
Licensed User
Longtime User
Are you measuring the APK file size? It shouldn't increase it more than 1 or 2mb
When I look at the App info on my device, the Application size increases to 7.46MB from 3MB. The data size lists as 6.81MB, the actual .db size is 231KB. I expected some un-zipping with SQLCipher, but not this much.
 

cdk

Member
Licensed User
Longtime User
thank you for the question
it is good for SQL1.Initialize(File.DirInternal, "MyBooks.sqlite", True,"","")
it is not good for SQL1.Initialize(File.DirInternal, "MyBooks.sqlite", True,"1","")
How can I do exactly for the mistake ? thank you
the message
upload_2014-10-20_23-36-19.png

#Region Project Attributes
#ApplicationLabel: test
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#DebuggerForceStandardAssets: true
#End Region
#Region Activity Attributes
#DebuggerForceStandardAssets: true
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
Dim SQL1 As SQLCipher
Dim cursor1 As Cursor
End Sub
 
Last edited:

cdk

Member
Licensed User
Longtime User
Erel thank you

the logs


** Activity (main) Create, isFirst = true **
net.sqlcipher.database.SQLiteException: file is encrypted or is not a database
at net.sqlcipher.database.SQLiteDatabase.native_setLocale(Native Method)
at net.sqlcipher.database.SQLiteDatabase.setLocale(SQLiteDatabase.java:2102)
at net.sqlcipher.database.SQLiteDatabase.<init>(SQLiteDatabase.java:1968)
at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:901)
at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:948)
at anyhwheresoftware.b4a.objects.sqlcipher.SQLCipher.Initialize(SQLCipher.java:51)
at ch14.gui.b4a.example.main._activity_create(main.java:335)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at ch14.gui.b4a.example.main.afterFirstLayout(main.java:98)
at ch14.gui.b4a.example.main.access$100(main.java:16)
at ch14.gui.b4a.example.main$WaitForLayout.run(main.java:76)
~e: at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
upload_2014-10-22_0-5-32.png
 
Last edited:

tdocs2

Well-Known Member
Licensed User
Longtime User
Thank you, Erel.

The forum is indeed a treasure trove for which I am grateful to you and to the generosity of all who contribute.

Before I start on this path which sounds very appealing, does SQLCipher encrypt table names, column names, and data?

I am also concerned about some of the comments of impact on the size of the data base - double the size?

Best regards and thank you.

Sandy
 

alwaysbusy

Expert
Licensed User
Longtime User
I'm having some trouble using this lib (1.31):

1. added all lib files to the library folder
2. added icudt64l.zip to the files tab
3. added #DebuggerForceStandardAssets: true to the project attributes

Error log:

Installing file.
PackageAdded: package:com.ab.bleemer
** Activity (main) Create, isFirst = true **
java.lang.UnsatisfiedLinkError: Couldn't load stlport_shared from loader dalvik.system.PathClassLoader[dexPath=/data/app/com.ab.bleemer-6.apk,libraryPath=/data/app-lib/com.ab.bleemer-6]: findLibrary returned null
at java.lang.Runtime.loadLibrary(Runtime.java:358)
at java.lang.System.loadLibrary(System.java:526)
at net.sqlcipher.database.SQLiteDatabase.loadLibs(SQLiteDatabase.java:112)
at net.sqlcipher.database.SQLiteDatabase.loadLibs(SQLiteDatabase.java:107)
at anyhwheresoftware.b4a.objects.sqlcipher.SQLCipher.Initialize(SQLCipher.java:50)
at com.ab.bleemer.abilglobal._initializedatabase(abilglobal.java:306)
at com.ab.bleemer.main._activity_create(main.java:398)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at com.ab.bleemer.main.afterFirstLayout(main.java:98)
at com.ab.bleemer.main.access$100(main.java:16)
at com.ab.bleemer.main$WaitForLayout.run(main.java:76)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
 

alwaysbusy

Expert
Licensed User
Longtime User
Galaxy S4 (4.4.2). I had a similar case https://www.b4x.com/android/forum/threads/sqlcipher-runtime-error-s.35636/#content about a year ago in another app, but in this case I don't have a own lib that uses armeabi.

The libs I use are:

libs.png


ABIndoorLocator is my own lib, but it does not use armeabi (it's a very simple lib that gets the Wifi's around you). The only other ones I've never used in another app are AHLocale and NotificationBuilder.
 

alwaysbusy

Expert
Licensed User
Longtime User
No it did not. I've now created a whole new project, copy and pasted all the code from the bad one and that one does also work. Unfortunately I've overwritten the bad one but I have a strong feeling all I had to do was just restart B4A and it would have been solved. Sorry for bothering you Erel with this. I've done very little with B4A the last 9 months (other language for other client/project), but now I've got a rather big cycle of B4A coming up :) btw, 4.00 rocks!
 

walterf25

Expert
Licensed User
Longtime User
Ok, so what am I missing here, i'm sure i've done this before but i can't remember how for the life of me. Do I need to create the database file with a different software on my laptop for example SQlite Expert Pro 3?
I've added the icudt46l.zip file under my files tab, and also added to the files folder in my app, i'm using version 1.2 of this library.

In My app i copy the Piko_Meter.db from my assets folder into my package name files folder of my app then when I initialize the db like so
B4X:
recs.Initialize(File.DirDefaultExternal, "piko_records.db", False, "password", "")

I get the following error, I know that Erel replied to someone who posted the same question by saying the reason is that there already is another database with a different password. In my case i have not set any password at all to my database, so what other reason could there be for me to get this error.

net.sqlcipher.database.SQLiteException: file is encrypted or is not a database

dbencrypted.jpg


Any advice will be greatly appreciated.

Thanks,
Walter
 
Status
Not open for further replies.
Top