Skip to content

Commit 14ffbf2

Browse files
committed
add vigenere cipher 2
1 parent b176295 commit 14ffbf2

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Implement the Vigenère Cipher in Python](https://thepythoncode.com/article/implementing-the-vigenere-cipher-in-python)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
colorama
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Import sys for system operations and colorama for colored output.
2+
import sys
3+
from colorama import init, Fore
4+
5+
# Initialise colorama
6+
init()
7+
8+
9+
# Function to Encrypt using the Vigenère cipher.
10+
def vigenere_encrypt(plain_text, key):
11+
encrypted_text = ''
12+
13+
# Repeat the key to match the length of the plaintext.
14+
key_repeated = (key * (len(plain_text) // len(key))) + key[:len(plain_text) % len(key)]
15+
16+
# Iterate through each character in the plaintext.
17+
for i in range(len(plain_text)):
18+
# Check if the character is an alphabet letter.
19+
if plain_text[i].isalpha():
20+
# Calculate the shift based on the corresponding key letter.
21+
shift = ord(key_repeated[i].upper()) - ord('A')
22+
23+
# Encrypt uppercase and lowercase letters separately.
24+
if plain_text[i].isupper():
25+
encrypted_text += chr((ord(plain_text[i]) + shift - ord('A')) % 26 + ord('A'))
26+
else:
27+
encrypted_text += chr((ord(plain_text[i]) + shift - ord('a')) % 26 + ord('a'))
28+
else:
29+
# If the character is not an alphabet letter, keep it unchanged.
30+
encrypted_text += plain_text[i]
31+
32+
# Return the final encrypted text
33+
return encrypted_text
34+
35+
36+
# Decryption function for the Vigenère cipher
37+
def vigenere_decrypt(cipher_text, key):
38+
decrypted_text = ''
39+
40+
# Repeat the key to match the length of the ciphertext
41+
key_repeated = (key * (len(cipher_text) // len(key))) + key[:len(cipher_text) % len(key)]
42+
43+
# Iterate through each character in the ciphertext
44+
for i in range(len(cipher_text)):
45+
# Check if the character is an alphabet letter
46+
if cipher_text[i].isalpha():
47+
# Calculate the shift based on the corresponding key letter
48+
shift = ord(key_repeated[i].upper()) - ord('A')
49+
50+
# Decrypt uppercase and lowercase letters separately
51+
if cipher_text[i].isupper():
52+
decrypted_text += chr((ord(cipher_text[i]) - shift - ord('A')) % 26 + ord('A'))
53+
else:
54+
decrypted_text += chr((ord(cipher_text[i]) - shift - ord('a')) % 26 + ord('a'))
55+
else:
56+
# If the character is not an alphabet letter, keep it unchanged
57+
decrypted_text += cipher_text[i]
58+
59+
# Return the final decrypted text
60+
return decrypted_text
61+
62+
63+
key = "KEY"
64+
# Get user input (Message to encrypt).
65+
plaintext = input('[!] Enter your message: ')
66+
67+
# Encrypt the plaintext using the Vigenère cipher
68+
cipher_text = vigenere_encrypt(plaintext, key)
69+
70+
# Print the results
71+
print(f"[+] Plaintext: {plaintext}")
72+
print(f"{Fore.GREEN}[+] Ciphertext: {cipher_text}")
73+
74+
# Ask if user wants to decrypt the message (just to see the functionality.)
75+
ask_to_decrypt = input('\n\n[?] Do you want to decrypt the message?\n[?] Y or N: ').lower()
76+
77+
# If user wants to.
78+
if ask_to_decrypt == 'y':
79+
# Decrypt the ciphertext back to the original plaintext.
80+
decrypted_text = vigenere_decrypt(cipher_text, key)
81+
print(f"{Fore.GREEN}[+] Decrypted text: {decrypted_text}")
82+
83+
# If user does not want to.
84+
elif ask_to_decrypt == 'n':
85+
sys.exit()
86+
# When an invalid input is entered.
87+
else:
88+
print(f"{Fore.RED}[-] Invalid input.")

0 commit comments

Comments
 (0)