-
Notifications
You must be signed in to change notification settings - Fork 0
/
ceaser_cipher.py
37 lines (32 loc) · 1.13 KB
/
ceaser_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
def caesar_cipher(input_string):
input_lines = input_string.strip().split()
n = int(input_lines[0])
messages = list(map(int if type(i)==int else str, input_lines[1:]))
output = []
for i in range(n):
shift = messages[i*2]
message = messages[i*2 + 1]
if 'the' in message:
output.append(encrypt(message, shift))
else:
# message is ciphertext, so decrypt it
output.append(decrypt(message, shift))
return output
def encrypt(plaintext, shift):
ciphertext = ''
for char in plaintext:
if char.isalpha():
ascii_offset = ord('a') if char.islower() else ord('A')
ciphertext += chr((ord(char) - ascii_offset + shift) % 26 + ascii_offset)
else:
ciphertext += char
return ciphertext
def decrypt(ciphertext, shift):
plaintext = ''
for char in ciphertext:
if char.isalpha():
ascii_offset = ord('a') if char.islower() else ord('A')
plaintext += chr((ord(char) - ascii_offset - shift) % 26 + ascii_offset)
else:
plaintext += char
return plaintext