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!

    If you want the ASCII numeric value of a character, use [man]ord/man -- [man]intval/man is a type-casting function.

      The reason I did that is the input for the Chr function is an integer, and I'm pulling items out of a string.. That translates to Chr(integer), which is why I thought it would work, and it is definitely putting something into the $buf[1] variable as the strlen function has shown me.. but for some reason if I output that string it just doesn't show up as anything. It says its x characters long but shows up blank.

      So confused 🙁

      Basically:

      Once it's fully encoded and done all the good stuff to it, it has a string of integers. Say.. "456543634561" as an example. It then goes through and turns each of those numbers into its ascii equivalent. That's the part of the operation that's failing, that final stage of turning it back into characters.. could it be because of the nature of the ascii characters from 0 - 9?

      Sorry if the code is sloppy!

        As I stated above, intval() is a type-casting function and has nothing to do with the ASCII value of a character. ord() on the other hand returns an integer which is the ASCII value of the supplied character.

        <?php
        header('Content-Type: text/plain');
        
        $chars = array('a', 'b', '1', '2');
        
        echo "Using ord():\n";
        foreach($chars as $char)
        {
           echo ord($char) . "\n";
        }
        
        echo "\nUsing intval():\n";
        foreach($chars as $char)
        {
           echo intval($char) . "\n";
        }
        ?>
        

        Outputs:

        Using ord():
        97
        98
        49
        50
        
        Using intval():
        
        
        1
        2
        

        PS: Of course, you could have me completely confused as to what exactly you are doing there. :p

          Say my string value is "5". Would intval not convert that into the integer 5? Which if I then put inside the Chr() function should output something?

          If I even separate every character with a "<br>" or something it comes up, but then of course it's displayed vertically down the page.. it's when I add them all together I think the problem arises?

          Would the characters 0 - 9 interfere with each other and cause problems?

            Yes, intval('5') will return the integer 5. Generally, though, this is not required since PHP is a loosely typed language. Admittedly I've not gone through your code with a fine-tooth comb, that line just looked odd to me.

            I'd recommend either using the [man]mcrypt[/man] PHP extension to do encryption/decryption, or look at the various PEAR Encryption packages, rather than reinventing the wheel (and almost certainly not being as strong an encryption method).

              It needs to be downloaded by a vb6 program and decoded in there, that's why it's like this..

              Okay thanks anyways.

                This seems to work, and the function is symmetrical in that it decodes itself:

                <?php
                
                function encode($string, $pid)
                {
                   $chars = str_split($string);
                   array_walk(
                      $chars, 
                      create_function(
                         '&$val,$key',
                         '$val = chr(ord($val) ^ '.$pid.');'
                      )
                   );
                   return implode('', $chars);
                }
                
                $test = "This is a test.";
                $pid = '12';
                
                $encrypted = encode($test, $pid);
                $decrypted = encode($encrypted, $pid);
                
                echo $encrypted . "<br>\n" . $decrypted;
                
                  Write a Reply...