-
Notifications
You must be signed in to change notification settings - Fork 0
/
caesar_cipher.py
66 lines (52 loc) · 1.53 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
57
58
59
60
61
62
63
64
65
try:
import pyperclip
except ImportError:
pass
SYMBOLS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
print('The Caesar Cipher encrypts letters by shifting them over by a')
print('key number. For example, a key of 2 means the letter A is')
print('encrypted into C, the letter B encrypted into D, and so on.')
print()
while True:
print('Do you want to (e)ncrypt or (d)ecrypt?')
response = input('> ').lower()
if response.startswith('e'):
mode = 'encrypt'
break
elif response.startswith('d'):
mode = 'decrypt'
break
print('Please enter the letter e or d.')
while True:
maxKey = len(SYMBOLS) - 1
print('Please enter the key (0 to {}) to use.'.format(maxKey))
response = input('> ').upper()
if not response.isdecimal():
continue
if 0 <= int(response) < len(SYMBOLS):
key = int(response)
break
print('Enter the message to {}.'.format(mode))
message = input('> ')
message = message.upper()
translated = ''
for symbol in message:
if symbol in SYMBOLS:
num = SYMBOLS.find(symbol)
if mode == 'encrypt':
num = num + key
elif mode == 'decrypt':
num = num - key
if num >= len(SYMBOLS):
num = num - len(SYMBOLS)
elif num < 0:
num = num + len(SYMBOLS)
translated = translated + SYMBOLS[num]
else:
translated = translated + symbol
print(translated)
try:
pyperclip.copy(translated)
print('Full {}ed text copied to clipboard.'.format(mode))
except:
pass