w3resource

MySQL des_encrypt() function

des_encrypt() function

MySQL DES_ENCRYPT() encrypts a string with a key Triple-DES algorithm.

This function works only with Secure Sockets Layer (SSL) if support for SSL is available in MySql configuration.

Syntax:

DES_ENCRYPT(str,[{key_num | key_str}]);

Arguments:

Name Description
str A string which is to be encrypted.
key_num A number from 0 to 9 from DES key file.
key_str A string to encrypt str.

MySQL Version: 8.0


Example: MySQL des_encrypt() function

Code:

SELECT DES_ENCRYPT('mytext',5),DES_ENCRYPT('mytext','mypassward');

Explanation:

The above MySQL statement encrypts the string mytext with key number 5; for the second instance of the function, mytext is encrypted with mypassword.

Output:

mysql> SELECT DES_ENCRYPT('mytext',5),DES_ENCRYPT('mytext','mypassward');
+-------------------------+------------------------------------+
| DES_ENCRYPT('mytext',5) | DES_ENCRYPT('mytext','mypassward') |
+-------------------------+------------------------------------+
| …ÿc}æ¤~               | ÿ]ï×ñ”Å                          | 
+-------------------------+------------------------------------+
1 row in set (0.00 sec)

Example : MySQL des_encrypt() function using table

Sample table: testtable


Code:

INSERT INTO testtable
VALUE(DES_ENCRYPT('mydesencrypttext','mydespassw'));

Explanation:

The above MySQL statement will insert encrypted data into table 'testtable'.

Output:

mysql> INSERT INTO testtable
    -> VALUE(DES_ENCRYPT('mydesencrypttext','mydespassw'));
Query OK, 1 row affected (0.00 sec)

Previous: DES_DECRYPT()
Next: ENCODE()



Follow us on Facebook and Twitter for latest update.