XP is just a number | |
PerlMonks |
Hex String XORby alanonymous (Sexton) |
on Mar 12, 2012 at 08:22 UTC ( [id://959080]=perlquestion: print w/replies, xml ) | Need Help?? |
alanonymous has asked for the wisdom of the Perl Monks concerning the following question:
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): 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: I'm still having trouble taking the hex string and finding the ascii character for it. Just testing: *********************************************************** *********************************************************** 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 ~. Thank you everyone for the help! And Marshall, I think you helped me last time I had a random question too!
Back to
Seekers of Perl Wisdom
|
|