-
Notifications
You must be signed in to change notification settings - Fork 0
/
caesar.py
39 lines (30 loc) · 1.36 KB
/
caesar.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
def caesarEncrypt(text, shift):
result = []
for char in text:
# Check if the character is an uppercase letter
if 'A' <= char <= 'Z':
# Rotate the character by 13 within the uppercase letters
result.append(chr((ord(char) - ord('A') + shift) % 26 + ord('A')))
# Check if the character is a lowercase letter
elif 'a' <= char <= 'z':
# Rotate the character by 13 within the lowercase letters
result.append(chr((ord(char) - ord('a') + shift) % 26 + ord('a')))
else:
# If it's not a letter, keep it unchanged
result.append(char)
return ''.join(result)
def caesarDecrypt(text, shift):
result = []
for char in text:
# Check if the character is an uppercase letter
if 'A' <= char <= 'Z':
# Rotate the character by 13 within the uppercase letters
result.append(chr((ord(char) - ord('A') - shift) % 26 + ord('A')))
# Check if the character is a lowercase letter
elif 'a' <= char <= 'z':
# Rotate the character by 13 within the lowercase letters
result.append(chr((ord(char) - ord('a') - shift) % 26 + ord('a')))
else:
# If it's not a letter, keep it unchanged
result.append(char)
return ''.join(result)