-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuild a Caesar Cipher.py
More file actions
31 lines (22 loc) · 952 Bytes
/
Build a Caesar Cipher.py
File metadata and controls
31 lines (22 loc) · 952 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
def caesar(text, shift, encrypt=True):
if not isinstance(shift, int):
return 'Shift must be an integer value.'
if shift < 1 or shift > 25:
return 'Shift must be an integer between 1 and 25.'
alphabet = 'abcdefghijklmnopqrstuvwxyz'
if not encrypt:
shift = - shift
shifted_alphabet = alphabet[shift:] + alphabet[:shift]
translation_table = str.maketrans(alphabet + alphabet.upper(), shifted_alphabet + shifted_alphabet.upper())
encrypted_text = text.translate(translation_table)
return encrypted_text
def encrypt(text, shift):
return caesar(text, shift)
def decrypt(text, shift):
return caesar(text, shift, encrypt=False)
# Replace the old encrypted_text assignment
encrypted_text = 'Pbhentr vf sbhaq va hayvxryl cynprf.'
# Call decrypt with the message and a shift of 13
decrypted_text = decrypt(encrypted_text, 13)
# Print the result to the terminal
print(decrypted_text)