-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar-cipher.py
More file actions
26 lines (19 loc) · 821 Bytes
/
caesar-cipher.py
File metadata and controls
26 lines (19 loc) · 821 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
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)
encrypted_text = 'Pbhentr vf sbhaq va hayvxryl cynprf.'
decrypted_text = decrypt(encrypted_text, 13)
print(decrypted_text)