B4J Question b4xencryption

le_toubib

Active Member
Licensed User
Longtime User
hi all
in mysql i encrypted a field using the sql :
UPDATE `TABLE` SET `FIELD1` = AES_ENCRYPT(`FIELD2`,'PASSWORD123456')
(both fields are varchar(100))

I fetched data using php json encode
in b4j i tryed to decrypt using the code :
B4X:
Sub decrypt (s As String) As String
    Dim su As StringUtils
            Dim Cipher As B4XCipher
             Dim bc As ByteConverter   
   s=bc.StringFromBytes(Cipher.Decrypt(s.GetBytes("UTF8"), "PASSWORD123456"),"UTF8")
   Return s
End Sub

but i get the error :
what am i missing ?
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
It is only part of the answer but. s.getBytes() is not enough. you must decode it with base64 like this.

B4X:
Dim byt() As Byte = SU.DecodeBase64(text)
    Dim bytEn() As Byte = Cipher.Decrypt(byt,"PASSWORD123456")
    Return BytesToString(bytEn,0,bytEn.Length,"UTF-8")
 
Upvote 0

le_toubib

Active Member
Licensed User
Longtime User
Thanks for reply ...
Should I encode it in base64 in mysql as well ?
 
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
That is why i said that it was a partial answer. i know that for encryption to work FROM and TO B4x, you should use base64, but i do not know if this line is already doing it.

AES_ENCRYPT(`FIELD2`,'PASSWORD123456')
 
Upvote 0

le_toubib

Active Member
Licensed User
Longtime User
That is why i said that it was a partial answer. i know that for encryption to work FROM and TO B4x, you should use base64, but i do not know if this line is already doing it.

AES_ENCRYPT(`FIELD2`,'PASSWORD123456')
I used
B4X:
  Update table SET `FIELD 1` = TO_BASE64 (AES_ENCRYPT(`FIELD2`,'PASSWORD123456'))
In mysql . But still some result ..
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…