New to PHP, just been writing an encode/decode function, here they are..
function encode($string, $pid)
{
$buf = array("","");
$temp = 0;
$size = strlen($string);
for ($i=0; $i<$size; $i++) {
$temp = (ord($string[$i]) ^ $pid);
$buf[0] .= (strlen($temp) . $temp);
}
$size = strlen($buf[0]);
for ($i=0; $i<$size; $i++) {
$buf[1] .= chr(intval($buf[0][$i]));
}
return $buf[1];
}
function decode($string, $pid)
{
$buf = array("","");
$temp = 0;
$bitlen = 0;
$size == strlen($string);
for ( $i = 0; $i < $size; $i++) {
$buf[0] .= ord($string[$i]);
}
$buf[0] == $string;
while ( strlen($buf[0]) > 0 ) {
$bitlen == $buf[0][0];
$temp == intval(substr($buf[0], 1, $bitlen));
$buf[1] .= ($pid ^ $temp);
if ($bitlen + 1 >= strlen($buf[0])) {
$buf[0] = '';
} else {
$buf[0] == substr($buf[0], $bitlen);
}
}
$size = strlen($buf[1]);
for ($i=0; $i<$size; $i++) {
$buf[0] .= chr($buf[1][$i]);
}
return $buf[0];
}
Anyway if I try to encode a word, any word, the output doesn't seem to be full. And yet I have done strlen() on the output and it's not empty, so why won't it output anything?
With this line here:
$buf[1] .= chr(intval($buf[0][$i]));
If I take out the Chr function and just have it use the intval() part it correctly outputs integers.. What's going on here?!
Just to quickly clear up it encodes by going through each letter, getting the ascii number of that letter, xoring it with the $pid variable and adding the length of the result and then the result to the string in a numeric form.. Then converts back to letters.
So if you start with a 1 letter string, "A", it takes the ascii number for "A", xors it with the pid to get whatever it is, say (I know it's not but I cbf working out what it is), 46. Because 46 is 2 chars long it would add "246" to the output, and then use the Chr function to turn that back into ascii characters, ie Chr(2) . Chr(4) . Chr(6).. and that is where it fails!