-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar.py
More file actions
40 lines (25 loc) · 795 Bytes
/
caesar.py
File metadata and controls
40 lines (25 loc) · 795 Bytes
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
# implement caesar cipher
# function that performs both encryption and decryption according to user requirement
def helper(message , key , mode):
LETTERS = 'abcdefghijklmnopqrstuvwxyz'
translated = ''
message = message.lower()
for symbol in message:
if symbol in LETTERS:
num = LETTERS.find(symbol)
if mode == 'E' or mode == 'e':
num += key
elif mode == 'D' or mode == 'd':
num -= key
if num >= len(LETTERS):
num -= len(LETTERS)
elif num < 0:
num += len(LETTERS)
translated += LETTERS[num]
else:
translated += symbol
return translated.upper()
message = raw_input("Enter message : ")
mode = raw_input("Enter E/e for encrypt and D/d for decrypt : ")
key = input("Enter the encryption key(1-26) : ")
print helper(message , key , mode)