Vigenère Cipher Mathematical Equation and Python Tutorial

Topic of Contents:

  1. Vigenère Cipher Basics and Tabula Recta
  2. Understanding Modular Arithmetic
  3. Vigenère Cipher Encryption Equation
  4. Vigenère Cipher Implementation in Python
  5. Python Program Testing and Proof of Concept
  6. Further Exploration: Try the Program Yourself
  7. Conclusion
  8. Frequently Asked Questions (FAQs)

Vigenère Cipher Basics and Tabula Recta

The Vigenère cipher is an encryption technique that involves altering each letter of the plaintext based on a keyword using a simple polyalphabetic substitution method. It shifts the position of each letter in the alphabet according to the provided key.

This article is a continuation of the previous discussion regarding the fundamentals of Vigenère cipher. Before we learn about the Vigenère cipher mathematical equation and its Python expression, you need to understand the fundamentals on how the Vigenère cipher encryption and decryption works. To examine your understanding, you should know how it works and the description of the components of the Tabula Recta in the following:

Alt-image & caption: Tabula Recta

If so, keep going on the journey in this article. If not, you are advised to understand the fundamentals first in this article: Vigenère Cipher Detailed Analysis.

Understanding Modular Arithmetic

The modulo operation, often denoted as “mod”, is a mathematical operation that finds the remainder when one number is divided by another. In other words, given two numbers, “a” (the dividend) and “b” (the divisor), the modulo operation returns the remainder after “a” is divided by “b”. For example, 10 mod 4 = 2 because when 10 is divided by 4, the remainder is 2.

In Python, the arithmetic expression of the modulo operation is the percentage symbol or “%”.

Vigenère Cipher Encryption Mathematical Equation

The mathematical equation for the Vigenère cipher encryption is:

Ex = (Px + Kx) mod 26
Ex Denotes the xth letter of the encrypted text
Px The x index of a plaintext
Kx The “key” index
mod 26 The modulo operations of the total amount of the alphabet which is 26

Index numbering starts with 0 to represent the alphabet in numeric (0-25).

Alt-image & caption: Index Numbering Value

For example, let’s encode the “BIMANDO” plaintext with the “LINUX” key using the previous Vigenère cipher equation.

Alt-image & caption: Vigenère Cipher Encryption

First, we need to identify each plain letter and the key index numbers. For the plain letter of “B”, the index value is 1 and the key of “L” has an index of 11.

Alt-image & caption: Vigenère Cipher Encryption

Using the Vigenère cipher equation, we then calculate each pair of plain and key index numbers to produce the cipher index (Ex). The plain letter of “B” and the key of “L” generate an equation as follows:

Calculate until the last plain letter. The result is shown in the following:

Alt-image & caption: Vigenère Cipher Encryption

So, the “BIMANDO” plaintext with the “LINUX” key is encrypted using the Vigenère cipher that results in “MQZUKOW”.

Vigenère Cipher Encryption Python Expression

Now, you have an idea on how the Vigenère cipher works and its equation. The challenge is raised. In our previous example, we just encrypted a single word. How do we do it if we want to encode a paragraph or any other longer text? The answer is using a Python program. Here is the step-by-step explanation:

Import the Required Library

We need the “argparse” module to handle the command-line arguments and the “os” module for the file path manipulation later.

Define the Vigenère Cipher Encryption Function

We want our script to provide a command-line tool for Vigenère cipher encryption of a text file. It takes in the input file path, an optional output file path (with a default value generated based on the input file name), and an optional encryption key (with a default value of “secret”). The script ensures that the encryption process maintains the input and output file character index positions and skips assigning a key for blank characters.

The Conditional Main Function

The conditional main function is where the script execution starts. It sets up an argument parser using “argparse”. Then, it calls the “encrypt_vigenere” function which is responsible for performing the Vigenère cipher encryption on the provided plaintext using the given key while preserving the non-alphabetic characters.

Inside this conditional main function, we design the robust error handling to catch the exceptions such as FileNotFoundError and other general exceptions. It also provides informative error messages to guide the user in case of issues. Additionally, it provides clear and informative success messages upon successful encryption and writing of the encrypted text to the specified output file.

Finally, our program is complete and saved it to “”. Now, for the first run, type the following command:

Alt-image & caption: Vigenère Cipher Encryption Python program

Proof of Concept

Let’s test it. We have a plaintext paragraph in a text file named “simple_text.txt” as follows:

Alt-image & caption: Plaintext Message

We want to encrypt using the Vigenère cipher with the “SUPERSECRET” key. We then launch our program with the following command:

Alt-image & caption: Proof of Concept Vigenère Cipher Encryption Python Program

Try It Yourself

Download this program source code on our GitHub page at and try it yourself.


In conclusion, by understanding the fundamentals of the Vigenère cipher, including the Tabula Recta and the modulo operation, you can grasp the intricacies of its encryption process. The encryption equation, Ex = (Px + Kx) mod 26, demonstrates how the plaintext and key indices are combined to produce the ciphertext. Additionally, using a mathematical equation and a Python program, you can efficiently encrypt the text files using the Vigenère cipher. The Vigenère cipher remains a valuable tool for secure data encryption, demonstrating its applicability in various practical scenarios.

Frequently Asked Questions (FAQs)

Q1: What role does the modulo operation play in the Vigenère cipher’s mathematical equation?

A1: The modulo operation ensures that the resulting index stays within the range of the alphabet (26 letters), enabling the cyclic nature of the encryption process.

Q2: How is the Vigenère cipher’s mathematical equation different from other encryption methods?

A2: Unlike some encryption methods that use the fixed substitution rules, the Vigenère cipher employs a dynamic polyalphabetic substitution, making it more resistant to frequency analysis attacks.

About the author


Penetration Tester with Kali Linux. Reach me on Facebook