note 38573 deleted from function.str-rot13 by aidan
| From: | aidan@php.net | Date: | Thu, 08 Jul 2004 01:30:36 +0000 |
| Subject: | note 38573 deleted from function.str-rot13 by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-72849@lists.php.net to get a copy of this message | ||
Note Submitter: pathfinderNO (AT) SPAMeia dot MEpl
----
I've wrote my own function that works very like Rot13 only it uses random number to rotate
characters
<?
/*
* Info about spliting function for PHP < 5.0.0 *
* split a string up into equal sized chunks *
* Paul Gregg <pgregg at NOSPAM pgregg dot com>
* 23 September 2003
* http://www.pgregg.com/projects/php/code/str_split.phps
* str_split is available from PHP version 5 by default
*
*/
Function str_split($string, $chunksize=1) {
preg_match_all('/('.str_repeat('.', $chunksize).')/Uims', $string,
$matches);
return $matches[1];
}
#
#
# Author: Michal Mazurek <pathfinder at DONT SEND SPAM eia dot pl>
#
# This code is for public purpose, you can edit, use it only if you will
# leave this header
#
# http://pathfinder.eia.pl
#
function mycrypt($str){
# creating random key to rotate
$key = rand(1, strlen($str));
$charr = str_split($str);
foreach($charr as $ch){
#making encrypted string
$out .= chr(ord($ch)+$key);
}
return Array($out, $key);
}
function unmycrypt($str, $key){
$charr = str_split($str);
foreach($charr as $ch){
#making decrypted string
$out .= chr(ord($ch)-$key);
}
return $out;
}
list($crypted, $key) = mycrypt("My girl dumped me when I said to her PHP");
echo $crypted." (".strlen($crypted).")\n Key:$key \n";
$uncrypted = unmycrypt($crypted, $key);
echo "$uncrypted (".strlen($uncrypted).")";
?>
This will produce:
4O{"iktn"fworgf"og"yjgp"K"uckf"vq"jgt"RJR (40)
Key:2
My girl dumped me when I said to her PHP (40)
It looks that crypted msg is longer than uncrypted, that is because browser can't display
special chars and browser is replacing them by numbers, when I checked strings lengths it was all
ok.
So when you want to copy crypted msg from browser to somewhere else it'll not work. I think it
will provide more security. On final this function is not safe enough so I don't provide any
guarantee that someone won't brake it.