Encrypt and Decrypt Data using PHP


In this tutorial i am explain about how to encrypt the data and how to decrypt the data using php. This is one of the important concept in PHP because it is used to secure your data in perfect manner.

Because if your data in encrypted no one can find your data this is unreadable format. So this concept is used for protect your data using this encrypt and decrypt method.

In this below code i am displaying both encryption and decription codes.

encrypt and decrypt data


Encrypt and Decrypt Data using PHP:

index.php

<?php
class encdec{
// encrypting data
function encrypt($data,$key){

$encrypted_text = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $data, MCRYPT_MODE_CBC, md5(md5($key))));
return $encrypted_text;

}
// decrypting data
function decrypt($data,$key){

$decrypted_text = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($data), MCRYPT_MODE_CBC, md5(md5($key))), "\0");
return $decrypted_text;

}
}
//usage
$data=new encdec();
echo "Encrypt Code :";echo" &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;";
echo $a=$data->encrypt('san web corner','key');
echo "</br> <br>";
echo "Decrypt Code :"; echo" &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;";
echo $v=$data->decrypt($a,'key');
?>


<a href="http://sanwebtutorials.blogspot.in/"> san web corner</a>



Download Code:



Post a Comment

0 Comments