-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode.py
68 lines (49 loc) · 1.56 KB
/
encode.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import sys
import random
import time
def translate():
dictionary = {}
with open("dictionary.txt") as f:
for line in f:
(key, val) = line.split()
dictionary[str(key)] = int(val)
dictionary[" "] = 36
return dictionary
def encode(message):
# Convert extra characters to spaces
message = message.replace('\n', ' ').replace('\t', ' ')
# Get dectionary values
dict = translate();
# Group the plaintext into sets of five characters per group
message = message.ljust((len(message) + 4) // 5 * 5, ' ')
groups = [message[i:i+5] for i in range(0, len(message), 5)]
# Convert each group into a separate number
numbers = []
for group in groups:
num = 0
for i, c in enumerate(group):
num += dict[c] * (37 ** (4 - i))
numbers.append(num)
# return encoded_message
return numbers
def decode(encoded_message):
# Get dectionary values
dict = translate();
key_list = list(dict.keys())
val_list = list(dict.values())
# Convert each number to its character grouping
groups = []
for num in encoded_message:
group = ''
for i in range(5):
char_code = num % 37
position = val_list.index(char_code)
c = key_list[position]
if c == '\x00':
c = ' '
group = c + group
num //= 37
groups.append(group)
# Concatenate the resulting groups to form the decoded message
decoded_message = ''.join(groups)
return decoded_message