Share My Creation Protect your Data with MYSQL ENCRYPTION

I make use of the MySql Function's like AES_ENCRYPT, AES_DECRYPT, HEX, UNHEX and SHA2
Maybe some Code snipper can help someone else too.
B4X:
Sub TestPasswordFromMysql(Pass As String, Key As String, DECRYPT As Boolean )
Dim MYSQL As String
Dim tmpPass As String

'Encrypt the Password
MYSQL = "SELECT HEX(AES_ENCRYPT('" & Pass & "', SHA2('" & Key & "', 512)));"
    ConnectToDBPass
    tmpPass = SQLDBPASS.ExecQuerySingleResult(MYSQL)
    SQLDBPASS.close
    Log(tmpPass)
    If DECRYPT = False Then
        Return tmpPass
    End If
'Test if all is working
'Decrypt the Password
'MYSQL = "SELECT (AES_DECRYPT('" & tmpPass & "','" & Key & "'));"
If DECRYPT = False Then
    MYSQL = "SELECT (AES_DECRYPT(UNHEX('" & tmpPass & "'), SHA2('" & Key & "', 512))) AS PASS;"
Else
    MYSQL = "SELECT (AES_DECRYPT(UNHEX('" & Pass & "'), SHA2('" & Key & "', 512))) AS PASS;"
End If
    ConnectToDBPass
    tmpPass = SQLDBPASS.ExecQuerySingleResult(MYSQL)
    SQLDBPASS.close
    Log(tmpPass)
Return tmpPass
End Sub

This way the code is compatible with PHP and you have not to calculate key length and thinks like this.
 

swissmade

Well-Known Member
Licensed User
For using this way you need MySql 5.5 and above.
To Test you have the good MYSql version use SELECT HEX(AES_ENCRYPT('Pass', SHA2('Key', 512)));
 
Top