note 38573 added to function.str-rot13
| From: | pathfinderNO at rn2 dot php dot net | Date: | Sun, 28 Dec 2003 00:18:19 +0000 |
| Subject: | note 38573 added to function.str-rot13 | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-62589@lists.php.net to get a copy of this message | ||
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.
----
Manual Page -- http://www.php.net/manual/en/function.str-rot13.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+38573
Delete -- http://master.php.net/manage/user-notes.php?action=delete+38573&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+38573&report=yes
Search -- http://master.php.net/manage/user-notes.php