Skip to content

Instantly share code, notes, and snippets.

@stevekrenzel
Created February 12, 2012 05:39
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 stevekrenzel/1806574 to your computer and use it in GitHub Desktop.
Save stevekrenzel/1806574 to your computer and use it in GitHub Desktop.
Substitution Cipher
#!/usr/bin/env python
from string import printable, maketrans
from random import seed, shuffle
def cipher_alphabet(key):
alphabet = [c for c in printable]
seed(hash(key))
shuffle(alphabet)
return ''.join(alphabet)
def encrypt(key, message):
return message.translate(maketrans(printable, cipher_alphabet(key)))
def decrypt(key, message):
return message.translate(maketrans(cipher_alphabet(key), printable))
# Sample usage:
key = 'password'
message = 'This is a message'
print encrypt(key, message)
print decrypt(key, encrypt(key, message))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment