Skip to content

Instantly share code, notes, and snippets.

@qgrosperrin
Forked from revolunet/xor.py
Created December 18, 2017 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qgrosperrin/7b900ddc2b18299720f171c76b90c460 to your computer and use it in GitHub Desktop.
Save qgrosperrin/7b900ddc2b18299720f171c76b90c460 to your computer and use it in GitHub Desktop.
Simple python XOR encrypt/decrypt
#
# NB : this is not secure
# from http://code.activestate.com/recipes/266586-simple-xor-keyword-encryption/
# added base64 encoding for simple querystring :)
#
def xor_crypt_string(data, key='awesomepassword', encode=False, decode=False):
from itertools import izip, cycle
import base64
if decode:
data = base64.decodestring(data)
xored = ''.join(chr(ord(x) ^ ord(y)) for (x,y) in izip(data, cycle(key)))
if encode:
return base64.encodestring(xored).strip()
return xored
secret_data = "239054"
print xor_crypt_string(secret_data, encode=True)
print xor_crypt_string(xor_crypt_string(secret_data, encode=True), decode=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment