Vigenère Cipher: Python Encryption Methods

Creating the Vigenère Cipher with Python

In this guide I will explain what the Vigenère Cipher is and what its used for. Then I will show you how to create your own Vigenère Cipher program with Python.

What is the Vigenère Cipher?

The Vigenère Cipher is a classic polyalphabetic substitution cipher that adds a layer of complexity to the simple Caesar cipher by using a keyword to determine the shift for each letter in the plaintext. It was developed by Blaise de Vigenère, a French diplomat, in the 16th century. Vigenère’s cipher is sometimes referred to as the “unbreakable cipher” due to its enhanced security compared to the Caesar cipher.

The Vigenère Cipher was widely used for centuries, particularly during the 19th and early 20th centuries, before more advanced encryption methods were developed. It gained a reputation for being relatively secure compared to earlier ciphers. Today, it is primarily used for educational purposes and historical reference, as modern encryption methods are significantly more secure.

Here’s an in-depth explanation of the Vigenère Cipher:

Key and Key Length:

  • The Vigenère Cipher uses a keyword, which is a secret word or phrase agreed upon by the sender and receiver. The keyword is repeated to match the length of the plaintext message.

Creating the Vigenère Table:

  • The Vigenère Cipher uses a tabular arrangement called the Vigenère Table (or Vigenère Square), which consists of 26 rows and 26 columns. The rows and columns are labeled with the letters of the alphabet, with ‘A’ to ‘Z’ arranged in a cyclical pattern. This table is used to determine the shift for each letter in the message.

Encryption Process:

  • Align the keyword with the plaintext message, repeating the keyword as necessary to match the length of the message.
  • For each letter in the plaintext, find the corresponding row in the Vigenère Table by using the letter from the keyword. This row represents the shift values for that letter.
  • Look up the shift value in the row to determine how much the letter in the plaintext should be shifted.
  • Shift the letter in the plaintext by the determined value to get the corresponding ciphertext letter.
  • Repeat this process for each letter in the message.

Decryption Process:

  • To decrypt a Vigenère-encrypted message, the recipient uses the same keyword and the Vigenère Table. However, instead of shifting the letters in the plaintext, they shift the letters in the ciphertext in the opposite direction to recover the original message.

One important feature of the Vigenère Cipher is that it uses multiple Caesar ciphers, each with a different shift value based on the keyword. This makes it more secure than the simple Caesar cipher, which uses a single fixed shift value.

How Secure is it?

The Vigenère Cipher is not unbreakable, and it can still be cracked with sufficient computational power and appropriate cryptanalysis techniques. The key length and the complexity of the keyword greatly influence the security of the cipher. Longer keywords with more randomness make the Vigenère Cipher more secure.

The Vigenère Cipher Table

You can create your own Vigenère Cipher table following the same pattern as the table below. The table can help you manually decipher messages, providing you have the key.

 ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZA
BCDEFGHIJKLMNOPQRSTUVWXYZAB
CDEFGHIJKLMNOPQRSTUVWXYZABC
DEFGHIJKLMNOPQRSTUVWXYZABCD
EFGHIJKLMNOPQRSTUVWXYZABCDE
FGHIJKLMNOPQRSTUVWXYZABCDEF
GHIJKLMNOPQRSTUVWXYZABCDEFG
HIJKLMNOPQRSTUVWXYZABCDEFGH
IJKLMNOPQRSTUVWXYZABCDEFGHI
JKLMNOPQRSTUVWXYZABCDEFGHIJ
KLMNOPQRSTUVWXYZABCDEFGHIJK
LMNOPQRSTUVWXYZABCDEFGHIJKL
MNOPQRSTUVWXYZABCDEFGHIJKLM
NOPQRSTUVWXYZABCDEFGHIJKLMN
OPQRSTUVWXYZABCDEFGHIJKLMNO
PQRSTUVWXYZABCDEFGHIJKLMNOP
QRSTUVWXYZABCDEFGHIJKLMNOPQ
RSTUVWXYZABCDEFGHIJKLMNOPQR
STUVWXYZABCDEFGHIJKLMNOPQRS
TUVWXYZABCDEFGHIJKLMNOPQRST
UVWXYZABCDEFGHIJKLMNOPQRSTU
VWXYZABCDEFGHIJKLMNOPQRSTUV
WXYZABCDEFGHIJKLMNOPQRSTUVW
XYZABCDEFGHIJKLMNOPQRSTUVWX
YZABCDEFGHIJKLMNOPQRSTUVWXY
ZABCDEFGHIJKLMNOPQRSTUVWXYZ

Creating the Vigenère Cipher with Python

To use the Python script below you have 3 input areas; message, key, mode.

To use this code you must:

  • Add a message to the message variable
  • Add a key to the key variable
  • Select whether you want to encrypt or decrypt in the mode variable.

Then run the script to get the encrypted/decrypted message.

Remember the same key for the encryption needs to be used for decryption too. If you lose the key, you will have a hard time decrypting the data.

import pyperclip #pip3 install pyperclip.py
message="Message to be encrypted" #the data to be encrypted or decrypted
key="ThisIsMyKey"   #the key to use to encrypt or decrypt data
mode="encrypt"     #switch mode to encrypt or decrypt
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'    #Characters to be used
##########  DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOUR DOING  ##########
def main():
    myMessage = message
    myKey = key
    myMode = mode
    if myMode == 'encrypt':
        translated = encryptMessage(myKey, myMessage)
    elif myMode == 'decrypt':
        translated = decryptMessage(myKey, myMessage)
    print('%sed message:' % (myMode.title()))
    print(translated)
    print()
def encryptMessage(key, message):
    return translateMessage(key, message, 'encrypt')
def decryptMessage(key, message):
    return translateMessage(key, message, 'decrypt')
def translateMessage(key, message, mode):
    translated = [] # stores the encrypted/decrypted message string
    keyIndex = 0
    key = key.upper()
    for symbol in message:
        num = LETTERS.find(symbol.upper())
        if num != -1:
            if mode == 'encrypt':
                num += LETTERS.find(key[keyIndex])
            elif mode == 'decrypt':
                num -= LETTERS.find(key[keyIndex])
            num %= len(LETTERS)
            if symbol.isupper():
                translated.append(LETTERS[num])
            elif symbol.islower():
                translated.append(LETTERS[num].lower())
            keyIndex += 1
            if keyIndex == len(key):
                keyIndex = 0
        else:
            translated.append(symbol)
    return ''.join(translated)
if __name__ == '__main__':
   main()

Once you have inputted the required parameters just run the script to see your encrypted/decrypted message printed to the terminal or console.

Conclusion

In this exploration of the Vigenère Cipher, we’ve uncovered a fascinating historical encryption method. By creating a Python program to implement it, we’ve not only delved into the past but also gained a valuable understanding of how ciphers have evolved to protect sensitive information. The Vigenère Cipher, with its polyalphabetic approach, provides an engaging challenge for both the curious and the cryptographers. It’s a reminder that the world of cryptography is a blend of art and science, where historical techniques meet modern coding.

As we conclude our journey through the Vigenère Cipher and Python, we’re equipped with a deeper appreciation of encryption’s past and a practical tool to use in the present. The secrets encoded by Vigenère are now within your grasp.

The Vigenère Cipher is by far my favorite cipher I have covered on this site so far.

That’s All Folks!

Find more of our Python guides here: Python Guides

Recommendation:

Big Book of Small Python Programs: 81 Easy Practice Programs: https://amzn.to/3rGZjCR

Luke Barber

Hello, fellow tech enthusiasts! I'm Luke, a passionate learner and explorer in the vast realms of technology. Welcome to my digital space where I share the insights and adventures gained from my journey into the fascinating worlds of Arduino, Python, Linux, Ethical Hacking, and beyond. Armed with qualifications including CompTIA A+, Sec+, Cisco CCNA, Unix/Linux and Bash Shell Scripting, JavaScript Application Programming, Python Programming and Ethical Hacking, I thrive in the ever-evolving landscape of coding, computers, and networks. As a tech enthusiast, I'm on a mission to simplify the complexities of technology through my blogs, offering a glimpse into the marvels of Arduino, Python, Linux, and Ethical Hacking techniques. Whether you're a fellow coder or a curious mind, I invite you to join me on this journey of continuous learning and discovery.

Leave a Reply

Your email address will not be published. Required fields are marked *

Verified by MonsterInsights