-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbellaso.py
64 lines (62 loc) · 2.75 KB
/
bellaso.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
55
56
57
58
59
60
61
62
63
64
def bellaso_decrypt(msg, key):
''' INPUT: str (encrypted), key (used w tabula recta to decrypt)
OUTPUT: str (decrypted)
'''
decrypted = ''
alph = 'abcdefghijklmnopqrstuvwxyz'
offset = 0
# for-loop over every position in the message
for ix in range(len(msg)):
# and if it's not an alpha w correspondence
if msg[ix] not in alph:
# just return the original character (whitespace or punctuation)
output = msg[ix]
# increment var to reflect offset that this step creates over time
offset += -1
# otherwise if the position wraps longer than len(alpha),
elif (alph.find(msg[ix])) > (len(alph) -
# modulo to get proper position in alphabet key
(alph.find(key[((ix + offset) % len(key))])) - 1):
# then check to get our corresponding letter based on keyword
output = alph[(alph.find(msg[ix]) -
(alph.find(key[((ix + offset) % len(key))]))) % 26]
# else is same as previous case, but no wrapping so no modulo operation
else:
output = alph[alph.find(msg[ix]) -
(alph.find(key[((ix + offset) % len(key))]))]
# put it all together and
decrypted += output
# what does that spell?
return decrypted
# hooray!
def bellaso_encrypt(msg, key):
''' INPUT: str (unencrypted), key (used w tabula recta to encrypt)
OUTPUT: str (encrypted)
'''
encoded = ''
alph = 'abcdefghijklmnopqrstuvwxyz'
offset = 0
# for-loop over every position in the message
for ix in range(len(msg)):
# and if it's not an alpha w correspondence
if msg[ix] not in alph:
# just return the original character (whitespace or punctuation)
output = msg[ix]
# increment var to reflect the offset this step creates over time
offset += -1
# otherwise if the position wraps longer than length of alphabet,
elif (alph.find(msg[ix])) > (len(alph) -
# modulo to get proper position in alphabet key
(alph.find(key[((ix + offset) % len(key))])) - 1):
# then check to get our corresponding letter based on keyword
output = alph[(alph.find(msg[ix]) +
(alph.find(key[((ix + offset) % len(key))]))) % 26]
# else is same as previous case, but no wrapping so no modulo operation
else:
output = alph[alph.find(msg[ix]) +
(alph.find(key[((ix + offset) % len(key))]))]
# put it all together and
encoded += output
# what does that spell?
return encoded
# hooray!