w3resource
MYSQL Tutorial

MySQL DES_DECRYPT() function

DES_DECRYPT() function

MySQL DES_DECRYPT() function decrypts an encrypted string and returns the original string.

Syntax:

DES_DECRYPT(crypt_str, [key_str]);

Arguments

Name Description
crypt_str An encrypted string.
key_str String to decrypt crypt_str.

Syntax Diagram:

MySQL DES_DECRYPT() Function - Syntax Diagram

MySQL Version: 5.6


Example:

Code:

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

Explanation

The above MySQL statement decrypts the encrypted string 'mytext' as specified in the argument and returns the original string.

Sample Output:

mysql> SELECT DES_DECRYPT(DES_ENCRYPT('mytext','mypassward'),'mypassward');
+--------------------------------------------------------------+
| DES_DECRYPT(DES_ENCRYPT('mytext','mypassward'),'mypassward') |
+--------------------------------------------------------------+
| mytext                                                       | 
+--------------------------------------------------------------+
1 row in set (0.01 sec)

PHP script

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>example-des_decrypt - php mysql examples | w3resource</title>
</head>
<body>
<?php
echo "<h2>Decrypting the encrypt string 'mytext' :</h2>";
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='100' align='center'>DES_DECRYPT(DES_ENCRYPT('mytext','mypassward'),'mypassward')</td>";
echo "</tr>";
include("../dbopen.php");
$result = mysql_query("SELECT DES_DECRYPT(DES_ENCRYPT('mytext','mypassward'),'mypassward')");
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align='center' width='200'>" . $row["DES_DECRYPT(DES_ENCRYPT('mytext','mypassward'),'mypassward')"] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>

View the example in browser

Example of MySQL des_decrypt() function using table

Sample table: testtable


Code:

SELECT description,DES_DECRYPT(description,'mydespassw')
FROM testtable;

Explanation

The above MySQL statement retrieves the decrypted data from encrypted 'description' column from 'testtable'.

Output:

des_decrypt function

PHP script

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>example1-des_decrypt - php mysql examples | w3resource</title>
</head>
<body>
<?php
echo "<h2>Retrieving the decrypted data from encrypted 'description' column from 'testtable' :</h2>";
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='100' align='center'>Description</td><td width='100' align='center'>DES_DECRYPT(description,'mydespassw')</td>";
echo "</tr>";
include("../dbopen.php");
$result = mysql_query("SELECT description,DES_DECRYPT(description,'mydespassw') 
FROM testtable");
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align='center' width='200'>" . $row['description'] . "</td>";
echo "<td align='center' width='200'>" . $row["DES_DECRYPT(description,'mydespassw')"] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>

Previous: DECODE()
Next: DES_ENCRYPT()