Skip to content

Added a Vignere cipher extension to the Ceasar cipher py file #367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions Caesar_Cipher/Vignere_README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<!--Please do not remove this part-->
![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

<p align="center">
<img src="assests/main.png" width=40% height=40%>

## 🛠️ 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
```

<img src="assests/colorgame.png" width=40% height=40%>

## 🤖 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!
38 changes: 38 additions & 0 deletions Caesar_Cipher/vignere_cipher.py
Original file line number Diff line number Diff line change
@@ -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}")