Skip to content

Instantly share code, notes, and snippets.

@pzp1997
Created May 8, 2015 23:23
Show Gist options
  • Save pzp1997/b3ea49b475acb4e3960c to your computer and use it in GitHub Desktop.
Save pzp1997/b3ea49b475acb4e3960c to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2.7
"""Vigenere Cipher"""
from string import ascii_uppercase as alphabet
__author__ = 'Palmer (pzp1997)'
__version__ = '1.0.0'
__email__ = '[email protected]'
val = {}
for x in xrange(len(alphabet)):
val[alphabet[x]] = x
val[x] = alphabet[x]
encrypt = lambda a, b: ''.join(val[(val[a[i]]+val[b[i%len(b)]])%26] for i in xrange(len(a)))
decrypt = lambda a, b: ''.join(val[(val[a[i]]-val[b[i%len(b)]])%26] for i in xrange(len(a)))
def main():
i = ''
while i.lower() not in ['encrypt', 'decrypt']:
i = raw_input("ENCRYPT or DECRYPT: ")
message = raw_input("Plaintext: ") if i == 'encrypt' else raw_input("Ciphertext: ")
message = filter(str.isupper, message.upper())
key = filter(str.isupper, raw_input("Key: ").upper())
print encrypt(message, key) if i == 'encrypt' else decrypt(message, key)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment