-
Notifications
You must be signed in to change notification settings - Fork 2
/
vigenere.py
51 lines (39 loc) · 1.29 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
"""
vigenere.py
This module provides functions for transforming text using a Vigenere cipher.
Ben Davies
2018 07 28
"""
CHARS = [c for c in (chr(i) for i in range(32, 127))]
def transform(text, key, want_decrypted=False):
"""Transform the supplied text and return the result.
Args:
text (str): The text to transform.
key (str): The key with which to apply the transformation.
want_decrypted (bool): Whether to encrypt (False) or decrypt (True).
"""
res = ""
for i, c in enumerate(text):
if c not in CHARS:
res += c
else:
text_index = CHARS.index(c)
key_index = CHARS.index(key[i % len(key)])
if want_decrypted:
key_index *= -1
res += CHARS[(text_index + key_index) % len(CHARS)]
return res
def encrypt(text, key):
"""Encrypt the supplied text and return the result.
Args:
text (str): The text to encrypt.
key (str): The key with which to perform the encryption.
"""
return transform(text, key)
def decrypt(text, key):
"""Decrypt the supplied text and return the result.
Args:
text (str): The text to decrypt.
key (str): The key with which to perform the decryption.
"""
return transform(text, key, True)