note 62607 deleted from function.crypt by bjori
| From: | bjori@php.net | Date: | Mon, 15 May 2006 05:10:05 +0000 |
| Subject: | note 62607 deleted from function.crypt by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-111293@lists.php.net to get a copy of this message | ||
Note Submitter: geniz70 ( at ) yahoo ( dot ) dk
----
I've made a simple crypt and decrypt function which uses a shared key. When run several time
with the same input text and shared key it will generate different crypted-text each time, but it
will always be decrypted correct back to the original text.
I hope someone finds it useful...
<?php
function encrypt($s, $key)
{
for($i=0;$i<=strlen($s);$i++)
$r.=substr(str_shuffle(md5($key)),($i % strlen(md5($key))),1).$s[$i];
for($i=1;$i<=strlen($r);$i++) $s[$i-1]=chr(ord($r[$i-1])+ord(substr(md5($key),($i %
strlen(md5($key)))-1,1)));
return urlencode(base64_encode($s));
}
function decrypt($s, $key)
{
$s=base64_decode(urldecode($s));
for($i=1;$i<=strlen($s);$i++) $s[$i-1]=chr(ord($s[$i-1])-ord(substr(md5($key),($i %
strlen(md5($key)))-1,1)));
for($i=1;$i<=strlen($s)-2;$i=$i+2) $r.=$s[$i];
return $r;
}
$key='shared';
$encrypted = encrypt('input text',$key); //encrypts the data using the key
echo "$encrypted<hr>";
$decrypted = decrypt($encrypted,$key); //decrypts the data using the key
echo $decrypted;
?>