Android Tutorial Encrypting information with RandomAccessFile library

Discussion in 'Tutorials & Examples' started by Erel, Sep 18, 2011.

  1. Erel

    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/basi...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:
    Code:
    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/addi...l-updates/6839-base64-encryption-library.html

    The library is available here: http://www.basic4ppc.com/forum/addi...domaccessfile-v1-30-writeenctypredobject.html
     
    sirjo66 and Daniel-White like this.
  2. AscySoft

    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
     
  3. Erel

    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.
     
  4. AscySoft

    AscySoft Active Member Licensed User

    Thanks, I'm working on that.
     
  5. chrjak

    chrjak Active Member Licensed User

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

    Code:
    Write
    Code:
    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
    Code:
    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.
     
  6. Erel

    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:
    Code:
    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.
     
    Peter Simpson likes this.
  7. chrjak

    chrjak Active Member Licensed User

    it writes backwards??? (1,0 ,-1)
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    No. RandomAccessFiles work with bytes. You should use CurrentPosition to start from the last position.
     
  9. chrjak

    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!?
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    You should post the error message and the relevant code.
     
  11. chrjak

    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.
     
  12. Erel

    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.
     
  13. chrjak

    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...
     
  14. chrjak

    chrjak Active Member Licensed User

    I think it is solved... Maybe it was a wrong object... Thank you again so much, Erel!
     
  15. Bpick

    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?
     
  16. Erel

    Erel Administrator Staff Member Licensed User

    You read the data with ReadEncryptedObject.
     
  17. Bpick

    Bpick Member Licensed User

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

    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.
     
  19. Bpick

    Bpick Member Licensed User

    I think I understand.
     
  20. locxtronic

    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.
     
Loading...