MySQL | DES_ENCRYPT ( ) Function

The MySQL DES_ENCRYPT function is used for encrypting a string using DES(Data Encryption Standard) algorithm. The MySQL DES_ENCRYPT function uses a key to encrypt a string.

The value returned by the DES_ENCRYPT function is an encrypted string or NULL. The DES_ENCRYPT function accepts three parameters which are the plain text string and a key string and a key number to encrypt the string.

Syntax:

DES_ENCRYPT(plaintext_string, [key_number | key_string]);

Parameters Used:

  • plaintext_string – It is used to specify a string which is to be encrypted.
  • key_number – It is used to specify a number in the range of 0 to 9 from the DES key file.
  • key_string – It is used to specify a string which is used for encrypting the plain-text string.

Return Value:
The DES_ENCRYPT function in MySQL returns an encrypted 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 DES_ENCRYPT function on a string by only passing the key number argument.

SELECT 
DES_ENCRYPT('geeksforgeeks', 5); 

Output:

??p4???c????-? 

Example-2: Implementing DES_ENCRYPT function on a string by passing both the key number and the key string arguments.

SELECT 
DES_ENCRYPT('geeksforgeeks', 7), 
DES_ENCRYPT('geeksforgeeks', 'TestPassward'); 

Output:

??p4???c????-?    ??]?? ???{?}\\?t? 

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

SELECT 
DES_ENCRYPT(NULL, 7); 

Output:

NULL 
My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

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.