Android Tutorial Encrypting information with RandomAccessFile library

Erel

Administrator
Staff member
Licensed User
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.basic4ppc.com/forum/basic4android-getting-started-tutorials/14965-android-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.basic4ppc.com/forum/additional-libraries-official-updates/6839-base64-encryption-library.html

The library is available here: http://www.basic4ppc.com/forum/additional-libraries-official-updates/11566-randomaccessfile-v1-30-writeenctypredobject.html
 

AscySoft

Active Member
Licensed User
Right now I am working on exporting to some text files...

I use encryption method describe by you, above using WriteEncryptedObject.

The text files are OK, but the question is..
How do I decrypt them on Windows PC? (There I use net framework 2.0 application)
Could you enlighten me? Thanks, I know is not B4A, but is related anyway
 

Erel

Administrator
Staff member
Licensed User
You will not be able to use a file written with WriteEncryptedObject on the PC. You will need to encrypt the data yourself with the Encryption library.
 

chrjak

Active Member
Licensed User
It does not work...
There is a "negative array size (-1) exception" while reading it.

Code:
Write
B4X:
If File.Exists(File.DirInternal, "file.dat") = False Then
File.WriteString(File.DirInternal, "file.dat","")
layout.Initialize(File.DirInternal, "file.dat",False)
layout.WriteObject("start0",False,0)
Dim pp As PhoneId
layout.WriteObject(pp.GetDeviceId,True,1)
layout.WriteObject("file",True,2)
layout.close
End If
Read
B4X:
layout.Initialize(File.DirInternal, "file.dat",True)
Dim stts As String = layout.ReadObject(0) 'Here the error
If stts = "start1" Then
It is in the same sub.
 

Erel

Administrator
Staff member
Licensed User
It doesn't work when it is not used correctly ;)

You are writing the objects one over another.
It should be:
B4X:
layout.WriteObject("start0", False, layout.CurrentPosition)
..
layout.WriteObject(pp.GetDeviceId,True, layout.CurrentPosition)
layout.WriteObject("file",True, layout.CurrentPosition)
I recommend you to use KeyValueStore class. It is easier.
 

Erel

Administrator
Staff member
Licensed User
No. RandomAccessFiles work with bytes. You should use CurrentPosition to start from the last position.
 

chrjak

Active Member
Licensed User
Ok- it worked now on my emulator.
but on my S4 there are Errors and only the first thing can be read!?
 

Erel

Administrator
Staff member
Licensed User
You should post the error message and the relevant code.
 

chrjak

Active Member
Licensed User
The Error is a BufferUnderflowException.
The Code is like:
For i to 5 (this is ok)
list.add(raf.readobject(layout.currentposition))
next
The 1rst entry is added successfully but then the exception is being occurred.
 

Erel

Administrator
Staff member
Licensed User
The problem is somewhere else. You should create a small project that demonstrates the error and upload it.

As I wrote before it will be simpler to use KeyValueStore. Also note that you can write and read the whole list as a single object.
 

chrjak

Active Member
Licensed User
I can't demonstrate it :(

I took a look at it but it is not useful for me. Sorry. But you did a great job!

Hmm yes... That is easier i think...
 

chrjak

Active Member
Licensed User
I think it is solved... Maybe it was a wrong object... Thank you again so much, Erel!
 

Bpick

Member
Licensed User
Once I decrypt the file, say, an Excel file, how would I write it unencrypted so I can work with it during the day and then encrypt it again to mail it to my friends once I'm done?
 

Bpick

Member
Licensed User
I understand.
But when I get the file decrypted, do I use RAF.WriteObject(list2,true,raf.currentposition)?
 

Erel

Administrator
Staff member
Licensed User
You don't decrypt the file. You read the object from the encrypted file and then work with the object.
 

locxtronic

Member
Licensed User
how can use this encryption method without needed to create file in any directory? the reason why im asking this is because i just wanna use this within the encrypted data that i get from online database and store it in string variable and display it to screen using label after decrypt.
 
Top