B4J Code Snippet Use simple encryption for B4J, B4A and B4I (also in url)

Perhaps, there is someone who needs it: I made a simple encryption from string to string for b4j, b4a and b4i.
First, the string is encrypted with the b4xEncryption, after that, the bytes are changed to a base64-string and this string is changed to a url-able string.

Needed libraries:
B4J: Byteconverter, jB4XEncryption, jStringUtils
B4A: StringUtils, ByteConverter, B4XEncryption
B4I: iEncryption, iRandomAccessfile, iStringUtils

B4X:
Sub Process_Globals
    Dim Cipher As B4XCipher  'B4J and B4A
    Dim Cipher as Cipher    'B4I
    Dim su As StringUtils
    Dim bc As ByteConverter
    Dim Password As String ="blabla"
End Sub

Sub AppStart (Form1 As Form, Args() As String) / Activity_Create(FirstTime As Boolean) / Application_Start (Nav As NavigationController)
    Dim Ergebnis As String
    Ergebnis=encrypt("12345")
    Log(Ergebnis)
    Log(decrypt(Ergebnis))
End Sub

'Verschlüsselung der Zeichenkette mit AES, dann die verschlüsselten Bytes in Base64-String wandeln und anschliessend diesen String in ein URL-Format wandeln'
Sub encrypt(Zeichenkette As String) As String
    Return su.EncodeUrl(su.encodeBase64(Cipher.Encrypt(Zeichenkette.GetBytes("UTF8"), Password)), "UTF8")
End Sub

'Den URL-String wieder dekodieren, anschliessend die Base64-Zeichenkette in Bytes wandeln und dann entschlüsseln. Die entschlüsselten Bytes wieder in einen String wandeln und zurück geben'
'In B4I wird der String mit chr(0) aufgefüllt. Diese vor der Rückgabe wieder entfernen'
Sub decrypt (Zeichenkette As String) As String
   Zeichenkette=bc.StringFromBytes(Cipher.Decrypt(su.DecodeBase64(su.DecodeUrl(Zeichenkette, "UTF8")), Password),"UTF8")
   If Zeichenkette.Contains(Chr(0)) Then Zeichenkette=Zeichenkette.SubString2(0, Zeichenkette.IndexOf(Chr(0)))
   Return Zeichenkette
End Sub
 
Last edited:

schimanski

Well-Known Member
Licensed User
Longtime User
With the following code, it is possible to encrypt each file on one platform and decrypt it on another. At the end, I have attached a code module, like Erel says, which can use on all three platforms....

B4X:
Wandler.encryptFile(File.DirAssets, "test2.jpg", File.DirApp, "crypt_test2.jpg")
Wandler.decryptFile(File.DirApp, "crypt_test2.jpg", File.DirApp, "decrypt_test2.jpg")

B4X:
'Datei in ein Byte-Array lesen, verschlüsseln und verschlüsselt mit B4XObject in neue Datei schreiben, damit sie mit B4J, B4A und B4I kompatibel ist'
Sub encryptFile(DirSource As String, fileSource As String, DirTarget As String, FileTarget As String)
    Dim time As Long  =DateTime.Now
 
    Dim in As InputStream
    in = File.OpenInput(DirSource, fileSource)
    Dim out As OutputStream
    out.InitializeToBytesArray(0)
    File.Copy2(in, out)

    Dim data() As Byte
    data = out.ToBytesArray
    data=Cipher.Encrypt(data, Password)
 
    Dim Dokumentdatei As RandomAccessFile
    Dokumentdatei.Initialize(DirTarget, FileTarget, False)
    Dokumentdatei.WriteB4XObject(data, 0)
    Dokumentdatei.Close
 
    Log("Verschlüsseln dauert: " & (DateTime.Now-time))
    time=DateTime.Now
End Sub

'Verschlüsselte Datei mit B4XObject laden, entschlüsseln und als Byte-Array in ursprünglicher Form speichern'
Sub decryptFile(DirSource As String, FileSource As String, DirTarget As String, FileTarget As String)
    Dim time As Long  =DateTime.Now

    Dim data() As Byte
    Dim Dokumentdatei As RandomAccessFile
    Dokumentdatei.Initialize(DirSource, FileSource, False)
    data=Dokumentdatei.ReadB4XObject(0)
    Dokumentdatei.Close
 
    data=Cipher.Decrypt(data, Password)

    Dim in As InputStream
    in.InitializeFromBytesArray(data, 0, data.Length)
    Dim out As OutputStream
    out=File.OpenOutput(DirTarget, FileTarget, False)
    File.Copy2(in, out)
    out.Close

    Log("Entschlüsseln dauert: " & (DateTime.Now-time))
    time=DateTime.Now
End Sub
 

Attachments

  • Wandler.bas
    2.7 KB · Views: 537
Last edited:

mindful

Active Member
Licensed User
Hi, I am new to B4X and I am trying to get the grips of it and as it happens i am in the need to encrypt/decrypt a file (text, image, pdf) so my question is why do you copy the input stream to an output stream and use RandomAccessFile to read/write the file ?

Why not just use:
For encrypt:
B4X:
Dim data() As Byte = Chiper.Encrypt(Bit.InputStreamToBytes(File.OpenInput(DirSource, FileSource)), Password)
For Decrypt:
B4X:
Dim data() As Byte = Chiper.Decrypt(Bit.InputStreamToBytes(File.OpenInput(DirSource, FileSource)), Password)
And for writing file:
B4X:
    Dim out As OutputStream = File.OpenOutput(DirTarget, FileTarget, False)
    out.WriteBytes(data, 0, data.Length)
    out.Close

And also instead of bc.StringFromBytes(...) you could put directly BytesToString(...) and not rely on ByteConverter library for this task.

Are there any performance benefits or is just a matter of choice !?
 
Last edited:
Top