PHP/MySQL ->
MySQL string encryption. Example 1

In the following query we use AES_ENCRYPT() function to encrypt Department name. AES_ENCRYPT() function uses the official AES (Advanced Encryption Standard) algorithm employing a 128-bit key:

SELECT DeptName, AES_ENCRYPT(DeptName, 'bampi')
FROM Departments

/* you can chose complicated key like this */

SELECT DeptName, AES_ENCRYPT(DeptName, 'Kx73oR4g9Bz')
FROM Departments

/* key can be number */

SELECT DeptName, AES_ENCRYPT(DeptName, '987')
FROM Departments

The results will be as following:
DeptNameAES_ENCRYPT(DeptName, '987')
management?rJh8??.ig)I
logistic.p?)??
advertizingoc5H?C:
accounting@iT...w?.ՙ3
sales:'?.. ?I


The way back from encrypted state is easy with AES_DECRYPT() that returns to Department name it's human meaning:
SELECT DeptName, AES_ENCRYPT(DeptName, '987') AS NameEncrypted,
AES_DECRYPT(AES_ENCRYPT(DeptName, '987'),'987') AS NameDecrypted
FROM Departments

The results will be as following:
DeptNameNameEncryptedNameDecrypted
management?rJh8??.ig)Imanagement
logistic.p?)??logistic
advertizingoc5H?C:advertizing
accounting@iT...w?.ՙ3accounting
sales:'?.. ?Isales

MySQL Help says that AES_ENCRYPT() and AES_DECRYPT() can be considered the most cryptographically secure encryption functions available for the current version (5.1) of this DBMS.


sqlexamples.info