Mysql Decrypt

Keeping sensitive information for any organization is a must in this day and age, and encrypting this data is one of your many lines of defense. Below are simple one liners on how to use AES encryption and decryption using MySQL as your database. When you call the AES_ENCRYPT function, MySQL will automatically return the encrypted value in a binary form, I’ll show you how to convert this into a hexadecimal value which you will need to do if you ever backup your database to SQL files. I was so happy to find that MySQL had this function built in, I can’t tell you how many types I ran through the PHP api looking for something that could meet my needs, I never even bothered to do a search for it on MySQL.

Note: Do not encrypt passwords!

Yeah, you read that correctly. Not everything needs to be decrypted. Imagine if you had a rouge system administrator, dba, or even worse, a hacker. Passwords need to be and should be hashed. Think about it for a minute, the only reason you would want to encrypt a password is if you want to be able to decrypt it at a later time. There’s no reason to do that, if a user forgets his or her password, it should be reset, and then they should be forced to change it on their next login.

use it

SELECT AES_Encrypt(‘SECRET DATA’,’wo’)

UNION

SELECT AES_Decrypt(unhex(‘798d89dc836d1b3eb60caab12de238a5′),’wo’)

UNION

SELECT AES_Decrypt(AES_Encrypt(‘SECRET DATA’,’wo’),’wo’)

SELECT DES_Encrypt(‘SECRET DATA’,’wo’)

UNION

SELECT DES_Decrypt(unhex(‘ff745565cceeb045bef69ae563bf6bf497′),’wo’)

UNION

SELECT DES_Decrypt(DES_Encrypt(‘SECRET DATA’,’wo’),’wo’)