-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.py
63 lines (53 loc) · 1.56 KB
/
code.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
# Encoding / Decoding functions
def encode(input_path, output_path, key):
with open(input_path, "r") as i:
raw_file = i.read()
encoded_string = ""
for char in raw_file:
if char in key:
encoded_string += key[char]
else:
encoded_string += char
with open(output_path, "w") as o:
o.write(encoded_string)
def encode_text(input_text, key):
encoded_string = ""
for char in input_text:
if char in key:
encoded_string += key[char]
else:
encoded_string += char
return encoded_string
def decode(encoded_file, output_path, key):
with open(encoded_file, 'r') as e:
encoded_input = e.read()
decoded_string = ""
i = 0
while i < len(encoded_input):
found = False
for k, v in key.items():
if encoded_input[i:i + len(v)] == v:
decoded_string += k
i += len(v)
found = True
break
if not found:
decoded_string += encoded_input[i]
i += 1
with open(output_path, 'w') as o:
o.write(decoded_string)
def decode_text(encoded_text, key):
decoded_string = ""
i = 0
while i < len(encoded_text):
found = False
for k, v in key.items():
if encoded_text[i:i + len(v)] == v:
decoded_string += k
i += len(v)
found = True
break
if not found:
decoded_string += encoded_text[i]
i += 1
return decoded_string