Android Question SQLCipher: File is encrypted or is not a database

Rusty

Well-Known Member
Licensed User
Longtime User
I am using SQLCipher 1.3 on Marshmallow Samsung S7.
I have created an SQLite/SQLCipher database/table using:

B4X:
Sub CreateDatabase
    If File.Exists(DBFolder, DBFile) = False Then        'Main.SDCard.SpecificData, StepDataDBFile) = False Then         'hasn't already been created
        Try
            File.Copy(File.DirAssets, "shellcipher.db3", DBFolder, DBFile)    'copy the cipher base file
Log("CREATED NEW DATABASE " & DBFolder & "/" & DBFile)
'            MigrateSQLite(SQLSteps, Main.Version.SQLite)
        Catch
            Log("Shell copy Failed SD: " & DBFolder & "MDBR: " & DBFile)
        End Try
    End If
    OpenDBF
End Sub

...

Sub OpenDBF
    If ThisSQL.IsInitialized = False Then    'open the database
        Try  
            ThisSQL.initialize(DBFolder, DBFile, False, SQLiteKEy, File.DirAssets)          
'            MigrateSQLite
        Catch
            Log("Not encrypted, or failed to open " & DBFolder & "/" & DBFile)
            ThisSQL.initialize(DBFolder, DBFile, False, "", File.DirAssets)
        End Try
    End If
End Sub

It successfully inserts a row into the table (after it has been created)
B4X:
INSERT OR REPLACE INTO [StepData] ([FacilityID], [MRN], [TabletNo], [Today], [StepCount], [Distance], [Calories]) VALUES (?, ?, ?, ?, ?, ?, ?)

The next time it attempts to write to this table (5 seconds later the service executes that wrote successfully the first time) i get an error:

CheckTable (SQLiteException) net.sqlcipher.database.SQLiteException: file is encrypted or is not a database: , while compiling:
SELECT name FROM sqlite_master WHERE Type='table' and name ='StepData';

Any ideas why this might happen?
Regards,
Rusty
 

Rusty

Well-Known Member
Licensed User
Longtime User
ShellCipher was created in 2014...probably the wrong version of SQLCipher...?
Does the "Shell" file need to be re-created by then new cipher?
 
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
Thanks Erel,
I regenerated my "shellcipher.db3" base file and all is well :)
Regards,
Rusty
 
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
In order to be able to use several different databases on my S7 (Marshmallow 6.0.1), I had to use SQLCipher v1.2
V1.3 would not work...I don't understand this as I thought 1.3 was required on Android 6+...
Any thoughts?

ADDITIONAL info: I used the 1.2 which seemed to work but then would sporatically error on:
InsertMaps: INSERT OR REPLACE INTO [StepData] ([FacilityID], [MRN], [TabletNo], [Today], [TotalTime], [StepCount], [Distance], [Calories]) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
VALUES: (ArrayList) [facility1, Rusty, Tablet1234, 2016/11/14, 101886, 175, 402, 3]
(SQLiteException) net.sqlcipher.database.SQLiteException: file is encrypted or is not a database: , while compiling: INSERT OR REPLACE INTO [StepData] ([FacilityID], [MRN], [TabletNo], [Today], [TotalTime], [StepCount], [Distance], [Calories]) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
(SQLiteException) net.sqlcipher.database.SQLiteException: file is encrypted or is not a database: , while compiling: Select * From [StepData]
svcshealth_vvvvvv7 (java line: 449)
java.lang.RuntimeException: Object should first be initialized (Cursor).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
at anywheresoftware.b4a.sql.SQL$CursorWrapper.Close(SQL.java:415)
at talkingsurvey.b4a.tshealth.svcshealth._vvvvvv7(svcshealth.java:449)
at talkingsurvey.b4a.tshealth.svcshealth._resolver_onreadresult(svcshealth.java:320)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at anywheresoftware.b4a.BA$2.run(BA.java:328)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

So I went back to v1.3 and the problem persists. The database is created within the app, so the version of the db should be compatible... STRANGELY enough, occasionally, it DOES succesffully write the data (and read it too)...??

InsertMaps: INSERT OR REPLACE INTO [StepData] ([FacilityID], [MRN], [TabletNo], [Today], [TotalTime], [StepCount], [Distance], [Calories]) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
VALUES: (ArrayList) [facility1, Rusty, Tablet1234, 2016/11/14, 101886, 175, 420, 3]

INSERTED SUCCESSFUL
READ DATA HERE:
Facility facility1
0 count 175
Today 2016/11/14
Distance 420

This is in a Widget, so I don't know if this makes a difference or not.
Also related to this, which version of the Windows SQLITE dlls are required to adequately create a database that is compatible with Android B4a v1.3?
Any suggestions would be greatly appreciated.
Rusty
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is in a Widget, so I don't know if this makes a difference or not.
It shouldn't make a difference.

Can you upload an example where you create a SQLCipher database at runtime and then it fails?

which version of the Windows SQLITE dlls are required to adequately create a database that is compatible with Android B4a v1.3?
B4A SQLCipher v1.3 library is based on the native SQLCipher v3.3.1.
 
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
Thanks Erel,
While getting things ready to send to you, I was watching the Logs and saw where a service (already compiled and installed once with startatboot = true) continues to fire even though the MAIN is recompiled and started.
If one catches the servicestart cycle "wrong", it is firing without the MAIN variables set yet. In my Main, I build the Cipher key and then initialize the service.
Since the service is still running, it can fire before my Main starts up and creates/supplies the Cipher key to the service.
When the service tries to access the encrypted database(s), it will obviously fail.
Please let me know if the above makes sense or not.
Regards,
Rusty
 
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
...and the light bulb comes on...now I understand :)

Quick question though, do the variables in the Starter service get encrypted when compiled in release obfuscated mode?
thanks Erel
 
Last edited:
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
I was more concerned about string encryption like:
Dim x as string = "abc123"
Will the "abc123" be encrypted in the starter service.
Sorry for my miscommunication on this.
Rusty
 
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
Erel,
Followup after our chase for the debug problem.
In one communication you indicated I needed to use SQLCipher 1.3.
I've tried to use 1.3 but the files created on the PC and subsequently downloaded to the tablet are not compatible.
Do you know which .NET library is proper to use on the PC to create 1.3 compatible databases? I've downloaded and used what looks to me to be their latest, but still no happiness.
Thanks,
Rusty
 
Upvote 0
Top