Cryptography: Vigenère Cipher

Cryptography: Vigenère Cipher

·

3 min read

The Vigenère Cipher is a classical encryption technique that extends the Caesar Cipher by using a key to shift letters in a cyclic manner. Unlike the Caesar Cipher, which uses a single shift value, the Vigenère Cipher applies multiple shifts based on the letters of a given keyword. This makes it more resistant to simple frequency analysis.

In this tutorial, we will explain how the Vigenère Cipher works, its mathematical foundation, and how to implement it in C# and Python.

How Does the Vigenère Cipher Work?

The Vigenère Cipher encrypts text by applying a series of Caesar shifts determined by a keyword. Each letter of the keyword corresponds to a shift value, which is then applied cyclically to the plaintext.

Encryption Process

  1. Write the plaintext message.

  2. Repeat the keyword until it matches the length of the plaintext.

  3. Convert each letter of the plaintext and keyword to a numerical value (A=0, B=1, ..., Z=25).

  4. Add the corresponding values (mod 26) to get the ciphertext.

  5. Convert the resulting numbers back to letters.

Decryption Process

  1. Convert the ciphertext and keyword to their respective numerical values.

  2. Subtract the keyword value from the ciphertext value (mod 26) to retrieve the original letter.

  3. Convert the resulting numbers back to letters.

Example Encryption

Let's encrypt "HELLO" with the keyword "KEY".

PlaintextHELLO
KeyKEYKE
Shift10424104
EncryptedRIJVS

Thus, "HELLO" becomes "RIJVS".

Implementation in C#

Let's implement both encryption and decryption in C#.

using System;

class VigenereCipher
{
    static string Encrypt(string text, string key)
    {
        string result = "";
        int keyIndex = 0;
        key = key.ToUpper();

        foreach (char c in text.ToUpper())
        {
            if (char.IsLetter(c))
            {
                int shift = key[keyIndex] - 'A';
                char encryptedChar = (char)(((c - 'A' + shift) % 26) + 'A');
                result += encryptedChar;
                keyIndex = (keyIndex + 1) % key.Length;
            }
            else
            {
                result += c;
            }
        }
        return result;
    }

    static string Decrypt(string text, string key)
    {
        string result = "";
        int keyIndex = 0;
        key = key.ToUpper();

        foreach (char c in text.ToUpper())
        {
            if (char.IsLetter(c))
            {
                int shift = key[keyIndex] - 'A';
                char decryptedChar = (char)(((c - 'A' - shift + 26) % 26) + 'A');
                result += decryptedChar;
                keyIndex = (keyIndex + 1) % key.Length;
            }
            else
            {
                result += c;
            }
        }
        return result;
    }

    static void Main()
    {
        string plaintext = "HELLO";
        string key = "KEY";
        string encrypted = Encrypt(plaintext, key);
        string decrypted = Decrypt(encrypted, key);
        Console.WriteLine($"Encrypted: {encrypted}");
        Console.WriteLine($"Decrypted: {decrypted}");
    }
}

Output

Encrypted: RIJVS
Decrypted: HELLO

Implementation in Python

Here's how we can implement the Vigenère Cipher in Python.

def vigenere_encrypt(plaintext, key):
    key = key.upper()
    encrypted_text = ""
    key_index = 0

    for char in plaintext.upper():
        if char.isalpha():
            shift = ord(key[key_index]) - ord('A')
            encrypted_char = chr(((ord(char) - ord('A') + shift) % 26) + ord('A'))
            encrypted_text += encrypted_char
            key_index = (key_index + 1) % len(key)
        else:
            encrypted_text += char

    return encrypted_text


def vigenere_decrypt(ciphertext, key):
    key = key.upper()
    decrypted_text = ""
    key_index = 0

    for char in ciphertext.upper():
        if char.isalpha():
            shift = ord(key[key_index]) - ord('A')
            decrypted_char = chr(((ord(char) - ord('A') - shift + 26) % 26) + ord('A'))
            decrypted_text += decrypted_char
            key_index = (key_index + 1) % len(key)
        else:
            decrypted_text += char

    return decrypted_text


# Test the functions
plaintext = "HELLO"
key = "KEY"
ciphertext = vigenere_encrypt(plaintext, key)
decrypted_text = vigenere_decrypt(ciphertext, key)

print("Encrypted:", ciphertext)
print("Decrypted:", decrypted_text)

Output

Encrypted: RIJVS
Decrypted: HELLO

Security Considerations

  • The Vigenère Cipher is vulnerable to cryptanalysis (e.g., Kasiski examination) if the keyword is short or reused.

  • Modern cryptographic algorithms (e.g., AES) are much more secure and recommended for real-world applications.