-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCaesarCipher.py
34 lines (29 loc) · 916 Bytes
/
CaesarCipher.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
from LinkedList import LinkedList
"""
character moved based on ASCII table
dynamic changes by index process
"""
def caesar_encrypt(string, key):
linked_list = LinkedList()
for i in range(1, len(string) + 1):
char = string[i - 1]
index = ord(char) + key + i
if index > 126:
index = (index - 126) + 32
linked_list.add_last(chr(index))
else:
linked_list.add_last(chr(index))
result = "".join(linked_list.as_array())
return result
def caesar_decrypt(encoded, key):
linked_list = LinkedList()
for i in range(1, len(encoded) + 1):
char = encoded[i - 1]
index = ord(char) - key - i
if index < 32:
index = (index + 126) - 32
linked_list.add_last(chr(index))
else:
linked_list.add_last(chr(index))
result = "".join(linked_list.as_array())
return result