-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvigenere.py
More file actions
23 lines (20 loc) · 885 Bytes
/
vigenere.py
File metadata and controls
23 lines (20 loc) · 885 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/env python
import string
# alphabet of lowercase, upppercase, digits and space
alphabet = string.ascii_lowercase + string.ascii_uppercase + string.digits + ' '
# vigenere cipher
def vigenere(msg, key, decrypt=False):
# result
result = ""
# control whether to encrypt (add key) or decrypt (subtract key)
mult = 1
if decrypt:
mult = -1
# for each index and character at this index in message
for i, c in enumerate(msg):
# get current letter of key to use
key_letter_to_use = key[i % len(key)]
# character of result is index of message character in alphabet plus/minus index of key letter
# in alphabet, mod 63 to wrap round if result is higher than length of alphabet
result += alphabet[(alphabet.index(c) + (mult * alphabet.index(key_letter_to_use))) % 63]
return result