Vigenere Cipher

 

Vigenère Cipher

Definition:

  • The Vigenère cipher is a method for encrypting alphabetic text.
  • It's a polyalphabetic substitution cipher, meaning it uses multiple Caesar ciphers with varying shifts.
  • Encryption relies on a keyword, which determines the shift amount for each letter of the plaintext.

Example:

  • Plaintext: ATTACK AT DAWN
  • Key: SECRET

Encryption process:

  1. Match each letter of the plaintext with the corresponding letter of the key (repeated if necessary).
    • ATTACK AT DAWN
    • SECRETSECRETS
  2. Use the Vigenère square to find the cipher text letter. The row index is the plaintext letter, and the column index is determined by the key letter.
    • Ciphertext: LXTFNL LPNFNLP

Applications (Historical):

  • The Vigenère cipher was once considered a very secure method due to its complex encryption.
  • It was used for military communication in various conflicts, including the American Civil War.

Limitations:

  • The Vigenère cipher is vulnerable to Kasiski Examination, which can reveal the key length.
  • With the key length known, cryptanalysis techniques can be applied to break the cipher.

Related Algorithms:

  • Caesar Cipher: A basic substitution cipher with a single shift value for the entire message.
  • Autokey Cipher: A variant of the Vigenère cipher where the key is derived from the plaintext itself.

Note: In modern cryptography, the Vigenère cipher is not considered secure due to its vulnerabilities. More robust encryption algorithms are used for secure communication.


Python code:

def vigenere_cipher(text, key, mode='encrypt'):

    result = []

    key = [ord(k) - 65 for k in key.upper()]

    key_length = len(key)

 

    for i, char in enumerate(text):

        if char.isalpha():

            shift = key[i % key_length]

            shift_base = 65 if char.isupper() else 97

            shift = shift if mode == 'encrypt' else -shift

            result.append(chr((ord(char) - shift_base + shift) % 26 + shift_base))

        else:

            result.append(char)

 

    return ''.join(result)

 

# Usage

message = "HELLO, WORLD!"

key = "KEY"

 

encrypted_message = vigenere_cipher(message, key, 'encrypt')

decrypted_message = vigenere_cipher(encrypted_message, key, 'decrypt')

 

print(f"Encrypted: {encrypted_message}")

print(f"Decrypted: {decrypted_message}")


Also see:
Ceaser Cipher

Popular Post

MindMaps

Featured post

Question 1: Reverse Words in a String III

  def reverseWords(s: str) -> str: words = s.split() return ' '.join(word[::-1] for word in words)