Android Question Encrypt Text with B4XCipher

hanyelmehy

Active Member
Licensed User
Longtime User
i need to Encrypt Text with B4XCipher ,save it to sqlite (as string) ,then able to get it back
this code not work with me
B4X:
Sub MakeText(text As String) As String
    Dim c As B4XCipher,CC() As Byte
    CC=c.Encrypt(text.GetBytes("utf8"),PPWW)
    Return BytesToString(CC,0,CC.Length, "utf8")
End Sub
Sub GetMyText(tx1 As String) As String
    Dim En1() As Byte =tx1.GetBytes("utf8")
   
    Dim c As B4XCipher
    Dim b() As Byte = c.Decrypt(En1,PPWW)
    Return BytesToString(b, 0, b.Length, "utf8")
End Sub
 

hanyelmehy

Active Member
Licensed User
Longtime User
Solved ,this code work
B4X:
Sub MakeText(text As String) As String
    Dim c As B4XCipher,CC() As Byte
    CC=c.Encrypt(text.GetBytes("UTF-8"),PPWW)
    Dim B64 As StringUtils
    Return B64.EncodeBase64(CC)
End Sub
Sub GetMyText(tx1 As String) As String
    Dim B64 As StringUtils
    Dim En1() As Byte =B64.DecodeBase64(tx1)
    Dim c As B4XCipher
    Dim b() As Byte = c.Decrypt(En1,PPWW)
    Return BytesToString(b, 0, b.Length, "UTF-8")
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
You should not use BytesToString on the encrypted data. It is not a String.
Convert the encrypted data to base64 and save this base64 string to your Database.

When reading from Database you start with decoding the base64 to a bytearray. Then decrypt this bytearray.
 
Upvote 0

hanyelmehy

Active Member
Licensed User
Longtime User
Can you link the b4xcipher i cant find it in the search @-@
 
Upvote 0
Top