PHP Encryption


 

PHP Encryption

This tutorial will explain the various ways to encrypt data. When we develop a web-site application then at first we need to develop few preliminary web pages like registration, login etc. It is always been suggested to store the passwords in encrypted form. In the current tutorial we will explain the same. Various examples will help you to understand the similarity as well as the differences of methods.

This tutorial will explain the various ways to encrypt data. When we develop a web-site application then at first we need to develop few preliminary web pages like registration, login etc. It is always been suggested to store the passwords in encrypted form. In the current tutorial we will explain the same. Various examples will help you to understand the similarity as well as the differences of methods.

How to Encrypt a password in PHP:

When we develop a web-site application then at first we need to develop few preliminary web pages like registration, login etc. It is always been suggested to store the passwords in encrypted form.

There are two popular method is available in PHP namely MD5 and SHA-1.

MD5: md5 method encrypts a simple text to a cipher text. We can use this method to convert the password into an encrypted text.

Description Parameters Return Values
string md5(string $str,[,bool $Boolean=false]) str=string, we can set Boolean as true  Returns the text as 32 character hexadecimal number.

 

SHA-1: sha1 method is same as the previous and it encrypts a simple text to a cipher text. We can use any one of these methodsz to convert the password into an encrypted text.

Description Parameters Return Values
string sha1(string $str,[,bool $Boolean=false]) str=string, we can set Boolean as true  Returns the text as 32 character hexadecimal number.

 

Difference between MD5 and SHA-1:

The SHA-1 method was introduced by NIST in 1994. SHA-1 produces 160-bit (20 Byte) message digest (the condensed representation is of fixed length also called fingerprint) and it is  slower than MD5. This message digest makes the text stronger against brute force attacks.

Prof. Ronald L. Rivest developed this message digest in 1994.  MD5 is a 128-bit (16 Byte)message digest and it is faster than SHA-1.

Example:   

<?php

if(isset($_POST['name'])){

$name=$_POST['name'];

echo "Hello, ".$name,". How are you doing?";

echo "<br/>Your password is :". $_POST['pass'];

$pass1=md5($_POST['pass']);

echo "<br/>Your md5 password is :". $pass1;

$pass1=sha1($_POST['pass']);

echo "<br/>Your sha1 password is :". $pass1;

$pass1=md5($_POST['pass'],true);

echo "<br/>Your md5,true password is :". $pass1;

$pass1=sha1($_POST['pass'],true);

echo "<br/>Your sha1,true password is :". $pass1;

}

else{

?>

<form name="reg" method="post" action="<?echo $_SERVER['PHP_SELF']; ?>">

<b>User Registration Form</b><br/>

0

Name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="text" name="name"/>

<br/>

Password:<input type="password" name="pass"/>

1

<br/>

<input type="submit" value="Submit"/>

</form>

2

<?php

}

?>

3

&n 

Output:

Registration Form

4

After filling the form and click the button, following output will be shown:

Hello, ss. How are you doing?
Your password is :sdf
Your md5 password is :d9729feb74992cc3482b350163a1a010
Your sha1 password is :9a6747fc6259aa374ab4e1bb03074b6ec672cf99
Your md5,true password is :Ùr?ët?,ÃH+5c¡ 
Your sha1,true password is :?gGübYª7J´á» KnÆrÏ?

Ads