Vigenère Cipher
Crack Vigenère ciphers using Kasiski examination, index of coincidence, and Friedman test.
Vigenère Cipher
The Vigenère cipher is a method of encrypting alphabetic text using a repeating keyword. It is a polyalphabetic substitution cipher, which means it uses more than one substitution alphabet while encrypting the message. In CTFs, it often appears as a classic crypto challenge where you must find the repeating key and decode the text.

Encryption
This section shows how the same plaintext letter can be encrypted differently depending on the matching key letter.
Plaintext: G E E K S F O R G E E K S
Keyword: A Y U S H A Y U S H A Y U
Ciphertext: G C Y C Z F M L Y L E I MEach position uses a different Caesar shift based on the repeating keyword letter.
For generating the key, repeat the keyword in a circular way until it matches the length of the plaintext.
Example:
- Plaintext:
GEEKSFORGEEKS - Keyword:
AYUSH - Generated key:
AYUSHAYUSHAYU
Attack Overview
This section shows the basic path to solve a Vigenère cipher: understand the pattern, generate the key, encrypt or decrypt, and check the result.
- Repeat the keyword until it matches the length of the plaintext.
- Add plaintext and key letters modulo 26 to generate ciphertext.
- Subtract key letters from ciphertext modulo 26 to recover plaintext.
Easy Formula View
You can also think of the cipher algebraically by converting letters from A-Z into numbers 0-25.
Encryption:
Decryption:
Here, D_i means the offset of the i-th plaintext character. A is 0, B is 1, and so on.
Encryption
Encryption is done by pairing each plaintext letter with the matching key letter and then shifting the plaintext letter forward.
For example, the first letter of GEEKSFORGEEKS is G, and the first key letter from AYUSH is A. Using row G and column A gives G. The second plaintext letter is E, and the second key letter is Y, which gives C. The rest of the text is encrypted in the same way.
Decryption
Decryption is the reverse process.
To decrypt, go to the row that matches the key letter, find the ciphertext letter in that row, and then read the column label as the plaintext letter.
For example, in row A from AYUSH, ciphertext G appears in column G, so the plaintext letter is G. Then in row Y, ciphertext C is found in column E, so the next plaintext letter is E.
Implementation Summary
Below is a simple implementation of the idea. It generates the repeated key, encrypts the plaintext, and then decrypts the ciphertext back to the original text.
generateKeyrepeats the keyword until it matches the plaintext length.cipherTextproduces the encrypted message.originalTextrecovers the plaintext.
Output
The sample program prints:
Ciphertext : GCYCZFMLYLEIM
Original/Decrypted Text : GEEKSFORGEEKSComplexity
- Time Complexity:
O(n), wherenis the length of the text. - Space Complexity:
O(n), wherenis the length of the text.
Checklist
Use this quick list to avoid missing the main Vigenère steps.
- Repeat the keyword until it matches the plaintext length
- Encrypt by adding plaintext and key letters modulo 26
- Decrypt by subtracting key letters modulo 26
- Remember that
A = 0,B = 1, and so on - Check the output against the known example if you are testing your code
Last updated on