Open In App

MySQL | DES_ENCRYPT ( ) Function

Last Updated : 13 Nov, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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 

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads