w3resource
MYSQL Tutorial

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.

Syntax:

AES_ENCRYPT(str, key_str);

Arguments

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


Example:

Code:

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

Explanation

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


Code:

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

Explanation

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)

Previous: AES_DECRYPT()
Next: COMPRESS()