-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathatbash.py
More file actions
executable file
·59 lines (49 loc) · 1.74 KB
/
atbash.py
File metadata and controls
executable file
·59 lines (49 loc) · 1.74 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from ciphers import Cipher
from helpers import Helpers
class Atbash(Cipher):
def __init__(self):
# alphabet
self.helpers = Helpers()
self.alphabet = self.helpers.alphabet()
# reverse alphabet
self.reverse = self.alphabet[::-1]
def encrypt(self, text):
"""
Encrypt text by building a dictionary of key value pairs
for the plain text char : encrypted text char
:param text: plain text string
:return: encrypted string
"""
encryption_key = {}
output = []
text = text.upper()
# build encrypt and decrypt keys as lists of corresponding
# key value pairs ( {A:Z, B:Y} etc.)
for i in range(len(self.alphabet)):
encryption_key[self.alphabet[i]] = self.reverse[i]
for letter in text:
if letter in encryption_key:
# match the incoming letter with its counterpart
output.append(encryption_key[letter])
else:
# Pass punctuation and numbers through normally
output.append(letter)
return ''.join(output)
def decrypt(self, text):
"""
Decrypt text by building a dictionary of key value pairs
Essentially the same as the process above, but in reverse
:param text:
:return:
"""
decryption_key = {}
output = []
text = text.upper()
for i in range(len(self.alphabet)):
decryption_key[self.reverse[i]] = self.alphabet[i]
for letter in text:
if letter not in decryption_key:
output.append(letter)
else:
output.append(decryption_key[letter])
return ''.join(output)