-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_cipher.py
More file actions
32 lines (27 loc) · 820 Bytes
/
simple_cipher.py
File metadata and controls
32 lines (27 loc) · 820 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
25
26
27
28
29
30
31
32
"""
Simple Cipher
"""
from itertools import cycle
from string import ascii_lowercase
from secrets import choice
class Cipher:
"""
Simple Cipher class
"""
def __init__(self, key=''.join(choice(ascii_lowercase) for _ in range(100))):
self.key = key
def encode(self, text, shift=1):
"""
encodes, using the simple cipher
* 26 represents the number of letters in the alphabet
* 97 is the ascii value for 'a'
"""
# zip the text and key, then shift the text by the key
return ''.join(
chr((ord(t) - 97 + shift * (ord(k) - 97)) % 26 + 97)
for t, k in zip(text, cycle(self.key)))
def decode(self, text):
"""
decodes, using the simple cipher
"""
return self.encode(text, shift=-1)