From 6239413b1cbbfad08ce5d5c2bd3ee02239030aff Mon Sep 17 00:00:00 2001 From: Nenad Balaneskovic Date: Sun, 22 Jun 2025 09:05:11 +0200 Subject: [PATCH] Added a Vignere cipher class A class for implementing a Vignere cipher (file "vignere_cipher.py") as an extension of Caesar's cipher and its README file have been added. --- Caesar_Cipher/Vignere_README.txt | 60 ++++++++++++++++++++++++++++++++ Caesar_Cipher/vignere_cipher.py | 38 ++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 Caesar_Cipher/Vignere_README.txt create mode 100644 Caesar_Cipher/vignere_cipher.py diff --git a/Caesar_Cipher/Vignere_README.txt b/Caesar_Cipher/Vignere_README.txt new file mode 100644 index 00000000..6b354a02 --- /dev/null +++ b/Caesar_Cipher/Vignere_README.txt @@ -0,0 +1,60 @@ + +![Star Badge](https://img.shields.io/static/v1?label=%F0%9F%94%91&message=If%20Useful&style=style=flat&color=green) +![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.svg?v=103) + +# Vigenère Cipher: Classical Text Encryption + +

+ + +## 🛠️ Description + +This script implements the **Vigenère Cipher**, a classic polyalphabetic substitution encryption technique. It accepts a keyword from the user to encode and decode messages. The same keyword must be used to decrypt the output, making it a lightweight reversible encryption tool. This is a great intro project for learning about ciphers and key-based text transformations. + +The script demonstrates: +1. A `VigenereCipher` class with methods to encrypt and decrypt alphabetic strings +2. Character-level transformation based on a repeating keyword +3. Custom `transform()` logic that handles both encryption and decryption + +Feel free to enhance it with non-alphabet support, file-based encryption, or use it inside a larger pipeline. + +## ⚙️ Languages or Frameworks Used + +- Python 3.x standard library only +- No external packages required + +## 🌟 How to run + +To execute the script, run the following in your terminal: + +```bash +python vigenere_cipher.py +``` + +When prompted: +- Enter a **keyword** to serve as the encryption key +- Enter the plaintext you'd like to encrypt +- The script will output the encrypted text and decrypt it back immediately + +Use `CTRL + C` to exit at any point. + +## 📺 Demo + +An example prompt might look like: + +``` +Enter encryption keyword: lemon +Enter text to encrypt: hello world +Encrypted text: siprb xbpqe +Decrypted back: hello world +``` + + + +## 🤖 Author + +Script by Dr. rer. nat. Nenad Balaneskovic. + +Nenad Balaneskovic → [GitHub Profile](https://github.com/NenadBalaneskovic) + +Feel free to contribute your own enhancements or port this into other encryption schemes! \ No newline at end of file diff --git a/Caesar_Cipher/vignere_cipher.py b/Caesar_Cipher/vignere_cipher.py new file mode 100644 index 00000000..6d5b7b8e --- /dev/null +++ b/Caesar_Cipher/vignere_cipher.py @@ -0,0 +1,38 @@ +class VigenereCipher: + def __init__(self, keyword: str): + self.keyword = keyword.lower() + self.alphabet = 'abcdefghijklmnopqrstuvwxyz' + + def shift_char(self, c, key_c, encode=True): + if c not in self.alphabet: + return c + shift = self.alphabet.index(key_c) + if not encode: + shift = -shift + return self.alphabet[(self.alphabet.index(c) + shift) % 26] + + def transform(self, text: str, encode=True): + text = text.lower() + result = [] + key_len = len(self.keyword) + for i, c in enumerate(text): + key_c = self.keyword[i % key_len] + result.append(self.shift_char(c, key_c, encode)) + return ''.join(result) + + def encrypt(self, text: str) -> str: + return self.transform(text, True) + + def decrypt(self, text: str) -> str: + return self.transform(text, False) + +if __name__ == "__main__": + keyword = input("Enter encryption keyword: ").strip() + cipher = VigenereCipher(keyword) + + plaintext = input("Enter text to encrypt: ").strip() + encrypted = cipher.encrypt(plaintext) + print(f"Encrypted text: {encrypted}") + + decrypted = cipher.decrypt(encrypted) + print(f"Decrypted back: {decrypted}") \ No newline at end of file