Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

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" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="https://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" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="https://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>

Online Practice Editor:


Previous: DECODE()
Next: DES_ENCRYPT()