B4J Question String to bytes handling with encryption

ThRuST

Well-Known Member
Licensed User
Longtime User
Hi! I felt a bit embarrassing to post this question since I wrapped the B4XAES encryption library,
but "if you don't ask you'll never learn" so here goes. I'm having problems to load an encrypted string and decode it.
Please provide examples how you handle strings to byte arrays to feed the encryption methods. In this case B4XEncryption
library
(B4XCipher) using AES algorithm. Let's tame the encryption mysteries once and for all

I'd like to add the I've come across posts were it is recommended to use StringUtils to encode/decode strings using EncodeBase64/DecodeBase64
when storing the encrypted string in an Array of Bytes. How to deal with this conversion? Examples?
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
#AdditionalJar: bcprov-jdk15on-154
Sub Process_Globals
    
End Sub

Sub AppStart (Args() As String)
    Dim p As String = "123456"
    Dim s As String = "top secret"
    Dim enc As String = Encrypt(s, p)
    Log(enc)
    Dim dec As String = Decrypt(enc, p)
    Log(dec)
End Sub

Private Sub Encrypt (Text As String, Password As String) As String
    Dim c As B4XCipher
    Dim b() As Byte = c.Encrypt(Text.GetBytes("utf8"), Password)
    Dim su As StringUtils
    Return su.EncodeBase64(b)
End Sub

Private Sub Decrypt(EncryptedText As String, Password As String) As String
    Dim c As B4XCipher
    Dim su As StringUtils
    Dim enc() As Byte = su.DecodeBase64(EncryptedText)
    Dim dec() As Byte = c.Decrypt(enc, Password)
    Return BytesToString(dec, 0, dec.Length, "utf8")
End Sub

Output:

8bTS+I0WS6PZuDCkbslVM73jTp3lrUJegSthoL06nAmwwWjFHiPhmg==
top secret
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
I have now updated B4XAES library to v2.0

Find it here

Check it out on all B4X platforms. I hope you all like the implementation of Erel's powerful solution in this b4xlib.

Thanks,
Roger
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…