Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

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

Alan

***********************************************************
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);
***********************************************************
***********************************************************
Solved!

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

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 browsing the Monastery: (3)
As of 2024-04-24 23:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found