Android Tutorial Encrypting information with RandomAccessFile library

RandomAccessFile v1.30 introduces two new methods named: WriteEncryptedObject and ReadEncryptedObject.
These methods are similar to WriteObject and ReadObject methods. The difference is that the new methods encrypt the object before writing it to a file and decrypt it before reading. The encryption algorithm is AES-256 which is considered to provide strong protection.

Android SQLite implementation doesn't support encryption. In many cases you can use these two methods instead. You can create a large Map or List with your own structures and save them to a file.
Edit: Using the new SQLCipher library you can encrypt SQLite databases: http://www.b4x.com/forum/basic4andr...id-database-encryption-sqlcipher-library.html

Developers who are not familiar with WriteObject / ReadObject should try these methods. They are very easy to use and allow you to store complicated objects in a single line of code. Unlike File.WriteMap / WriteList which convert the data to a string, WriteObject and WriteEncryptedObject can write many types of data without losing any information.

You can see more information about the supported types here: Basic4android - RandomAccessFile

Example of writing an encrypted List to a file:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim list1 As List
   list1.Initialize
   For i = 1 To 1000
      list1.Add("Item #" & i)
   Next
   'writing the object
   Dim raf As RandomAccessFile
   raf.Initialize(File.DirRootExternal, "1.dat", False)
   raf.WriteEncryptedObject(list1, "some secret password", raf.CurrentPosition)
   'you can continue to write more objects to this file
   '...
   raf.Close
   
   'reading the object
   Dim raf As RandomAccessFile
   raf.Initialize(File.DirRootExternal, "1.dat", False)
   Dim list2 As List
   list2 = raf.ReadEncryptedObject("some secret password", raf.CurrentPosition)
   Log(list2)
   raf.Close
End Sub

Other more advanced encryption methods are available in the encryption library: http://www.b4x.com/forum/additional-libraries-official-updates/6839-base64-encryption-library.html

The library is available here: http://www.b4x.com/forum/additional...domaccessfile-v1-30-writeenctypredobject.html
 

RickV

Member
Licensed User
Still new to b4a.
Looking at the code from above....
B4X:
raf.WriteEncryptedObject(list1, "some secret password", raf.CurrentPosition)
Does the compiler store the plain text "some secret password" in the binary executable like qb and vb would ?
Also is this true for Const / dim / public pw = "some secret password" ?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Top