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 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)
Online Practice Editor:
Previous: AES_DECRYPT()
Next: COMPRESS()
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
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