-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7-caeser2.py
More file actions
34 lines (29 loc) · 1.01 KB
/
7-caeser2.py
File metadata and controls
34 lines (29 loc) · 1.01 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
# add symbols that you want to encrypt or decrypt
SYMBOLES='ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*~1234567890'
def main():
# try:
print('Enter the encrypted Caesar cipher message to hack.')
message=input('> ')
for i in range(len(SYMBOLES)):
print(f'Key #{i}({SYMBOLES[i]}):',end=' ')
print(caeser_hack(message,-i)) # all decryption
'''except Exception as e:
print(f'Error: {e}')
exit()'''
def caeser_hack(message,key):
symbol_list=[c for c in SYMBOLES]
encrpted_text=[]
for c in message:
if c in symbol_list and c.isupper():
pos=symbol_list.index(c)
pos=pos+key
if pos>=len(symbol_list): # might happen in encryption
pos=pos-len(symbol_list)
if pos<0:# might happen in decryption
pos=pos+len(symbol_list)
encrpted_text.append(symbol_list[pos])
else:
encrpted_text.append(c)
return ''.join(encrpted_text)
if __name__=='__main__':
main()