Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
$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 ...


In reply to Re: strings to number and XOR by almut
in thread strings to number and XOR by alager

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (16)
    As of 2014-09-30 14:49 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (374 votes), past polls