Skip to content

Instantly share code, notes, and snippets.

Created June 27, 2013 23:26
Show Gist options
  • Save anonymous/5881280 to your computer and use it in GitHub Desktop.
Save anonymous/5881280 to your computer and use it in GitHub Desktop.
import string
def vigener_cypher(key, message):
alphabet = string.lowercase
key *= (len(message) // len(key)) + 1
translate = lambda x: [alphabet.index(c) for c in x]
message, key = translate(message), translate(key)
cypher = [(pair[0] + 1 + pair[1]) % 26 for pair in zip(key, message)]
cypher = map(lambda c: alphabet[c], cypher)
return ''.join(cypher)
key = "crypto"
message = "whatanicedaytoday"
expected_cypher = "zzzjucludtunwgcqs"
cypher = vigener_cypher(key, message)
print "c(",key,",",message,") := ", cypher
assert cypher == expected_cypher
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment