H I O X INDIA
Online PHP Tutorial
 HOME  ||  Scripts  ||  Purchase  ||  Tutorials  ||  Images  ||  Tools  ||  Templates 
  :-)  Send Page   :-)   Feedback   :-)   Register   :-)   Links   :-)   Support   :-)
Español Français 中文 Deutsch Portuguese Japanese தமிழ்
 Forums   Hosting   Internet Stats   Easy Calculation   FUN Games 

PHP Topics
Introduction
Syntax
Data Types
Variables
Operators
Control Structures
Functions
Pre-defined Function
Calendar Functions
Date and Time
Array Functions
Array Functions List1
Array Functions List2
Math Functions
PHP Mysql Functions
File Handling
Error Handling
DB Size
PHP Mail
String Tokens
String Functions
String Functions List1
String Functions List2
Session Functions
Cookies Functions
Form Variables
Running PHP from JS
Array To JS
Array to PHP
Encryption
Common Header
Ask Your Doubts
More about PHP
Feedback





Simple Encryption in PHP


Topic

How to encrypt a text in php?
I want my password to be protected from human reading?



Explanation

Encrypting using php?

A text or a password encryption can be done very easily using the functions md5() or sha1() or crypt() in php. It's a basic need to protect the password. The first step towards it is to encrypt it.

Text Encryption Using MD5 ( RSA's MD5 Message-Digest Algorithm ):

Example:
Original Text - This is test
Encrypted Text - fe1ca9859cefff19959d57aadc17187e

code:
<?php
$var = "This is test";
$enc = md5($var);
echo "Encrypted Text - $enc";
?>


On handling password, update the encrypted password in to database.
On login request, encrypt the password entered by user and compare it with the one in db.



Text Encryption Using SHA1 ( US Secure Hash Algorithm 1 ):

Example:
Original Text - This is test
Encrypted Text - 21650e52cb7a82638b93971684f6dcfceb14c9ab

code:
<?php
$var = "This is test";
$enc = sha1($var);
echo "Encrypted Text - $enc";
?>


On handling password, update the encrypted password in to database.
On login request, encrypt the password entered by user and compare it with the one in db.



Text Encryption Using Unix DES-based encryption algorithm :

Example:
Original Text - This is test
Encrypted Text - $1$WGAk30jW$0d90jdFjrXk9DelcVpvF1.

code:
<?php
$var = "This is test";
$enc = crypt($var); //salt be automatically generated
echo "Encrypted Text - $enc";
?>


On handling password, update the encrypted password in to database.
On login request, use encrypt() as below to validate the password.

if (crypt($input, $password) == $password) {
echo "Password verified!"; }

where $input in user posted password and $password is the encrypted one in database.




privacypolicy     licence     sitemap
© 2004-2010 HIOX INDIA