Vigenère Cipher (Python)

Previously I looked at the Vigenère cipher, but I did not have a working Python example. After some thought and consideration I came to the realisation that the Vigenère cipher is pretty much just a Caesar cipher with a shift that changes each letter, which then allowed me to figure out how to make it in Python.

geektechstuff_vigenere_cipher_1
Vigenere Cipher (Python)
Vigenere Cipher (Python)
Vigenere Cipher (Python)
geektechstuff_vigenere_cipher_3
Vigenere Cipher (Python)

#!/usr/bin/python3
# GeekTechStuff
“””
To find out more about the Vigenère Cipher please visit: https://geektechstuff.com/2019/12/25/vigenere-cipher/
“””
def vigenere_enc():
alphabet = “abcdefghijklmnopqrstuvwxyz”
input_string = “”
enc_key = “”
enc_string = “”
# Takes encrpytion key from user
enc_key = input(“Please enter encryption key: “)
enc_key = enc_key.lower()
# Takes string from user
input_string = input(“Please enter a string of text: “)
input_string = input_string.lower()
# Lengths of input_string
string_length = len(input_string)
# Expands the encryption key to make it longer than the inputted string
expanded_key = enc_key
expanded_key_length = len(expanded_key)
while expanded_key_length < string_length:
# Adds another repetition of the encryption key
expanded_key = expanded_key + enc_key
expanded_key_length = len(expanded_key)
key_position = 0
for letter in input_string:
if letter in alphabet:
# cycles through each letter to find it’s numeric position in the alphabet
position = alphabet.find(letter)
# moves along key and finds the characters value
key_character = expanded_key[key_position]
key_character_position = alphabet.find(key_character)
key_position = key_position + 1
# changes the original of the input string character
new_position = position + key_character_position
if new_position > 26:
new_position = new_position – 26
new_character = alphabet[new_position]
enc_string = enc_string + new_character
else:
enc_string = enc_string + letter
return(enc_string)
def vigenere_dec():
alphabet = “abcdefghijklmnopqrstuvwxyz”
input_string = “”
dec_key = “”
dec_string = “”
# Takes encrpytion key from user
dec_key = input(“Please enter encryption key: “)
dec_key = dec_key.lower()
# Takes string from user
input_string = input(“Please enter a string of text: “)
input_string = input_string.lower()
# Lengths of input_string
string_length = len(input_string)
# Expands the encryption key to make it longer than the inputted string
expanded_key = dec_key
expanded_key_length = len(expanded_key)
while expanded_key_length < string_length:
# Adds another repetition of the encryption key
expanded_key = expanded_key + dec_key
expanded_key_length = len(expanded_key)
key_position = 0
for letter in input_string:
if letter in alphabet:
# cycles through each letter to find it’s numeric position in the alphabet
position = alphabet.find(letter)
# moves along key and finds the characters value
key_character = expanded_key[key_position]
key_character_position = alphabet.find(key_character)
key_position = key_position + 1
# changes the original of the input string character
new_position = position – key_character_position
if new_position > 26:
new_position = new_position + 26
new_character = alphabet[new_position]
dec_string = dec_string + new_character
else:
dec_string = dec_string + letter
return(dec_string)
# Testing
print(vigenere_enc())
print(vigenere_dec())

I’ve uploaded the full Python program into my Python Encryption GitHub repository: https://github.com/geektechdude/Python_Encryption with file directly at: https://github.com/geektechdude/Python_Encryption/blob/master/geektechstuff_vigenere_cipher.py

2 responses to “Vigenère Cipher (Python)”

  1. Instant Messaging App With Vigenère Encryption (Python) – Geek Tech Stuff Avatar

    […] and encryption, which has included a Python program to brute force the Caesar cipher, Python to encrypt / decrypt with the Vigenère cipher , some Morse code and RSA Public […]

    Like

  2. Encrypting Files With Ansible Vault (Linux) – Geek Tech Stuff Avatar

    […] But cat or a text editor (e.g. Nano or Vim) could be used to get similar results. The first line of the file shows that Ansible Vault has encrypted the file using a AES 256 cipher. […]

    Like

Welcome to GeekTechStuff

my home away from home and where I will be sharing my adventures in the world of technology and all things geek.

The technology subjects have varied over the years from Python code to handle ciphers and Pig Latin, to IoT sensors in Azure and Python handling Bluetooth, to Ansible and Terraform and material around DevOps.

Let’s connect