Encrypted Database?

canalrun

Well-Known Member
Licensed User
Longtime User
Hello,
I have a simple database that took quite a bit of work to build. I would like to have this encrypted when I install it with my app. Before using the database I would load it into memory unencrypted.

I understand SQLite does not support database encryption.

I thought of the following scenario:
Suppose I include the database as an encrypted SQL record dump or encrypted CSV text file with my application.
On startup I would load and decrypt the dump or CSV file, hopefully into memory, or maybe to a temporary Android filesystem file.
I would then import to create a temporary, in memory, SQLite database from the SQL dump or CSV.

Does this sound reasonable? Is there a better way?

Is it possible to work with in-memory data streams or create in-memory files. I would prefer that nothing decrypted ever resides in the Android filesystem.

Thanks,
Barry.
 

canalrun

Well-Known Member
Licensed User
Longtime User
It is not possible to create an in-memory database.
How large is your database? Do you need to issue complicated queries?

Hello,
The database is fairly small and very simple. The sqlite file is 380 K. I have thought of using a list of objects or array of strings also. The queries are not complicated at all – just a "Select * Were rowid = xx". There are 3 fields. I thought of using a database mostly for convenience.

I would like to have the database encrypted just to deter casual users from reusing the data.

Thanks,
Barry.
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
In that case I recommend you to try this solution: Encrypting information with RandomAccessFile library

You can create a custom type with the fields and then convert your database to a list of these type.

Yup, thanks.

I am using the ReadEncryptedObject and WriteEncryptedObject of RAF to store a MAP file of configuration information.

For the database, I dumped the table into CSV records, wrote a PC program to scramble the printable characters in each line, then in B4A List.LoadFromFile, and de-scramble building a List of records I defined using Type.

Can I ask a question about one of the details I came across?

I am used to the syntax – something like:
B4X:
  for i=0 to max-1
    tlr = New(MyListRec)
    ...
    MyList.Add(tlr)
  Next
In B4A, I came up with:
B4X:
  for i=0 to max-1
    Dim tlr As MyListRec
    tlr.Initialize
    ...
    MyList.Add(tlr)
  Next
Is this correct?
Dim creates a new copy of the variable TLR?
Initialize sets up the record fields of TLR making it ready for use?
I don't run the risk of memory leaks if I don't dispose of all the TLR's?

Thanks also for any other insights.

Barry.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I've added [ code ] tags (without spaces) to your post.

Dim creates a new copy of the variable TLR?
Yes.

Initialize sets up the record fields of TLR making it ready for use?
Yes. If your type is only made of "primitive" types then it is not required though it will not do any harm.

It is not needed when you want to assign one variable to another.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…