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\,.\]

Assignment

Example

>>> 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!'