The Vigenere Cipher requires a key mapping from letters to numbers.  The mapping for the English language is (A–>0, B–>1, C–>2, …, Z–>25). The Vigenere Cipher also requires a key sequence.  The key sequence can be as short as 1 character and as long as M characters, where M is the length of the message to be encrypted.  For each character in the key sequence a random letter is selected.

 For example, a key sequence of length 5 could be: k1=22, k2=15, k3=18, k4=12, k5 = 23

The key sequence is then used in the following encryption formula:

Note the % character is the modulus operator

In the formula X is the current character being encrypted by the encryption function E. K is the current Key Sequence to encrypt the input character with.  Lets take our classic example “Hello, World!” as with the Shift and Substitution Ciphers the punctuation and white space is removed and all letters are capitalized to produce:

HELLOWORLD which can be expressed in decimal format as 7,4,11,11,14,22,14,17,11,3

Using the Key Sequence (22,15,18,12,23) the following cipher text is produced:

3, 19, 3, 23, 11, 18, 3, 9, 23, 0

or in English characters

DTDXLSDJXA

It is important to note here that the Key sequence was not as long as the cipher text. To solve this, the key was used in a circular fashion, for example the key used was 22,15,18,12,23,22,15,18,12,23

The Key sequence can be used in the following decryption formula to reverse the process:

Plugging our cipher text into the decryption formula we are able to get our original text back out and the process is complete.

Special Cases of the Vigenere Cipher

When a Key sequence length of 1 is selected this is the Shift Cipher

When a Key sequence length of M (where M is the length of the message being encrypted) is selected, the Vigenere Cipher acts as a One Time Pad Cipher.

Vigenere Cipher Example and Source Code

A sample python implementation of the Vigenere Cipher can be found on our github here: https://github.com/bscain/Cryptography/blob/master/vigenere_cipher.py

import vigenere_cipher
cipher = vigenere_cipher.vigenere()
cipherText = cipher.encrypt_message("Secret Message to be encrypted!")
plainText = cipher.decrypt_message(cipherText)

print("Encrypted Text is: " + cipherText)
print("Decrypted Text is: " + plainText)
print("The key used for the encryption / decryption is: " + str(cipher.get_key()))

Executing the above code results in the following output:

Encrypted Text is: EJAHVOBPKLICXUNHASBWNBMKLG
Decrypted Text is: SECRETMESSAGETOBEENCRYPTED
The key used for the encryption / decryption is: [12, 5, 24, 16, 17, 21, 15, 11, 18, 19, 8, 22, 19, 1, 25, 6, 22, 14, 14, 20, 22, 3, 23, 17, 7, 3, 4, 25, 22, 26, 7, 2]