SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Wizard bbolte's Avatar
    Join Date
    Nov 2001
    Location
    The Central Plains
    Posts
    3,304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    XOR encoding question

    I'm trying to do some simple XOR encoding and my functions don't seem to be working correctly. I'm not getting the same thing out that I'm putting in. Can anyone see what I'm missing?

    Below are my functions:
    PHP Code:
    function hexToStr($hex){
        
    $string='';
        for (
    $i=0$i strlen($hex)-1$i+=2){
            
    $string .= chr(hexdec($hex[$i].$hex[$i+1]));
        }
        return 
    $string;
    }

    function 
    strToHex($string){
        
    $hex='';
        for (
    $i=0$i strlen($string); $i++){
            
    $hex .= dechex(ord($string[$i]));
        }
        return 
    $hex;
    }

    function 
    doit($key,$text){
        
    $outText='';
      for(
    $i=0;$i<strlen($text);){
        for(
    $j=0;$j<strlen($key);$j++,$i++){
               @
    $outText .= $text{$i} ^ $key{$j};
        }
      }
      return 
    $outText;

    Basically to encode I run doit() and the strToHex() and to decode I run hexToStr() and then doit().

    Thanks...

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    It seems your XOR function is wrong...I've always seen XOR done this way:
    PHP Code:
    function xorSomething $key$text )
    {
        
    $len strlen$key );
        for ( 
    $i 0; isset( $text$i ] ); $i++ )
            
    $text$i ] = chrord$text$i ] ) ^ ord$key$i &#37; $len ] ) );
        
    return $text;

    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    Grumpy Minimalist
    Join Date
    Jul 2006
    Location
    Ontario, Canada
    Posts
    424
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    By the way...

    Code PHP:
    function hexToStr($hex){
    	return pack('H*', $hex);
    }
     
    function strToHex($string){
    	$res = unpack('H*', $string);
    	return $res[1];
    }


  4. #4
    SitePoint Wizard bbolte's Avatar
    Join Date
    Nov 2001
    Location
    The Central Plains
    Posts
    3,304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sweet! thanks guys, I'll give those a go. truly appreciate it.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •