|
Encrypting & Decrypting Data in MySQL |
|
|
|
|
Written by martcon
|
|
Monday, 31 August 2009 08:26 |
|
Often, MySQL developers may wish to enable the addition of confidential data to the database. This data could be encrypted. For example, we can use AES Encryption as follows:
INSERT INTO LOGON VALUES('testuser',AES_ENCRYPT('testpassword','secretkey'));
In the example above we have a database table called LOGON with two columns USERNAME and PASSWORD. We add a user ('testuser') as normal but we encrypt the password using the AES_ENCRYPT() function. The first parameter is the password ('testpassword') and the second parameter is a secret key which will be used for decryption ('secretkey' in this example).
To retrieve as decrypted column value we can SELECT as follows:
SELECT AES_DECRYPT(PASSWORD,'secretkey') FROM LOGON
In the above we call the AES_DECRYPT() function for the PASSWORD column using a secret key (again, 'secretkey' in this example).
|