2013-04-11 01:07:29 -07:00
|
|
|
from itertools import starmap, cycle
|
|
|
|
|
|
|
|
|
|
def encrypt(message, key):
|
|
|
|
|
|
|
|
|
|
# convert to uppercase.
|
|
|
|
|
# strip out non-alpha characters.
|
2016-12-05 22:15:40 +01:00
|
|
|
message = filter(str.isalpha, message.upper())
|
2013-04-11 01:07:29 -07:00
|
|
|
|
|
|
|
|
# single letter encrpytion.
|
2016-12-05 22:15:40 +01:00
|
|
|
def enc(c,k): return chr(((ord(k) + ord(c) - 2*ord('A')) % 26) + ord('A'))
|
2013-04-11 01:07:29 -07:00
|
|
|
|
|
|
|
|
return "".join(starmap(enc, zip(message, cycle(key))))
|
|
|
|
|
|
2013-04-11 11:14:19 -07:00
|
|
|
def decrypt(message, key):
|
2013-04-11 01:07:29 -07:00
|
|
|
|
|
|
|
|
# single letter decryption.
|
2016-12-05 22:15:40 +01:00
|
|
|
def dec(c,k): return chr(((ord(c) - ord(k) - 2*ord('A')) % 26) + ord('A'))
|
2013-04-11 01:07:29 -07:00
|
|
|
|
|
|
|
|
return "".join(starmap(dec, zip(message, cycle(key))))
|