All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

How To Encrypt And Decrypt Password In PHP

Last Updated : Mar 11, 2024

How To Encrypt And Decrypt Password In PHP

In this tutorial we will show you the solution of how to encrypt and decrypt password in PHP, encryption is nothing but achieving the encryption code with the help of some algorithms which are sometimes called hashing algorithms and they usually work with by taking a string then it will creating a unique format string.

The conversion of encrypted data into its original form is called Decryption.

For encrypt a password string we used hash algorithm method available in php.

Step By Step Guide On How To Encrypt And Decrypt Password In PHP :-

Here we takes password string in a variable then using password_hash() method for our password string change into encrypted type of string.

Password_hash() creates a new password hash using a strong one-way hashing algorithm with constant PASSWORD_DEFAULT it supports algorithm.

Method of password_verify() can verify that given hash matches the given password.

By this method we verified our password with hash type string and it returns Boolean value if its true our password correct otherwise it is not valid password.

$pwsd = "prawin@123";
$hash = password_hash($pwsd,PASSWORD_DEFAULT);
    echo 'original password: '.$pwsd.'<br>';
    echo 'encrypt password: '.$hash.'<br>';
    echo 'Password Verified Successfully';
    echo 'Incorrect Password';
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
  3. When declaring a variable in php we must use ‘$’ symbol before the variable name. Here we stored our password to variable ‘$pwsd’ and the password is ‘prawin@123’.
  4. Then using password_hash() method we transformed our password to hash type and the result string stored to variable ‘$hash’. Then don’t forget to add constant parameter of ‘PASSWORD_DEFAULT’ otherwise it throws error.
  5. Now we have to pass parameter of our original password and hashing password to the password_verify() method and it checks two variable values are same string then it returns Booleans value. The result of Boolean value stored to variable ‘$res’.
  6. If() condition checks whether Boolean value returned true or not. If result true means changed hash password and original form of password are same. If its false it throws message of ‘incorrect password’ to the webpage.
  7. If our passwords are same then we prints original password, encrypted password and result string message of ‘Password verified successfully’ will display on browser webpage.
  8. In php for display any string or value on webpage we need to use ‘echo’ or ‘print’. Here we used echo for print results on browser.

Conclusion :-

In conclusion we are able to know how to encrypt and decrypt password using php.

When work with php we need to create and process php files at server location and then we need to start the server before execute the program.

When we executing this program on browser it checks our user defined password and hash type converted passwords are same then result will display on webpage browser.

I hope this tutorial on how to encrypt and decrypt password in PHP helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