-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcaesar.py
More file actions
executable file
·38 lines (32 loc) · 1.1 KB
/
caesar.py
File metadata and controls
executable file
·38 lines (32 loc) · 1.1 KB
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
import string
from ciphers import Cipher
class Caesar(Cipher):
FORWARD = string.ascii_uppercase * 3
def __init__(self, offset=3):
self.offset = offset
self.FORWARD = string.ascii_uppercase + string.ascii_uppercase[
:self.offset + 1]
self.BACKWARD = string.ascii_uppercase[
:self.offset + 1] + string.ascii_uppercase
def encrypt(self, text):
output = []
text = text.upper()
for char in text:
try:
index = self.FORWARD.index(char)
except ValueError:
output.append(char)
else:
output.append(self.FORWARD[index + self.offset])
return ''.join(output)
def decrypt(self, text):
output = []
text = text.upper()
for char in text:
try:
index = self.BACKWARD.index(char)
except ValueError:
output.append(char)
else:
output.append(self.BACKWARD[index - self.offset])
return ''.join(output)