Encryption & Decryption in php


in php md5() is the one way encryption technique. but the functioni give is the two way encryption and also allow the decryption.

function encrypt($string, $key) {

$result = ”;

for($i=0; $i<strlen($string); $i++) {

$char = substr($string, $i, 1);

$keychar = substr($key, ($i % strlen($key))-1, 1);

$char = chr(ord($char)+ord($keychar));

$result.=$char;

}

return base64_encode($result);

}

function decrypt($string, $key) {

$result = ”;

$string = base64_decode($string);

for($i=0; $i<strlen($string); $i++) {

$char = substr($string, $i, 1);

$keychar = substr($key, ($i % strlen($key))-1, 1);

$char = chr(ord($char)-ord($keychar));

$result.=$char;

}

return $result;

}

for example

$string=encrypt(‘1001′,’502’);

it gives the string like AX=120

to decrypt

$original_String=decrypt(‘AX=120‘,’502’). it give the original result.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s