In cryptography, the Vigenère cipher is one of the classic methods to encode and decode text messages. The Vigenère cipher has been reinvented many times. The method was originally described by Giovan Battista Bellaso in his 1553 book La cifra del sig. However, the scheme was later misattributed to Blaise de Vigenère in the 19th century, and is now widely known as the Vigenère cipher.
To encode a given text message, a secret keyword is chosen, for example ZODIAK. The keyword can only contain uppercase letters (no spaces), and is repeated until a string is obtained that has the same length as the original text that needs to be encoded (to achieve this, the repetition is stripped at the end). The repeated keyword is then written below the original text.
plaintext : THIS IS EXTREMELY SECRET! +++++++++++++++++++++++++ keyword : ZODIAKZODIAKZODIAKZODIAKZ ========================= ciphertext : SVLA SR HFTBDAHTY RSFZED!
Then, each letter in the original text is added to the corresponding letter of the keyword. In this addition, the letters A to Z are considered to have integer values 0 to 25. The addition is done modulo 26 (corresponding to the number of letters in the alphabet). The Vigenère cipher can thus be written as \[C_i = (O_i + K_i)\ \mathrm{mod}\ 26\,,\] where $$C_i$$ represents the $$i$$-th letter of the ciphertext, $$O_i$$ represents the $$i$$-the letter of the original text and $$K_i$$ represents the $$i$$-th letter of the (repeated) keyword. At the first position in the above example we thus get \[\mathrm{T} + \mathrm{Z} = (19 + 25)\ \mathrm{mod}\ 26 = 18 = \mathrm{S}\,.\]
A similar procedure is used to decode an encoded message, where a letter of the keyword is subtracted from the corresponding letter in the encoded message. The decoding procedure of the Vigenère cipher can thus be written as \[O_i = (C_i - K_i)\ \mathrm{mod}\ 26\,.\]
Write a function encode that encrypts a given text message $$t$$ (str) according to the Vigenère cipher with given keyword $$s$$ (str) that only contains uppercase letters. The text message $$t$$ and the keyword $$s$$ must be passed as arguments to the function. The function must return the encrypted text message (str). In doing so, only the uppercase letters in the given text message must be encoded. Other characters (lowercase letters, spaces, digits, punctuation marks, …) must remain unchanged in the encrypted message.
Write a function decode as the dual function of the function encode. This function must therefore decrypt a given text message $$t$$ (str) according to the Vigenère cipher with given keyword $$s$$ (str) that only contains uppercase letters. The encrypted text message $$t$$ and the keyword $$s$$ must be passed as arguments to the function. The function must return the decrypted text message (str). In doing so, only the uppercase letters in the given text message must be decoded. Other characters (lowercase letters, spaces, digits, punctuation marks, …) must remain unchanged in the decrypted message.
>>> encode('NOBODY EXPECTS THE SPANISH INQUISITION!', 'CIRCUS')
'PWSQXQ MORYUVA VBW AGCHAUP KHIWQJKNAQV!'
>>> decode('PWSQXQ MORYUVA VBW AGCHAUP KHIWQJKNAQV!', 'CIRCUS')
'NOBODY EXPECTS THE SPANISH INQUISITION!'
>>> encode('OH SHUT UP! AND GO AND CHANGE YOUR ARMOUR!', 'ARTHUR')
'OY ZBLT NW! AEW AF RGK THRGNY YFNY RRDHBL!'
>>> decode('OY ZBLT NW! AEW AF RGK THRGNY YFNY RRDHBL!', 'ARTHUR')
'OH SHUT UP! AND GO AND CHANGE YOUR ARMOUR!'