toby Well-Known Member Licensed User Longtime User Mar 15, 2022 #1 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.
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.
Q Quandalle Member Licensed User Mar 15, 2022 #2 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 MySqlAESEcryptation.zip 1.7 KB · Views: 432
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
toby Well-Known Member Licensed User Longtime User Mar 15, 2022 #3 Quandalle said: 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 Click to expand... 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? [CODE lang="b4x" title="mysql"]SELECT aes_decrypt('0x911ff5b9a15aae9b52e7e9fde75315b1', '123') , aes_decrypt( '911ff5b9a15aae9b52e7e9fde75315b1', '123'); Output: Null, Null [/CODE]
Quandalle said: 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 Click to expand... 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? [CODE lang="b4x" title="mysql"]SELECT aes_decrypt('0x911ff5b9a15aae9b52e7e9fde75315b1', '123') , aes_decrypt( '911ff5b9a15aae9b52e7e9fde75315b1', '123'); Output: Null, Null [/CODE]
toby Well-Known Member Licensed User Longtime User Mar 16, 2022 #4 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 [CODE lang="b4x" title="test is the column name"] SELECT *, aes_decrypt(test, '123') as test_decrypted FROM `tests` WHERE 1; [/CODE]
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 [CODE lang="b4x" title="test is the column name"] SELECT *, aes_decrypt(test, '123') as test_decrypted FROM `tests` WHERE 1; [/CODE]
toby Well-Known Member Licensed User Longtime User Mar 16, 2022 #5 @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, 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?
Q Quandalle Member Licensed User Mar 16, 2022 #6 @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: Mar 16, 2022
@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');