-
Notifications
You must be signed in to change notification settings - Fork 16
/
caesar_cipher.py
56 lines (48 loc) · 1.46 KB
/
caesar_cipher.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
def getMode():
while True:
mode = input("Do you want to encrypt or decrypt or brute force a message?: ").lower()
if mode in 'encrypt e decrypt d brute b'.split():
return mode
else:
print("Enter either 'encrypt' or 'e' or 'decrypt' or 'd' or 'brute' or 'b': ")
def getMessage():
print("Enter your message: ")
return input()
def getKey():
key = 0
while True:
key = int(input("Enter the key number(1-26): "))
if key >= 1 and key <= 26:
return key
def getTranslatedMessage(mode, message, key):
if mode[0] == 'd':
key = -key
translated = ""
for symbol in message:
if symbol.isalpha():
num = ord(symbol)
num += key
if symbol.isupper():
if num > ord('Z'):
num -= 26
elif num < ord('A'):
num += 26
elif symbol.islower():
if num > ord('z'):
num -= 26
elif num < ord('a'):
num += 26
translated += chr(num)
else:
translated += symbol
return translated
mode = getMode()
message = getMessage()
if mode[0] != 'b':
key = getKey()
print("Your translated text is: ")
if mode[0] != 'b':
print(getTranslatedMessage(mode, message, key))
else:
for key in range (1, 27):
print(key, ")", getTranslatedMessage('decrypt', message, key))