Created
February 12, 2012 05:39
-
-
Save stevekrenzel/1806574 to your computer and use it in GitHub Desktop.
Substitution Cipher
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
#!/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