External Exam Download Resources Web Applications Games Recycle Bin

Vigenère Cipher (interwoven Caeser)

vigenere_(interwovenCaeser).py

#vigenere square cipher

key = "XYZ"
plaintext = "MATE"

#work out how much wrapping is needed:
whole,partial = divmod(len(plaintext),len(key))
adjusted_key = (key * whole) + key[:partial]
#key is now "XYZX" to cover all the letters of "MATE"

ciphertext=[]
for index, letter in enumerate(plaintext):
    shift_amount = (ord(letter) + ord(adjusted_key[index])) % 26
    encrypted_letter = chr( shift_amount + ord("A") )
    ciphertext.append(encrypted_letter)

print("Ciphertext:", "".join(ciphertext))