PHP Tutorial





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
PHP Topics
Introduction Introduction
Syntax Syntax
Data Types Data Types
Operators Operators
Control Structures Control Structures
Functions Functions
Pre-defined Function Pre-defined Function
Calendar Functions Calendar Functions
Date and Time Date and Time
Array Functions Array Functions
Array List Array Functions List1
Array Function List Array Functions List2
Math Functions Math Functions
PHP MYSQL Functions PHP Mysql Functions
File Handling File Handling
Error Handling Error Handling
DB Size DB Size
PHP Mail PHP Mail
String Tokens String Tokens
String Functions String Functions
String Functions List String Functions List1
String Functions List2 String Functions List2
Session Functions Session Functions
Cookies Functions Cookies Functions
Form Variables Form Variables
Examining variables Examining variables
Filter Functions Filter Functions
PHP Filters PHP Filters
Running PHP from JS Running PHP from JS
Array To JS Array To JS
JS Array Array to PHP
Encryption Encryption
Common Header Common Header
Forums Ask Your Doubts
Scraps More about PHP
Feedback Feedback
 

Simple Encryption in PHP


Tutorials 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$X2ZDui8V$lgt2tSZtIXiZsvpiRmrHB0

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.





A Note

Learn PHP programming language tutorial with simple and neat example. Hope you enjoy this free tutorial. Do give us your valuable feedback and suggestions on this online tutorial. This is a Copyright Content.

Other Links

web hosting