How to decrypt md5 password in PHP? Step by Step Guide | Edureka (2024)

Become a Certified Professional

One of the most important parts of a website is the authentication system and it is commonplace for developers to commit mistakes leaving out vulnerabilities for others to exploit. Since PHP is a server-side scripting language, it is responsible for all the back-end functionalities required by the website. In this article, we will learn how to decrypt md5 password in PHP in the following sequence:

Let’s begin.

Why do we need MD5 in PHP?

One basic example could be storing and using user passwords in its true form, in this situation an unauthorized person might get access to the database and the whole system is compromised. To prevent this situation password hashing is used. Password hashing can be defined as a method that takes the user password or string and encrypts it into a fixed-length password, PHP has a few functions to achieve the same like md5(), sha1(), hash().

What is MD5 hashing?

MD5 hashing algorithm generates a 32 characters string (hexadecimal) for any word or phrase we give in the input. We can even encrypt an entire file into an MD5 hash. The algorithm can also be used for digital signature applications, where a large file is compressed in a secure manner and then encrypted with the help of a private key.

How to use MD5 in PHP?

To calculate the MD5 hash of a string PHP has a pre-defined function md5(). The md5() function calculates the MD5 hash of a string input and returns the hash hexadecimal number. The md5() function uses the MD5 Message-Digest Algorithm.

Syntax:

 md5(string,raw)

Parameter

Description

string

Mandatory. It is the input string that needs to be calculated

raw

Optional. Specifies binary or hex output format:

  • If it is set to TRUE – Raw 16 character binary format

  • If it is set to FALSE – Default. 32 character hex number

Return Type:

md5() returns hash as a 32-character hexadecimal number.

How to Decrypt MD5 Passwords in PHP?

The MD5 cryptographic algorithm is not reversible i.e. We cannot decrypt a hash value created by the MD5 to get the input back to its original value. So there is no way to decrypt an MD5 password. But, we can use something like brute force hacking, which is extremely resource-intensive, not practical, and unethical. Thus, if someone is entering the correct password, we need to find the hash value of whatever the user entered, and see if it matches what we have in our database thus it is time and resource-intensive job to perform.

It is possible to guess what the original password is by looking in dictionaries of MD5 hashes of many known words, these dictionaries can be useful to tell a user that the password that he has chosen may be easily discovered thus we can ask the user to build a more strong password.

Examples to Decrypt MD5 Password

Example 1:

<?php $string = " PHP with Edureka"; echo "Your string is:".$string; echo "<br>"; echo "Hex formed by md5 function is ".md5($string); ?> 

Output:

How to decrypt md5 password in PHP? Step by Step Guide | Edureka (1)

In the above example, we print the hash value of “ PHP with Edureka” generated by the md5() function.

Example 2:

<?php $string = ' PHP with Edureka'; if (md5($string) =='9a1359e7be2d2670780b85471675dc72'){ echo "PHP with Edureka is Fun"; } else {echo"look for the error";}?> 

Output:

How to decrypt md5 password in PHP? Step by Step Guide | Edureka (2)

In the above example, we check if the hash value of variable $string is equal to 9a1359e7be2d2670780b85471675dc72 the program prints out “PHP with Edureka is Fun” else it prints “look for the error”

Example 3

<?php $string = " PHP with Edureka"; echo "Your string is: ".$string."<br>"; echo "Setting raw input to TRUE getting 16 character binary: ".md5($string, TRUE)."<br>"; echo "default raw input set to FALSE giving 32 charater hex number: ".md5($string)."<br>"; ?> 

Output:

How to decrypt md5 password in PHP? Step by Step Guide | Edureka (3)

In the above example, we look at the application of the raw parameter in the md5() function. If we set it to TRUE it gives a 16 character binary output else it simply gives 32 characters hex number.

Example 4:

<?php $password= "pass123"; if (isset($_POST['password']) && !empty($_POST['password'])){$new_password=$_POST['password'];if(md5($new_password)==md5($password)){echo "<br> Correct password ";}else{echo "<br> Incorrect password ";}}?> <form action="md5.php" method="post"><input type="text" name="password"><br><input type="submit" ></form>

Output:

The above code gives an output of an HTML form with a text block and a submit button if we enter the correct password it prints “Correct password” else it prints “Incorrect password”.

How to decrypt md5 password in PHP? Step by Step Guide | Edureka (4)

When we type in the wrong password for example here it checks for the hash of “pass” input with the hash of “pass123” the correct password if it does not match it gives out as follows

How to decrypt md5 password in PHP? Step by Step Guide | Edureka (5)

It prints out “Incorrect password”

How to decrypt md5 password in PHP? Step by Step Guide | Edureka (6)

If we type in the right password (i.e “pass123”) the hash of the input matches with the hash of the correct password and it gives the following output

How to decrypt md5 password in PHP? Step by Step Guide | Edureka (7)

It prints out “Correct password”

How to decrypt md5 password in PHP? Step by Step Guide | Edureka (8)

Now with this, we have come to the end of the PHP Tutorial. I hope you guys enjoyed this article and understood the concepts of decryption. So, with the end of this PHP Tutorial, you are no longer a newbie to the scripting language.

If you found this PHP Tutorial blog relevant, check out thePHP Certification Trainingby Edureka,a trusted online learning companywith a network of more than250,000satisfied learnersspread acrossthe globe.

Got a question for us? Please mention it in the comments section of ”How to decrypt md5 password in PHP?” and I will get back to you.

