One line Vigeneré single decrypt/encrypt function with generator

TrustyTony 0 Tallied Votes 496 Views Share

I did some transformation of overcomplicated Rosetta code version

# Vigenère cipher one liner based on
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))) 

if __name__ =='__main__':
    text = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"

    encr = vigenere(text, key)
    decr = vigenere(encr, key, encode=False)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.