Android Question Encrypt a Small Text File on PC And Use it in a B4A App

Mahares

Well Known Member
Licensed User
I have a very small text file comprising of a list that I tried to encrypt using Notepad++ and copied via USB to the device’s root directory. On the device, I tried to open it using RandomAccessFile lib: list = raf.ReadEncryptedObject("password", raf.CurrentPosition).
The app crashes: “Unfortunately 'App Label' has stopped”.
Can it be done without having to encrypt it on the device first using: raf.WriteEncryptedObject(……
Thank you
 

Jeffrey Cameron

Well-Known Member
Licensed User
You might try wrapping your "list = raf.Read...." line in a Try/Catch block and writing the error to the log so you can see what is causing the crash.

How are you using Notepad ++ to encrypt the text? Do you have a 3rd-party plug-in that does it? And if you do, are you sure it is using the same encryption method as the RandomAccessFile library?
 

Mahares

Well Known Member
Licensed User
How are you using Notepad ++ to encrypt the text? Do you have a 3rd-party plug-in that does it? And if you do, are you sure it is using the same encryption method as the RandomAccessFile library?
@Jeffrey Cameron I used Notepad++ encryption plug-in and tried all these 3 encoding options, but I get the same result. The error in the log is java.lang.OutOfMemoryError, but the file is merely 0.4KB. It does not make sense:
upload_2015-4-3_14-22-52.png

@KMatle: I have used the ramdomAccessFile lib. I have actually copied the text file to the device without encryption, then encrypt it using the lib, then save it to PC encrypted, then deleted the data from the device of the code portion not encrypted, but that is not what I want to do. I am looking for a way to encrypt on the PC first so it is compatible with the RandomAccessFile lib.
Thank you
 

Attachments

Last edited:

Jeffrey Cameron

Well-Known Member
Licensed User
When I needed to do this, I used VB.NET to encrypt it using the .NET System.Security.Cryptography.TripleDES class and then used the same method with the same initialization vector/key values in B4A. For example, here's my TripleDES class:
B4X:
'Class module
'  Requires Libraries: 
'      Encryption  v1.10 
'      ByteConverter  v1.10
'
Sub Class_Globals
 
End Sub

Public Sub Initialize
End Sub

#Region " Triple DES encryption code "
' sIV must be 8 bytes, sKEy must be 16 bytes, returns base-64 encoded encrypted string
Public Sub TripleDESEncrypt(sIV As String, sKey As String, dataToEncrypt As String) As String  'ignore
    Dim kg As KeyGenerator
    Dim C As Cipher
    Dim B64 As Base64
    Dim Bconv As ByteConverter
    Dim data(0) As Byte
    Dim iv(0) As Byte
    If sIV.Length <> 8 OR sKey.Length <> 16 Then
        Return Null
     End If

     iv = Bconv.StringToBytes(sIV, "ASCII")
  
     C.Initialize("DESEDE/CBC/PKCS5Padding")  
     C.InitialisationVector = iv
     kg.Initialize("DESEDE") 
  
      kg.KeyFromBytes(Bconv.StringToBytes(sKey, "ASCII"))
      data = Bconv.StringToBytes(dataToEncrypt, "ASCII")  
      data = C.Encrypt(data, kg.Key, True) 
      Return B64.EncodeBtoS(data, 0, data.Length)
End Sub

'  sIV must be 8 bytes, sKEy must be 16 bytes, returns plain-text decrypted string
Public Sub TripleDESDecrypt(sIV As String, sKey As String, Base64EncryptedData As String) As String  'ignore
    Dim kg As KeyGenerator
    Dim C As Cipher
    Dim B64 As Base64
    Dim Bconv As ByteConverter
    Dim data(0) As Byte
    Dim iv(0) As Byte
    If sIV.Length <> 8 OR sKey.Length <> 16 Then
       Return Null
    End If
    Try
        iv = Bconv.StringToBytes(sIV, "ASCII")
  
        C.Initialize("DESEDE/CBC/PKCS5Padding")  
        C.InitialisationVector = iv
        kg.Initialize("DESEDE")  
        kg.KeyFromBytes(Bconv.StringToBytes(sKey, "ASCII"))
  
        data = B64.DecodeStoB(Base64EncryptedData)
        data = C.Decrypt(data, kg.Key, True) 
        Return Bconv.StringFromBytes(data, "ASCII")
    Catch
        LogColor("Error decrypting: " & LastException, Colors.Red)
    End Try
    Return ""
End Sub

#End Region
 

Mahares

Well Known Member
Licensed User
Maybe I should explain myself better: Has anyone been able to use Notepad++ encryption methods that I can I use to encrypt a small text file on the PC and consequently read it on the tablet using RandomAccessFile: raf.ReadEncryptedObject("password", raf.CurrentPosition). If so, what parameters in the encryption menu did you select.
 

Peter Simpson

Expert
Licensed User
What exactly are you trying to hide, is it a username or password?

If you just want to scrabble some basic data, you can always encode on the PC using Base64 and then decode the Base64 on any Android device. It's not secure as in encryption but it depends what's in the original file.

Is the content in the original file suppose to be secure?
 

Mahares

Well Known Member
Licensed User
What exactly are you trying to hide, is it a username or password?
It is a list of user names and passwords that need encrypted. I have a feature that extracts the user name from Google account and applies the corresponding password that is looked up from this preferred encrypted list so an automatic email SMTP with an attachment is sent to a given individual via a daily run service. I need this text file with the list encrypted when stored in the device so I can read it with RandomAccessFile..
 

Mahares

Well Known Member
Licensed User
Is there any way to encrypt a text file that has a list of users and passwords on PC using any program, copy it to device via USB and open it with RandomAccessFile like this:
list = raf.ReadEncryptedObject("password", raf.CurrentPosition).
 

fixit30

Active Member
Licensed User
Your best option would be to create a small B4J application to create the encrypted file using the same RandomAccessFile library
 

Mahares

Well Known Member
Licensed User
Your best option would be to create a small B4J application to create the encrypted file using the same RandomAccessFile library
Thank you for the suggestion. I wanted to avoid any duplicate work at the PC and was looking for an easy solution like Notepad++ Encryption. Actually what I did is this which works, but it is not my preferred solution:
1. Created the list on the device since it is small, and encrypted it via RandowmAccessfile by writing it to a text file.
2. Deleted the list from the app and started using the encrypted file.
It works, but it is not my preferred way to approach the problem.
 
Last edited:
Top