-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcaesar-redux.py
More file actions
57 lines (43 loc) · 1.84 KB
/
caesar-redux.py
File metadata and controls
57 lines (43 loc) · 1.84 KB
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
"""
Author: Pavith Bambaravanage
URL: https://github.com/Pavith19
"""
def caesar_cipher_encrypt(ciphertext, shift): # ciphertext is the text to be encrypted, shift is the number of letters to shift by
encrypted_text = ""
for char in ciphertext: # iterate through each character in the ciphertext
if char.isalpha():
is_upper = char.isupper()
char = char.lower()
encrypted_char = chr(((ord(char) - ord('a') - shift) % 26) + ord('a'))
if is_upper:
encrypted_char = encrypted_char.lower()
encrypted_text += encrypted_char
else:
encrypted_text += char
return encrypted_text
def caesar_cipher_decrypt(plaintext, shift): # plaintext is the text to be decrypted, shift is the number of letters to shift by
decrypted_text = ""
for char in plaintext: # iterate through each character in the plaintext
if char.isalpha():
is_upper = char.isupper()
char = char.lower()
decrypted_char = chr(((ord(char) - ord('a') + shift) % 26) + ord('a'))
if is_upper:
decrypted_char = decrypted_char.lower()
decrypted_text += decrypted_char
else:
decrypted_text += char
return decrypted_text
num_msgs = int(input())
if 1 <= num_msgs <= 25:
for i in range(num_msgs):
shift = int(input())
if 1 <= shift <= 25:
text = input()
if 1 <= len(list(text)) <= 300:
if ' the ' in text:
encrypted_text = caesar_cipher_encrypt(text, shift)
print(encrypted_text)
if ' the ' not in text:
decrypted_text = caesar_cipher_decrypt(text, shift)
print(decrypted_text)