# VigenÃ¨re cipher one liner based on http://rosettacode.org/wiki/Vigen%C3%A8re_cipher from itertools import cycle def vigenere(message, key, encode=True): return "".join(chr((((ord(k) if encode else -ord(k)) + ord(c)) % 26) + ord('A')) for c,k in zip((m.upper() for m in message if m.isalpha()), cycle(key))) #Demonstrating: if __name__ =='__main__': text = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!" key = "VIGENERECIPHER" encr = vigenere(text, key) decr = vigenere(encr, key, encode=False) print(text) print(encr) print(decr)
Are you able to help answer this sponsored question?
Questions asked by members who have earned a lot of community kudos are featured in order to give back and encourage quality replies.