note 39884 added to function.md5
| From: | silasjpalmer at optusnet dot com dot au | Date: | Sat, 14 Feb 2004 01:17:57 +0000 |
| Subject: | note 39884 added to function.md5 | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-65125@lists.php.net to get a copy of this message | ||
A user friendly example of hkmaly's XOR encryption / decryption functions which use MD5 hashing
on the key.
<?php
function bytexor($a,$b,$l)
{
$c="";
for($i=0;$i<$l;$i++) {
$c.=$a{$i}^$b{$i};
}
return($c);
}
function binmd5($val)
{
return(pack("H*",md5($val)));
}
function decrypt_md5($msg,$heslo)
{
$key=$heslo;$sifra="";
$key1=binmd5($key);
while($msg) {
$m=substr($msg,0,16);
$msg=substr($msg,16);
$sifra.=$m=bytexor($m,$key1,16);
$key1=binmd5($key.$key1.$m);
}
echo "\n";
return($sifra);
}
function crypt_md5($msg,$heslo)
{
$key=$heslo;$sifra="";
$key1=binmd5($key);
while($msg) {
$m=substr($msg,0,16);
$msg=substr($msg,16);
$sifra.=bytexor($m,$key1,16);
$key1=binmd5($key.$key1.$m);
}
echo "\n";
return($sifra);
}
// Example of usage...
$message = "This is a very long message, but it is very secret and important
and we need to keep the contents hidden from nasty people who might want to steal it.";
$key = "secret key";
$crypted = crypt_md5($message, $key);
echo "Encoded = $crypted<BR>"; // returns ¦ý¼=¯ ¶òºÏ`¬ù<ÂH
ëÇ{.1º{ïåÉÑJÞV£+ß³jeÎ
$uncrypted = decrypt_md5($crypted, $key);
echo "Unencoded = $uncrypted"; // returns This is a very long message (etc)
?>
----
Manual Page -- http://www.php.net/manual/en/function.md5.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+39884
Delete -- http://master.php.net/manage/user-notes.php?action=delete+39884&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+39884&report=yes
Search -- http://master.php.net/manage/user-notes.php