-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vigenere.py
55 lines (43 loc) · 2.21 KB
/
Vigenere.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Description: This program encrypts and decrypts a message using the Vigenere cipher.
# Author: Lerynnx (GitHub)
# Date: 2024-03-12
# Version: 1.0
# Do not remove this Attribution if you use this code.
# Nottify the author if you want to use this code before using it.
def vigenere_encode(message, key):
encoded_message = ""
alphabet = "abcdefghijklmnñopqrstuvwxyz"
for i in range(len(message)):
# Get the index of the character in the alphabet
message_index = alphabet.index(message[i])
key_index = alphabet.index(key[i % len(key)])
# Calculate the shift and get the encoded character
shift = (message_index + key_index) % 27
encoded_character = alphabet[shift]
# Show the operation performed to calculate the encoded index
print(f"({message[i]}+{key[i % len(key)]})mod 27 --> ({message_index}+{key_index})mod 27 = {shift} --> {encoded_character}")
# Add the encoded character to the encoded message
encoded_message += encoded_character
return encoded_message
def vigenere_decode(encoded_message, key):
decoded_message = ""
alphabet = "abcdefghijklmnñopqrstuvwxyz"
for i in range(len(encoded_message)):
# Get the index of the encoded character in the alphabet
encoded_index = alphabet.index(encoded_message[i])
key_index = alphabet.index(key[i % len(key)])
# Calculate the shift and get the decoded character
shift = (encoded_index - key_index) % 27
decoded_character = alphabet[shift]
# Show the operation performed to calculate the encoded index
print(f"({encoded_message[i]}-{key[i % len(key)]})mod 27 --> ({encoded_index}-{key_index})mod 27 = {shift} --> {decoded_character}")
# Add the decoded character to the decoded message
decoded_message += decoded_character
return decoded_message
message = "WHRYQKYVOMUBJHAMHRWWBACXMJ"
key = "soyunbuenestudiante"
#TODO it's case sensitive, do not use upper case
encoded_message = vigenere_encode(message, key)
print("Encoded message:", encoded_message)
decoded_message = vigenere_decode(encoded_message.lower(), key)
print("Decoded message:", decoded_message)