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


in reply to strings to number and XOR

$msg = "this is a test messagez"; $key = "\x4f\xcb"; # or $key = pack "n", 0x4fcb; $xored = $msg ^ $key; print join(" ", split(//, $msg)), "\n"; print join(" ", unpack("(H2)*", $msg)), "\n"; print join(" ", unpack("(H2)*", $key)), "\n"; print join(" ", unpack("(H2)*", $xored)), "\n"; __END__ t h i s i s a t e s t m e s s a g e z 74 68 69 73 20 69 73 20 61 20 74 65 73 74 20 6d 65 73 73 61 67 65 7a 4f cb 3b a3 69 73 20 69 73 20 61 20 74 65 73 74 20 6d 65 73 73 61 67 65 7a

The easiest way to generate your key string for the entire message is probably something like

$key = "\x4f\xcb" x (length($msg)/2); or $key = pack("n", 0x4fcb) x (length($msg)/2);

That way you can directly XOR both strings without any further ado:

t h i s i s a t e s t m e s s a g e z 74 68 69 73 20 69 73 20 61 20 74 65 73 74 20 6d 65 73 73 61 67 65 7a + $msg 4f cb 4f cb 4f cb 4f cb 4f cb 4f cb 4f cb 4f cb 4f cb 4f cb 4f cb + $key 3b a3 26 b8 6f a2 3c eb 2e eb 3b ae 3c bf 6f a6 2a b8 3c aa 28 ae 7a + XOR result

___

 $xitem =  unpack("H*", $array[1]) ^ $key;

What this is doing is XORing the hex-string "7468" (i.e. 4 ASCII chars, hex 37 34 36 38) with the Perl internal representation of the number 0x4fcb ...