Skip to content

Instantly share code, notes, and snippets.

@opie4624
Created January 10, 2009 05:15
Show Gist options
  • Save opie4624/45393 to your computer and use it in GitHub Desktop.
Save opie4624/45393 to your computer and use it in GitHub Desktop.
Puzzles for Hackers
Solutions to the "Puzzles for Hackers" book by Ivan Skylarov
ISBN10: 1-931769-45-1
>>> from xorencdec import xorencdec
>>> xorencdec('creature_creature_creature',']VTYJQC]aGC]_PDJ[{RJ[EEMLA')
'>$18>$18>$18>$18>$18>$18>$'
>>> xorencdec('Smith','>$18')
'mIXLV'
def xorencdec(a, b, debug=False):
if debug:
print a," Length: ",len(a)
print b," Length: ",len(b)
if len(a) < len(b):
key,string = a,b
else:
key,string = b,a
if debug:
print "Key: ",key," [",len(key),"]"
print "String: ",string," [",len(string),"]"
wkey=i=0
output=''
while i<len(string):
if wkey == len(key):
wkey = 0
if debug:
print "",key
print " "*wkey,"^ ",wkey
print "",string
print " "*i,"^ ",i
print key[wkey],"^",string[i]," = ",chr(ord(string[i])^ord(key[wkey]))
output += chr(ord(string[i])^ord(key[wkey]))
wkey += 1
i+=1
return output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment