Python code for Vigenere Cipher
The Vigenère cipher is a polyalphabetic substitution cipher that was invented by Blaise de Vigenère in the 16th century. It is a type of substitution cipher that uses a repeating keyword to encrypt a message.
In the Vigenère cipher, each letter of the plaintext is shifted by a value determined by the corresponding letter in the keyword. The key determines the number of positions each letter of the plaintext will be shifted, with each letter of the keyword corresponding to a different shift value.
The Vigenère cipher is considered to be more secure than monoalphabetic substitution ciphers (such as the Caesar cipher), as the same plaintext letter can be encrypted to different ciphertext letters depending on the position of the letter in the plaintext. This makes it more difficult for attackers to analyze and decipher the ciphertext.
Code for Vigenere Cipher
def vigenere_cipher(plaintext, key):
ciphertext = ""
key_index = 0
key = key.upper()
for char in plaintext:
if char.isalpha():
shift = ord(key[key_index % len(key)]) - 65
shift_char = chr((ord(char.upper()) - 65 + shift) % 26 + 65)
if char.islower():
shift_char = shift_char.lower()
ciphertext += shift_char
key_index += 1
else:
ciphertext += char
return ciphertext
plaintext = "HELLO WORLD"
key = "abc"
print("Plaintext: ", plaintext)
print("Key: ", key)
print("Ciphertext: ", vigenere_cipher(plaintext, key))
The Vigenère cipher is a polyalphabetic substitution cipher that uses a repeating keyword to encrypt the message. The encryption process involves shifting each letter of the plaintext by a value determined by the corresponding letter in the keyword.
This implementation first converts the key to uppercase, then iterates over each character in the plaintext. If the current character is an alphabetical character, the corresponding letter in the keyword is used to determine the shift value, and the character is shifted accordingly. If the current character is not an alphabetical character, it is added to the ciphertext without any changes. The resulting ciphertext is then returned as the output of the function.
Explanation of the code
The code implements the Vigenère cipher, which is a polyalphabetic substitution cipher that uses a repeating keyword to encrypt a message. The encryption process involves shifting each letter of the plaintext by a value determined by the corresponding letter in the keyword.
The implementation uses the following steps:
- The key is first converted to uppercase.
- A
key_indexvariable is initialized to 0, which will be used to keep track of the current position in the keyword. - A for loop is used to iterate over each character in the plaintext.
- If the current character is an alphabetical character, the corresponding letter in the keyword is used to determine the shift value. The shift value is calculated as the difference between the ASCII value of the current letter in the keyword and 65 (which is the ASCII value of ‘A’).
- The character is shifted accordingly by adding the shift value to its ASCII value. If the character is lowercase, it is converted back to lowercase after the shift.
- The resulting character is added to the ciphertext.
- The
key_indexvariable is incremented by 1. - If the current character is not an alphabetical character, it is added to the ciphertext without any changes.
- The resulting ciphertext is returned as the output of the function.
Unit Tests
import unittest
class TestVigenereCipher(unittest.TestCase):
def test_vigenere_cipher(self):
plaintext = "HELLO WORLD"
key = "abc"
expected_output = "IFMMP XPSME"
self.assertEqual(vigenere_cipher(plaintext, key), expected_output)
if __name__ == '__main__':
unittest.main()
In this test case, the vigenere_cipher function is called with the input plaintext and key, and the output is compared with the expected output. If the output is the same as the expected output, the test will pass. Otherwise, the test will fail.
Code to decrypt a message encrypted with Vigenere Cipher
def vigenere_decipher(ciphertext, key):
plaintext = ""
key_index = 0
key = key.upper()
for char in ciphertext:
if char.isalpha():
shift = ord(key[key_index % len(key)]) - 65
shift_char = chr((ord(char.upper()) - shift - 65) % 26 + 65)
if char.islower():
shift_char = shift_char.lower()
plaintext += shift_char
key_index += 1
else:
plaintext += char
return plaintext
ciphertext = "IFMMP XPSME"
key = "abc"
print("Ciphertext: ", ciphertext)
print("Key: ", key)
print("Plaintext: ", vigenere_decipher(ciphertext, key))
The decryption process for the Vigenère cipher is similar to the encryption process, but with the shift value subtracted instead of added to each letter.
This implementation first converts the key to uppercase, then iterates over each character in the ciphertext. If the current character is an alphabetical character, the corresponding letter in the keyword is used to determine the shift value, and the character is shifted accordingly. If the current character is not an alphabetical character, it is added to the plaintext without any changes. The resulting plaintext is then returned as the output of the function.