Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Hello Monks!

I'm taking the free Stanford cryptography course, and while I have a decent basic understanding of perl, I'm having trouble doing the bit by bit and byte by byte XOR operations required. I've done a lot of googling, and I think I find solutions to what I am trying to do, but I don't understand how any of it actually works.

My questions:

1. How do you XOR two hex strings together? For instance, I give an input of say "aabbcc" and I want to XOR with "112233". The answer should be "bb99ff", but using $a ^ $b interprets as ascii instead of as hex values and so returns "PPPPPP". I suppose I just need to convert the string to a hex value, then the ^ would work? I'm not sure how.

2. This may be inherently related to question 1, but how do you convert between the 1 byte of hex and the ASCII representation of a character, and vice versa?

Thanks so much!


Update 1

I think I am close with this ($t1 is string of hex characters, pushing ASCII to tk text box):
my @ar = $t1 =~ /(..)/g; $foo->delete("1.0","end"); foreach (@ar) { $foo->insert("1.0",chr(pack("H*",$_))); }
This *should* break the string into couplets, then for each couplet, turn it into the hex value of it's string hex self, then convert that hex value into ASCII with chr. It doesn't seem to work though :/

Update 2

I got it to xor the two strings and output the hex result:
$txt3->delete("1.0","end"); my $hexor = unpack('h*',pack('h*',$t1) ^ pack('h*',$t2)); $txt3->insert("1.0",$hexor);
I'm still having trouble taking the hex string and finding the ascii character for it. Just testing:
my $hhh = unpack('h*',pack('h*',"20") ^ pack('h*',"61")); #xor ' ' and + 'a' should result in 'A' print unpack('h*',$hhh);

Here's my code. Just takes 2 input strings of hex values, XORs them, then if that hex XOR is a real ASCII text character, returns the value, else just puts something obvious like ~.
sub parse () { my $t1 = $txt1->Contents(); my $t2 = $txt2->Contents(); chomp($t1); chomp($t2); $txt3->delete("1.0","end"); my $hexor = unpack('h*',pack('h*',$t1) ^ pack('h*',$t2)); $txt3->insert("1.0",$hexor); my @ar = $hexor =~ /(..)/g; $txt33->delete("1.0","end"); foreach (@ar) { if (chr(hex($_)) =~ /[a-zA-Z]/) { $txt33->insert("end",chr(hex($_))); } else { $txt33->insert("end","~"); } } }
Thank you everyone for the help! And Marshall, I think you helped me last time I had a random question too!

In reply to Hex String XOR by alanonymous

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (6)
    As of 2018-06-18 16:12 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (110 votes). Check out past polls.