Lesson 2 of 15

Vigenère Cipher

Vigenère Cipher

The Vigenère cipher (1553) extends the Caesar cipher by using a keyword instead of a single shift. Each letter of the plaintext is shifted by the corresponding letter of the key (cycling through the key).

Encryption

Given plaintext PP and key KK (both uppercase letters), the ii-th ciphertext letter is:

Ci=(Pi+KimodK)mod26C_i = (P_i + K_{i \bmod |K|}) \bmod 26

where letter values are A=0, B=1, …, Z=25.

Decryption

Pi=(CiKimodK)mod26P_i = (C_i - K_{i \bmod |K|}) \bmod 26

Example

Index01234
PlaintextHELLO
KeyKEYKE
Shift10424104
CipherRIJVS
  • HELLO with key KEYRIJVS

Security

The Vigenère cipher defeated frequency analysis for centuries — it was called the "le chiffre indéchiffrable" (the indecipherable cipher). Charles Babbage and Friedrich Kasiski independently broke it in the 19th century using the Kasiski test, which exploits repeated key cycles.

Your Task

Implement (uppercase alphabetic characters only; non-alpha characters are skipped):

  • vigenere_encrypt(text, key) — encrypt using the Vigenère cipher
  • vigenere_decrypt(text, key) — decrypt
Python runtime loading...
Loading...
Click "Run" to execute your code.