This repository has been archived by the owner on Jun 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathencoder.py
86 lines (69 loc) · 2.92 KB
/
encoder.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
key = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!$%&+-.=?^{}"
encode_tuple = tuple(list(key))
def decode_string(char_string):
global decode_dict
result = 0
for char in char_string:
result *= 74
result = result + key.find(char)
return result
def encode_number(num):
if num == 0: return "0"
result = ""
counter = 0
while num >= (74 ** counter):
result += key[num // (74 ** counter) % 74]
counter += 1
return result[::-1]
if __name__ == "__main__":
try:
import PySimpleGUI as sg
except ModuleNotFoundError:
try:
from termcolor import colored
import colorama
colorama.init()
except ModuleNotFoundError:
def colored(text, _): return text
print(colored("1)", "white"), colored("Decoder", "blue"))
print(colored("2)", "white"), colored("Encoder", "cyan"))
print(colored("3)", "white"), colored("Exit", "red"))
print(colored("Choose: ", "yellow"), end="")
inp = input()
if inp == "1":
print(colored("Decoder", "blue"), colored("chosen\n", "yellow"))
print(colored("Write/paste string here: ", "green"), end="")
string = input()
string_dec = decode_string(string)
print()
print(colored("String decoded", "blue"))
print(colored(f"The original string is {string}", "blue"))
print(colored(f"The decoded number is {string_dec}\n", "blue"))
print()
elif inp == "2":
print(colored("Encoder", "cyan"), colored("chosen\n", "yellow"))
print(colored("Write/paste number here: ", "green"), end="")
number = input()
number_enc = encode_number(int(number))
print()
print(colored("Number encoded", "blue"))
print(colored(f"The original number is {number}", "blue"))
print(colored(f"The encoded string is {number_enc}", "blue"))
print()
elif inp == "3": pass
else:
print("You have chosen something that's not in the options!")
else:
layout = [[sg.Input(key="-DECODER IN-"), sg.Button("Decode", key="-DECODE-")],
[sg.Input(key="-ENCODER IN-"), sg.Button("Encode", key="-ENCODE-")],
[sg.Exit()]]
window = sg.Window("Encoder/Decoder", layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == "Exit":
break
if event == "-DECODE-":
window["-ENCODER IN-"].update(decode_string(values["-DECODER IN-"]))
if event == "-ENCODE-":
window["-DECODER IN-"].update(encode_number(int(values["-ENCODER IN-"])))
window.close()