Five Ways to Crack a Vigenère Cipher
brought to you by The Mad Doctor ("madness")
This is just a review of five nice ways to break a Vigenère cipher. It assumes that you are using a
computer and can write simple code. The examples in this paper are in Python 3 (for Python 3, / and
// behave differently, so be careful).
The Vigenère cipher
The Vigenère cipher is a periodic polyalphabetic substitution cipher. The key is a string of characters.
To explain how the cipher works, let's first replace the characters of the key and the characters of the
plaintext by integers, where A=0, B=1, ..., Z=25. The length of the key let's call the period or L. So the
key is just a set of numbers k
0
, k
1
, ..., k
L-1
. Next take the plaintext and express it also as a list of
numbers: p
0
, p
1
, p
2
, ... The text is encrypted by adding a number from the key modulo 26 to a number
from the plaintext, where we run through the key over and over again as needed as we run through the
plaintext. As an equation, the i
th
character is encrypted like this:
c
i
= (p
i
+ k
i mod L
) mod 26
After that, the ciphertext is expressed as letters instead of numbers.
Here is a Python routine that encrypts a text:
ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
def encrypt(plaintext,key):
ciphertext = ''
for i in range(len(plaintext)):
p = ALPHABET.index(plaintext[i])
k = ALPHABET.index(key[i%len(key)])
c = (p + k) % 26
ciphertext += ALPHABET[c]
return ciphertext
Decryption is simply the inverse. In other words, instead of adding, we subtract. Here is some code:
def decrypt(ciphertext,key):
plaintext = ''
for i in range(len(ciphertext)):
p = ALPHABET.index(ciphertext[i])
k = ALPHABET.index(key[i%len(key)])
c = (p - k) % 26
plaintext += ALPHABET[c]
return plaintext