Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-03-19 04:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found