How to decrypt md5 password in PHP? Step by Step Guide | Edureka (2024)

FAQs

How to decrypt MD5 encrypted password in PHP? ›

How to Decrypt MD5 Passwords in PHP? The MD5 cryptographic algorithm is not reversible i.e. We cannot decrypt a hash value created by the MD5 to get the input back to its original value. So there is no way to decrypt an MD5 password.

How to read encrypted password in PHP? ›

Decryption of the password: To decrypt a password hash and retrieve the original string, we use the password_verify() function. The password_verify() function verifies that the given hash matches the given password, generated by the password_hash() function.

How to get MD5 encrypted password? ›

MD5 is a hashing algorithm. There is no direct method for MD5 decryption. MD5 is decrypted by using Trial & Error methodology. It may take some time if either the text that will be decrypted or the character set that will be used for decryption is long.

How to check MD5 in PHP? ›

The md5_file() function calculates the MD5 hash of a file. The md5_file() function uses the RSA Data Security, Inc. MD5 Message-Digest Algorithm.

How to decrypt encrypted data in PHP? ›

To perform PHP encrypt and decrypt data, we can use the openssl_encrypt() and openssl_decrypt() functions, respectively. These functions rely on OpenSSL, a robust and widely-used cryptographic library.

What is the function to decrypt password in PHP? ›

The decryption of the password: To decrypt a password hash and retrieve the original string, we use the password_verify() function.

How to decode the encrypted password? ›

Use the Right Tools: Once you've figured out the type of encryption employed to protect a password, then you'll want to find the right tools to help you decrypt it. Here are some of the tools you can use: Password cracking software (John the Ripper, oclHashcat, etc) Decryption services (Decryptum, CyberChef, etc)

Is it possible to decrypt a encrypted password? ›

Even the most sophisticated computers on Earth are incapable of decrypting an encrypted password with 100% accuracy. A hacker may be able to guess your password, but they won't be able to see it. The most secure methods rely on an algorithm known as a one-way function that is infeasible to invert.

How to retrieve hashed password in PHP? ›

Here is my code <? php include("database/config. php"); if($_SERVER['REQUEST_METHOD'] == "POST"){ $old_password = $_POST['old_password']; $new_password = $_POST['new_password']; $con_password = $_POST['con_password']; $stmt = $con->prepare('SELECT * FROM users WHERE user_id= ?

What is the MD5 function in PHP? ›

The md5() function uses the RSA Data Security, Inc. MD5 Message-Digest Algorithm. From RFC 1321 - The MD5 Message-Digest Algorithm: "The MD5 message-digest algorithm takes as input a message of arbitrary length and produces as output a 128-bit "fingerprint" or "message digest" of the input.

How to login with MD5 password in php? ›

Creating the Main Function
  1. <? ...
  2. session_start();
  3. require_once 'conn.php';
  4. if(ISSET($_POST['login'])){
  5. $username = $_POST['username'];
  6. $password = md5($_POST['password']);
  7. $query = mysqli_query($conn, "SELECT * FROM `user` WHERE `username` = '$username' && `password` = '$password'") or die(mysqli_error());
Feb 8, 2021

What is the MD5 password format? ›

The MD5 password is made by encoding any number of strings into a 128-bit fingerprint. In the realm of information technology, some form of security feature is always required to ensure data security and consistency.

Can you decrypt MD5? ›

Everyone should know that the result of the MD5 hash function is an irreversible hash value of 128 bits (16 byte/characters as string): knowing the hash, it will be impossible to get back the value that generated it.

How do I use MD5 command? ›

Open a terminal window. Type the following command: md5sum [type file name with extension here] [path of the file] -- NOTE: You can also drag the file to the terminal window instead of typing the full path. Hit the Enter key. You'll see the MD5 sum of the file.

How to encrypt in PHP? ›

In the PHP programming language, Encryption, as well as Decryption of string data, is accomplished using the OpenSSL function. Here, we can encrypt and decrypt value through openssl_encrypt(data) and openssl_decrypt(data) respectively. We can use a single method or both methods of PHP language.

How to login with MD5 password in PHP? ›

Creating the Main Function
  1. <? ...
  2. session_start();
  3. require_once 'conn.php';
  4. if(ISSET($_POST['login'])){
  5. $username = $_POST['username'];
  6. $password = md5($_POST['password']);
  7. $query = mysqli_query($conn, "SELECT * FROM `user` WHERE `username` = '$username' && `password` = '$password'") or die(mysqli_error());
Feb 8, 2021

What is MD5 password in PHP? ›

The md5() function uses the RSA Data Security, Inc. MD5 Message-Digest Algorithm. From RFC 1321 - The MD5 Message-Digest Algorithm: "The MD5 message-digest algorithm takes as input a message of arbitrary length and produces as output a 128-bit "fingerprint" or "message digest" of the input.

Top Articles
Latest Posts
Article information

Author: Greg O'Connell

Last Updated:

Views: 5377

Rating: 4.1 / 5 (42 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Greg O'Connell

Birthday: 1992-01-10

Address: Suite 517 2436 Jefferey Pass, Shanitaside, UT 27519

Phone: +2614651609714

Job: Education Developer

Hobby: Cooking, Gambling, Pottery, Shooting, Baseball, Singing, Snowboarding

Introduction: My name is Greg O'Connell, I am a delightful, colorful, talented, kind, lively, modern, tender person who loves writing and wants to share my knowledge and understanding with you.