Skip to content

Instantly share code, notes, and snippets.

@lunaluxie
Created February 13, 2017 12:19
Show Gist options
  • Save lunaluxie/7ed206a6e5e36cd9dee886542c57fb48 to your computer and use it in GitHub Desktop.
Save lunaluxie/7ed206a6e5e36cd9dee886542c57fb48 to your computer and use it in GitHub Desktop.
Simple encode/decode functions (cryptographically not safe)
import base64
def encode(key, string):
encoded_chars = []
for i in xrange(len(string)):
key_c = key[i % len(key)]
encoded_c = chr(abs(ord(string[i]) + ord(key_c) % 256))
encoded_chars.append(encoded_c)
encoded_string = "".join(encoded_chars)
return base64.urlsafe_b64encode(encoded_string)
def decode(key, string):
decoded_chars = []
string = base64.urlsafe_b64decode(string)
for i in xrange(len(string)):
key_c = key[i % len(key)]
encoded_c = chr(abs(ord(string[i]) - ord(key_c) % 256))
decoded_chars.append(encoded_c)
decoded_string = "".join(decoded_chars)
return decoded_string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment