Skip to content

Instantly share code, notes, and snippets.

@xeago
Created November 22, 2012 14:09
Show Gist options
  • Save xeago/4131363 to your computer and use it in GitHub Desktop.
Save xeago/4131363 to your computer and use it in GitHub Desktop.
import sys
def strxor(a, b): # xor two strings of different lengths
if len(a) > len(b):
return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a[:len(b)], b)])
else:
return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a, b[:len(a)])])
def encrypt(key, msg):
c = strxor(key, msg)
print
print c.encode('hex')
return c
class String
def xor(msg)
key= [self].pack("H*").unpack("C*")
msg = [msg].pack("H*").unpack("C*")
x = if key.length > msg.length
msg.zip(key)
else
key.zip(msg)
end
x = x.collect{|c1,c2| c1^c2}
end
def hex
self.unpack("H*").join
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment