Vigenere Cipher Dictionary Attack Python


In this article, we will explore the concept of dictionary attack on the Vigenere cipher using Python.

In the world of cryptography, the Vigenere cipher is a classical method of encrypting messages.

It was invented by Giovan Battista Bellaso in the 16th century and later popularized by Blaise de Vigenère.

The Vigenere cipher is a polyalphabetic substitution cipher that provides a higher level of security compared to simple substitution ciphers.

However, like any encryption method, it is not impervious to attacks.

Also Read: Twin Prime Number Program in Python

What is the Vigenere Cipher?

The Vigenere cipher is a method of encrypting plaintext messages by shifting each letter by a corresponding letter from a keyword.

It is a polyalphabetic substitution cipher because it uses multiple substitution alphabets.

Also Read: Permute in Python: A Comprehensive Guide to Permutations

Each letter of the plaintext is encrypted using a different Caesar cipher, determined by a repeating keyword.

Vigenere Cipher Dictionary Attack Python: Understanding the Attack

The Vigenere cipher has been widely used for centuries, but it is not immune to attacks.

One of the most common and effective attacks on the Vigenere cipher is the dictionary attack.

Also Read: Python Program to Check If Two Strings are Anagram

In a dictionary attack, the attacker uses a precompiled list of words, known as a dictionary, to try to decrypt the ciphertext without knowing the exact keyword used for encryption.

Step 1: Creating a Dictionary

To perform a dictionary attack on the Vigenere cipher using Python, we need to start by creating a dictionary of common words.

Also Read: Python Program to Check Armstrong Number

There are various publicly available dictionaries that can be used for this purpose. One popular dictionary is the “rockyou.txt” file, which contains millions of commonly used passwords.

dictionary = []
with open('rockyou.txt', 'r', encoding='latin-1') as file:
    for word in file:

Step 2: Decrypting the Ciphertext

Once we have our dictionary, we can proceed to decrypt the ciphertext.

The idea behind the dictionary attack is to try each word from the dictionary as the potential keyword and check if the decrypted text makes sense.

Also Read: Python Program to Delete an Element From a Dictionary

We can use the Vigenere cipher decryption algorithm to decrypt the ciphertext using each word from the dictionary.

def vigenere_decrypt(ciphertext, keyword):
    plaintext = ''
    keyword_length = len(keyword)
    for i, char in enumerate(ciphertext):
        if char.isalpha():
            keyword_index = i % keyword_length
            keyword_char = keyword[keyword_index].lower()
            shift = ord(keyword_char) - ord('a')
            decrypted_char = chr((ord(char.lower()) - ord('a') - shift) % 26 + ord('a'))
            plaintext += decrypted_char
            plaintext += char
    return plaintext

Step 3: Evaluating Decrypted Text

After decrypting the ciphertext with each potential keyword, we need to evaluate the decrypted text to determine if it makes sense.

Also Read: Barplot Python: Visualizing Data with Python’s Matplotlib Library

We can compare the decrypted text with words from the dictionary and calculate a score based on the number of matches.

def evaluate_decryption(decrypted_text, dictionary):
    decrypted_words = decrypted_text.split()
    match_count = 0
    for word in decrypted_words:
        if word.lower() in dictionary:
            match_count += 1
    return match_count

Step 4: Performing the Dictionary Attack

With the decryption and evaluation functions in place, we can now perform the dictionary attack.

Also Read: Online Python Playground: Unleash Your Coding Potential

We iterate over each word in the dictionary and decrypt the ciphertext using that word as the potential keyword.

We evaluate the decrypted text and keep track of the keyword with the highest score.

def dictionary_attack(ciphertext, dictionary):
    max_score = 0
    best_keyword = ''
    for word in dictionary:
        decrypted_text = vigenere_decrypt(ciphertext, word)
        score = evaluate_decryption(decrypted_text, dictionary)
        if score > max_score:
            max_score = score
            best_keyword = word
    return best_keyword

Also Read: Logical Operators Python: A Comprehensive Guide

FAQs (Frequently Asked Questions)

Q: What is a dictionary attack?

A dictionary attack is a method of breaking a cipher by trying each word from a precompiled list, known as a dictionary, as the potential decryption key.

Q: How does the Vigenere cipher work?

The Vigenere cipher is a polyalphabetic substitution cipher that encrypts plaintext messages by shifting each letter by a corresponding letter from a keyword.

Q: Can the Vigenere cipher be cracked?

Yes, the Vigenere cipher can be cracked using various techniques, such as the dictionary attack discussed in this article.

Q: How effective is a dictionary attack on the Vigenere cipher?

The effectiveness of a dictionary attack depends on the size and quality of the dictionary used. Using a comprehensive and up-to-date dictionary increases the chances of successfully cracking the Vigenere cipher.

Q: Are there any countermeasures against dictionary attacks on the Vigenere cipher?

One countermeasure is to use a longer and more random keyword that is not easily guessable. Additionally, modern cryptographic algorithms, such as symmetric-key ciphers and public-key cryptography, provide stronger security than the Vigenere cipher.

Q: Can Python be used to perform a dictionary attack on the Vigenere cipher?

Yes, Python is a versatile programming language that can be used to implement a dictionary attack on the Vigenere cipher, as demonstrated in this article.

Also Read: Ultimate Guide to Using os.environ in Python


In conclusion, the Vigenere cipher is a classic encryption method that has its vulnerabilities, including the dictionary attack.

Python provides a powerful toolset to implement and experiment with various cryptographic attacks, such as the dictionary attack on the Vigenere cipher.

Also Read: 19 Pythonic Ways to Replace if-else Statements

By understanding the weaknesses of encryption methods, we can strengthen our overall cryptographic systems and ensure the security of sensitive information.

Remember, cryptography is an ever-evolving field, and it is crucial to stay updated with the latest advancements and best practices to protect our data from malicious attacks.

Also Read: Boost Python Code Efficiency: Eliminating Loops for Enhanced Performance