MySQL | AES_ENCRYPT ( ) Function

The MySQL AES_ENCRYPT function is used for encrypting a string using Advanced Encryption Standard (AES) algorithm. The MySQL AES_ENCRYPT 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 value returned by the AES_ENCRYPT function is a binary string or NULL if the argument in NULL. The AES_ENCRYPT function accepts two parameters which are the encrypted string and a key string used to encrpyt the string.

Syntax:

AES_ENCRYPT(str, key_str)

Parameters Used:

  • str – It is used to specify the plain string.
  • key_str – It is used to specify the String which is used to encrypt the str.

Return Value:
The AES_ENCRYPT function in MySQL returns a binary string.



Supported Versions of MySQL:

  • MySQL 5.7
  • MySQL 5.6
  • MySQL 5.5
  • MySQL 5.1
  • MySQL 5.0
  • MySQL 4.1

Example-1: Implementing AES_ENCRYPT function on a string.

SELECT
AES_ENCRYPT('ABC', 'key'); 

Output:

\\YJ??f&K?M?q?* 

Example-2: Implementing AES_ENCRYPT function on a bigger string.

SELECT 
AES_ENCRYPT('geeksforgeeks', 'key'); 

Output:

2G???B?????*?? 

Example-3: Implementing AES_ENCRYPT function on a NULL string.

SELECT  
(AES_ENCRYPT(NULL, 'key'); 

Output:

NULL 
My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.