MySQL AES_ENCRYPT() function

AES_ENCRYPT() function

MySQL AES_ENCRYPT() function encrypts a string using AES algorithm.

AES stands for Advance Encryption Standard. This function encodes the data with 128 bits key length but it can be extended up to 256 bits key length. It encrypts a string and returns a binary string. The return result will be NULL when an argument is NULL.


AES_ENCRYPT(str, key_str);


Name Description
str A string which will be encrypted.
key_str String to encrypt str.

Syntax Diagram:

MySQL AES_ENCRYPT() Function - Syntax Diagram

MySQL Version: 5.6



SELECT AES_ENCRYPT('mytext', 'mykeystring');


The above MySQL statement encrypts the string 'mytext' with key myteststring.

Sample Output:

mysql> SELECT   AES_ENCRYPT('mytext', 'mykeystring');
| AES_ENCRYPT('mytext', 'mykeystring') |
| ­•›¨í ƒðbáÒ9•j                     | 
1 row in set (0.00 sec)

Example: MySQL aes_encrypt() function using table

Sample table: testtable


INSERT INTO testtable VALUE(AES_ENCRYPT('mytext','passw'));


The above MySQL statement encrypts the string mytext with passw and inserts the encrypted string into the table 'testtable'.

Sample Output:

mysql> INSERT INTO testtable VALUE(AES_ENCRYPT('mytext','passw'));
Query OK, 1 row affected (0.04 sec)

Online Practice Editor:

Previous: AES_DECRYPT()

Follow us on Facebook and Twitter for latest update.

We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook