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ê??.ÑâiÃg)I
logistic.‰p“Ö?ñù)åÖêõ??­
advertizingº¤çoÅc§Ò5H?×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ê??.ÑâiÃg)Imanagement
logistic.‰p“Ö?ñù)åÖêõ??­logistic
advertizingº¤çoÅc§Ò5H?×C:ï×advertizing
accounting@iT™.Àë..áw?.Õ™3accounting
sales÷ðô:'?.¼Ì.ä ?ãIæsales

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