http://www.perlmonks.org?node_id=799620

alager has asked for the wisdom of the Perl Monks concerning the following question:

I would like to xor every two bytes in an arbitrary string with the same value, or key. Here is one things I've tried, but the result is not correct. For the first two characters (th) xor I expect it to be 0x3ba3, but I'm getting 0x52e7, I can't figure out what is going wrong.
#!/usr/bin/perl 2 3 $msg = "this is a test messagez"; 4 $key = 0x4fcb; 5 print "$msg\n"; 6 7 @array = split(/(..)/, $msg); 8 9 10 printf "key: %x\n", $key; 11 12 $xitem = unpack("H*", $array[1]) ^ $key; 13 print "item: $array[1] = hex " . unpack("H*", $array[1]) . "\n"; 14 printf ("xored item \'%x\', want 0x3ba3\n", $xitem);
Thanks, Aaron