-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path049-Vigenere-Cypher.py
33 lines (27 loc) · 1.12 KB
/
049-Vigenere-Cypher.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
def vigenere_cipher(text, key, mode):
"""
Encrypts or decrypts the given text using the Vigenère Cipher with the given key.
Parameters:
text (str): The text to encrypt or decrypt.
key (str): The key to use for the encryption or decryption.
mode (str): The mode to use - 'encrypt' to encrypt the text, 'decrypt' to decrypt it.
Returns:
str: The encrypted or decrypted text.
"""
result = ""
key_index = 0
for i in range(len(text)):
# If the current character is a letter, shift it by the corresponding key letter
if text[i].isalpha():
key_letter = key[key_index % len(key)]
shift = ord(key_letter.upper()) - 65
if mode == "encrypt":
shifted_letter = chr((ord(text[i].upper()) + shift - 65) % 26 + 65)
else:
shifted_letter = chr((ord(text[i].upper()) - shift - 65) % 26 + 65)
result += shifted_letter
key_index += 1
# Otherwise, leave it as is
else:
result += text[i]
return result