Created
November 22, 2012 14:09
-
-
Save xeago/4131363 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 c.encode('hex') | |
return c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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