Tuesday 1 April 2014

MD5 encryption In php

MD5 hash in php
MD5 Encryption in php or generating MD5 hash from a string. Now If you don't know what is MD5 ? MD5 is a form of encryption and hash is generated from a string that's given in php we had function called md5(); if you given a string here it will convert MD5 hash.

code for MD5 encryption example(Screenshot1)

MD5 Encryption in php
Screenshot1

<?php



$string = 'password';

$string_hash = md5($string);



echo $string_hash;



?>

Output (Screenshot2)
MD5 encryption in php
Screenshot2
We creating a string like password saving inside the variable called $string. $string = 'password'; giving value of password. Now if you want to encrypt this we could simply create new variable $string_hash i will make this equal to md5 and inside a bracket i am going to supply the $string the data that i want to convert or encrypt. echo out the $string_hash. You can see screenshot2 a password what it looks like it when it be hashed.

5f4dcc3b5aa765d61d8327deb882cf99 is the hash presentation of the string password. However much be refresh it will remain same. We called this one way encryption this $string_hash can not be un-encryption back no more text. We can only do that we can take a input and match to the encrypted data.

For example you had stored a password in database with encrypted value user typed and say mohammed is the password the encrypted data would not match to the password. Therefore password would be incorrect.

Now Let so You example by html form. I am gonna create new file copy and paste 5f4dcc3b5aa765d61d8327deb882cf99 this hash over into file we gonna called this hash.txt. So, obliviously it is unsecure way of saving password because someone could access it. However Now from index.php let the user enter value so we gonna create a form that allows the user to enter value the form will submit we gonna open hash.txt we gonna look content and we gonna see if password match hash string must be equal to password means if we type mohammed password will be incorrect if we type password it will be correct input.


code for Example MD5 Encryption


<?php



if (isset($_POST['user_password']) && !empty($_POST ['user_password'])) {

$user_password = $_POST['user_password'];



$filename = 'hash.txt';

$handle = fopen($filename,'r');

$file_password = fread ($handle,filesize($filename));



if ($user_password==$file_password) {

echo 'password ok!';

}else {

echo 'Incorrect password.';

}





}else {

 echo 'Please enter a password.';

}

?>

<form action = "index.php" method="POST">

password: <input type="text" name="user_password"><br><br>

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

</form>

Output 


MD5 Encryption in php
Screenshot3
MD5 encryption in php
Screenshot4
MD5 Encryption in php
Screenshot5
MD5 In php
Screenshot6
MD5 in php
Screenshot7
Creating from with form action with index.php the method is going to be POST remember posting a password so want to keep outside so we use POST. When using login forms in your web application you must sure you must use POST method because you got lot of data in process as well as its going clean as possible. So, Now create label called password: with text called password with input type must be password but purpose of this article we gonna use text so we can see the text what we had written in input box. name of its going to be user_password then creating submit button with value be submit. You can see screenshot.

Then with if condition we will check whether password is correct and form is be submitted. With isset function we will check whether form has been submitted with !empty function also we are checking that form is submitted is not empty It should filled. Now we will open up thid file called hash.txt and we want to grape value and we had to compare with the user input value. Then we will open file with fopen function by file handling in php. We are opening file to read the data in so we use r in file handling in php. Then we will convert user input into md5 function to check whether our password is correct in md5 function is equal same therefore we will check the user input value convert it to md5 function and will check with aur encrypted data which we had already converted and saved to hash.txt.

Now why we use password hashes in first place this md5 encryption whats the point using it. Why we need md5 function used ? . The reason is md5 function is not most secure form of hashing and is also not the most secure of encryption. It can be crack easily by checking. Lets say you had a database setup and there were lot's of username and password means you had a lot's of user on your website and you using an database now what happen's some one goes unauthorized access to your database what going happen is they are going to able to see your passwords if they are un-encrypted. If someone can view users password.

For example Screenshot6 and Screenshot7

In screenshot6 database i can easily use users password and username and login into it and In screenshot7 its difficult to anyone to login easily They can not convert MD5 function encrypted data because MD5 is one way encryption. There is no way that you can return this to there value that we called one way encryption.


No comments:

Post a Comment

Thanks For Comment Will get you Soon..

About Me

Welcome to Extra Tutorials! My name is Mohammed and I am the 22 year writer, website developer, and photographer behind the blog. Thanks for visiting! Tutorials Jackpot… In addition to Developer, I love to develop websites and I love to write. Starting a php Blog was inevitable for me. What began as a simple way to share all of my Tutorials with friends and family has developed into my Part time job.

Mohammed Padela

WHAT IS PHP PROGRAMMING

WHAT IS PHP PROGRAMMING
WHAT IS PHP PROGRAMMING

Follow Us

Popular Posts

Designed ByBlogger Templates