-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVigenereCipher.py
More file actions
24 lines (24 loc) · 844 Bytes
/
Copy pathVigenereCipher.py
File metadata and controls
24 lines (24 loc) · 844 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def vigenere_cipher(message, key, method = "decode"):
decoded_message = ''
keyphrase = ''
alph = 'abcdefghijklmnopqrstuvwxyz'
letter_point = 0
for i in range(len(message)):
if message[i] not in alph:
keyphrase += message[i]
else:
keyphrase += key[letter_point]
letter_point = (letter_point + 1) % len(key)
if method == "decode":
for i in range(len(message)):
if message[i] not in alph:
decoded_message += message[i]
else:
decoded_message += alph[(alph.find(message[i]) + alph.find(keyphrase[i])) % len(alph)]
elif method == "encode":
for i in range(len(message)):
if message[i] not in alph:
decoded_message += message[i]
else:
decoded_message += alph[(alph.find(message[i]) - alph.find(keyphrase[i])) % len(alph)]
return decoded_message