Skip to content

Instantly share code, notes, and snippets.

@013
Created December 2, 2012 00:19
Show Gist options
  • Save 013/4186067 to your computer and use it in GitHub Desktop.
Save 013/4186067 to your computer and use it in GitHub Desktop.
XOR Encrypter
#!/usr/bin/python
# -*- coding: UTF-8 -*-
string = "something something something"
key = "cheese"
def crypt(string, key):
data = ''
for i in range(len(string)):
# XOR
xc = ord(string[i]) ^ ord(key[i%len(key)])
# Convert XOR to base 2
xc = bin(xc)[2:].zfill(8)
data += str( xc )
#print string[i]
return data
def decrypt(string, key):
data = ''
for i in range( (len(string)+1) / 8):
# Convert each 8 bits to base 10
x = int( string[((i+1)*8)-8:(i+1)*8], 2 )
# XOR with key
data += chr( x ^ ord(key[i%len(key)]) )
return data
print "String: %s - Key: %s \n \n%s\n" % (string, key, "="*10)
x = crypt(string, key)
print x + "\n"
y= decrypt(x, key)
print y + "\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment