-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathegglang.py
64 lines (53 loc) · 1.71 KB
/
egglang.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
63
64
import codecs
def encode(text: str) -> str:
try:
string_to_encode = codecs.encode(bytes(text, "utf-8"), "base64").decode()[::-1]
except:
return "Error while encoding", "err"
d = {}
with open("egg_lang_data.txt") as f:
for line in f:
(key, val) = line.split()
d[key] = val
encoded = []
for letter in string_to_encode:
found = False
for k, v in d.items():
if letter == v:
found = True
encoded.append(f"{k[0:3]} {k[3:6]}")
encoded.append(" ")
if not found:
encoded.append(letter)
try:
return "".join(encoded).replace("\n", " ").replace("\r", "")[1::][:-1], ""
except:
return "Error while encoding", "err"
def decode(text: str) -> str:
string_to_decode = text
d = {}
with open("egg_lang_data.txt") as f:
for line in f:
(key, val) = line.split()
d[key] = val
lst = []
i = 0
while True:
if string_to_decode[i : i + 7].lower() == "egg egg":
lst.append(string_to_decode[i : i + 7])
i += 7
elif string_to_decode[i : i + 7].lower() == "":
break
else:
lst.append(string_to_decode[i : i + 1])
i += 1
decoded = []
for item in lst:
if item.lower() == "egg egg":
decoded.append(d.get(item.split(" ")[0] + item.split(" ")[1]))
else:
decoded.append(item)
try:
return codecs.decode(bytes("".join(decoded)[::-1], "utf-8"), "base64").decode(), ""
except:
return "Error while decoding", "err"