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:

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:
Sub Activity_Create(FirstTime As Boolean)
   Dim list1 As List
   For i = 1 To 1000
      list1.Add("Item #" & i)
   '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
   '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)
End Sub

Other more advanced encryption methods are available in the encryption library:

The library is available here:


Still new to b4a.
Looking at the code from above....
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" ?


B4X founder
Staff member
Licensed User
Longtime User