From c63210e3c4b298b1e05642fb29bcd875d57e6b11 Mon Sep 17 00:00:00 2001 From: TheekshaniPramodya Date: Tue, 15 Apr 2025 22:10:59 +0530 Subject: [PATCH] Added Vigenere Cipher script in Python by Theekshani --- .../vigenere_cipher_theekshani.py | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Encryption_Algorithms/vigenere_cipher_theekshani.py diff --git a/Encryption_Algorithms/vigenere_cipher_theekshani.py b/Encryption_Algorithms/vigenere_cipher_theekshani.py new file mode 100644 index 0000000..7e69bc0 --- /dev/null +++ b/Encryption_Algorithms/vigenere_cipher_theekshani.py @@ -0,0 +1,37 @@ +# vigenere_cipher_theekshani.py +# A basic implementation of the Vigenère Cipher + +def encrypt_vigenere(plaintext, key): + encrypted = "" + key_index = 0 + + for char in plaintext: + if char.isalpha(): + shift = ord(key[key_index % len(key)].lower()) - ord('a') + base = ord('A') if char.isupper() else ord('a') + encrypted += chr((ord(char) - base + shift) % 26 + base) + key_index += 1 + else: + encrypted += char + return encrypted + +def decrypt_vigenere(ciphertext, key): + decrypted = "" + key_index = 0 + + for char in ciphertext: + if char.isalpha(): + shift = ord(key[key_index % len(key)].lower()) - ord('a') + base = ord('A') if char.isupper() else ord('a') + decrypted += chr((ord(char) - base - shift) % 26 + base) + key_index += 1 + else: + decrypted += char + return decrypted + +# Example usage +plaintext = "HELLO WORLD" +key = "KEY" +cipher = encrypt_vigenere(plaintext, key) +print("Encrypted:", cipher) +print("Decrypted:", decrypt_vigenere(cipher, key))