Translate java code to B4A: Mysql aes_encrypt() and aes_decrypt() compatible

toby

Well-Known Member
Licensed User
Longtime User
Requirements
1. aes_encrypt(): encrypted data can be decrypted using mysql's aes_decrypt() function
2. aes_decrypt(): It can decrypt data encrypted using mysql's aes_encrypt() function.
3. Initialization vector not used.
4. The two functions should produce same results as corresponding mysql functions.

Below are some sample data:
B4X:
SELECT aes_encrypt('abc', '123'), aes_decrypt(aes_encrypt('abc', '123'), '123');
result: 0x911ff5b9a15aae9b52e7e9fde75315b1, abc

If you're interested, please send me a private message with your asking price. I'll pay for the source code via paypal.

More detail: details

Solution in Java is here. All you need to do is to convert the code to B4A.
 

Quandalle

Member
Licensed User
attached is a module that does AES encryption/decryption for MySQL encrypted keys

B4X:
    Dim cipher As MySqlAES
    cipher.Initialize
    
    Log(cipher.Encrypt("abc", "123"))
    Log(cipher.Decrypt(cipher.Encrypt("abc","123"),"123"))

Ouput is
B4X:
911ff5b9a15aae9b52e7e9fde75315b1
abc
 

Attachments

  • MySqlAESEcryptation.zip
    1.7 KB · Views: 143

toby

Well-Known Member
Licensed User
Longtime User
attached is a module that does AES encryption/decryption for MySQL encrypted keys

B4X:
    Dim cipher As MySqlAES
    cipher.Initialize
   
    Log(cipher.Encrypt("abc", "123"))
    Log(cipher.Decrypt(cipher.Encrypt("abc","123"),"123"))

Ouput is
B4X:
911ff5b9a15aae9b52e7e9fde75315b1
abc
Quandalle,

Thank you very much for the code!!

I copied and pasted the encrypted data to run a mysql query and it returned Null. I notice that the mysql encrypted data has "0x" prefix, so I tried it as well and both return Null. Could you kindly take a look to see what I was missing?

mysql:
SELECT aes_decrypt('0x911ff5b9a15aae9b52e7e9fde75315b1', '123')
     , aes_decrypt(  '911ff5b9a15aae9b52e7e9fde75315b1', '123');
    
Output: Null, Null
 

toby

Well-Known Member
Licensed User
Longtime User
My solution is to first save the encrypted data to a mysql table column of VarBinary(50) data type, then query the table as follows
test is the column name:
SELECT *, aes_decrypt(test, '123') as  test_decrypted
FROM `tests` WHERE 1;
 

toby

Well-Known Member
Licensed User
Longtime User
@Quandalle,

Your code works finally now! Thank you very much for sharing the code! I appreciate it indeed. How do I send you some coffee money?
 

Quandalle

Member
Licensed User
@toby
B4X:
SELECT aes_decrypt('0x911ff5b9a15aae9b52e7e9fde75315b1', '123')
     , aes_decrypt(  '911ff5b9a15aae9b52e7e9fde75315b1', '123');
 
Output: Null, Null

This execution which gives "Null" is normal because aes_encrypt returns a binary string (and aes_decrypt uses the binary string as a parameter).
It is probably just a display option in your MySQL environment that makes the display in hexadecimal and not in binary form

Also to decode a string written in hexdecimal you have to complete with a call to UNHEX which will transform the hexadecimal string into a binary string

B4X:
SELECT aes_decrypt(UNHEX('911ff5b9a15aae9b52e7e9fde75315b1'),'123');
 
Last edited:
Top