-
Notifications
You must be signed in to change notification settings - Fork 31
/
atbash.py
54 lines (43 loc) · 1.48 KB
/
atbash.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
52
53
54
def atbash_encrypt(input_string):
"""Use atbash encryption to take an input string and
return the capitalized, reverse alphabetical position string.
Parameters
----------
input_string : str
input string to be encrypted
Returns
-------
atbash_string : str
atbash encrypted string
"""
alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
reverse_alpha = 'ZYXWVUTSRQPONMLKJIHGFEDCBA'
atbash_string = ''
for char in input_string:
char = char.upper()
if char in alpha:
position = alpha.find(char)
atbash_string += reverse_alpha[position]
else:
atbash_string = None
break
return atbash_string
def atbash_decrypt(atbash_string):
ALPHA='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
REVERSEALPHA='ZYXWVUTSRQPONMLKJIHGFEDCBA'
atbash_string=atbash_string.upper()
decrypted_string=''
for l in atbash_string:
if l in REVERSEALPHA:
letterindex=REVERSEALPHA.find(l)
decrypted_string=decrypted_string+ALPHA [letterindex]
else: decrypted_string=decrypted_string+l
return decrypted_string
def atbash_wrapper(input_string, method='encrypt'):
if method == 'encrypt':
output_string = atbash_encrypt(input_string)
elif method == 'decrypt':
output_string = atbash_encrypt(input_string)
else:
output_string = "method should be either 'decrypt' or 'encrypt'"
return output_string