Open In App

MySQL | DES_ENCRYPT ( ) Function

Last Updated : 23 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In today’s world, keeping data safe is important for any database. MySQL, one of the most popular database systems, offers various tools to secure your information. Among these is the DES_ENCRYPT() function, which uses the Data Encryption Standard (DES) algorithm to encrypt your data. This function adds an extra layer of protection by converting plaintext into an encrypted format, making it unreadable without proper decryption.

In this article, we will explore the DES_ENCRYPT() function, and its syntax, and provide examples to help you understand how it works.

MySQL DES_ENCRYPT ( ) Function

In the digital age, securing sensitive information is very important. The ‘DES_ENCRYPT' function in MySQL encrypts a string using the Data Encryption Standard (DES) algorithm, providing an additional layer of security for your data. It uses a specified key to perform the encryption, returning an encrypted string or NULL.

Syntax:

DES_ENCRYPT(plaintext_string, [key_number | key_string]);

Parameters:

  • plaintext_string: Specifies the string that is to be encrypted.
  • key_number: Specifies a number in the range of 0 to 9 from the DES key file.
  • key_string: Specifies a string used for encrypting the plaintext string.

Return Value

The DES_ENCRYPT function in MySQL returns an encrypted string or ‘NULL' if the encryption fails.

Supported Versions of MySQL:

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

Examples of MySQL DES_ENCRYPT ( ) Function

Example 1: Using Key Number

We are implementing ‘DES_ENCRYPT‘ function on a string by only passing the key number argument.

SELECT 
DES_ENCRYPT('geeksforgeeks', 5);

Output:

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

Explanation: The string 'geeksforgeeks' is encrypted using the key number 5. The output is an encrypted binary string that appears as garbled characters.

Example 2: Using Key String

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? 

Explanation: The first part of the output is the result of encrypting 'geeksforgeeks' with the key number 7. The second part of the output is the result of encrypting 'geeksforgeeks' with the key string 'TestPassword'. Both encrypted outputs are displayed as garbled characters due to the binary nature of the encryption.

Example 3: Encrypting a NULL String

In this example, the ‘DES_ENCRYPT' function attempts to encrypt a ‘NULL' string.

SELECT 
DES_ENCRYPT(NULL, 7);

Output:

NULL 

Explanation: Since the plaintext string is ‘NULL', the output is also ‘NULL'. The function cannot encrypt a ‘NULL' value, hence it returns ‘NULL'.

Conclusion

The ‘DES_ENCRYPT‘ function in MySQL is a powerful tool for encrypting strings using the DES algorithm. It accepts either a key number or a key string for encryption and returns an encrypted string or NULL if the encryption is not possible. While DES encryption is available in MySQL, it is important to note that DES is considered weak by modern cryptographic standards, and stronger algorithms like AES (Advanced Encryption Standard) are recommended for better security. By understanding and using the DES_ENCRYPT function, you can enhance the security of sensitive data stored in your MySQL databases.



Next Article
Article Tags :

Similar Reads