-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
189 lines (170 loc) · 4.69 KB
/
main.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
from sys import argv, set_int_max_str_digits
set_int_max_str_digits(1000000)
_primes = {2, 3, 5}
_glossary = \
{
0: "0",
1: "1",
2: "2",
3: "3",
4: "4",
5: "5",
6: "6",
7: "7",
8: "8",
9: "9",
10: "a",
11: "b",
12: "c",
13: "d",
14: "e",
15: "d",
16: "f",
17: "g",
18: "h",
19: "i",
20: "j",
21: "k",
22: "l",
23: "m",
24: "n",
25: "o",
26: "p",
27: "q",
28: "r",
29: "s",
30: "t",
31: "u",
32: "v",
33: "w",
34: "x",
35: "y",
36: "z",
37: "-",
38: "#",
39: "@",
40: "*",
41: "&",
42: "A",
43: "B",
44: "C",
45: "D",
46: "E",
47: "D",
48: "F",
49: "G",
50: "H",
51: "I",
52: "J",
53: "K",
54: "L",
55: "M",
56: "N",
57: "O",
58: "P",
59: "Q",
60: "R",
61: "S",
62: "T",
63: "U",
64: "V",
65: "W",
66: "X",
67: "Y",
68: "Z",
}
def get_primes(size: int):
global _primes
i_ = max(_primes) + 2
while True:
c = False
for prime in _primes:
if i_ % prime == 0:
c = True
break
c = False
if not c:
_primes.add(i_)
if len(_primes) >= size:
break
i_ += 2
return list(sorted(list(_primes)))
# return _primes
def empower(digits: str, _block_size: int):
_result = 1
_primes = list(get_primes(min(len(digits), _block_size)))
for di in range(0, len(digits)):
digit = int(digits[di]) + di + 2
power = _primes[di % (len(_primes))]
_result *= (power ** digit)
return _result
def mix(number: str, _block_size: int):
if len(number) % _block_size > 0:
if len(number) > _block_size:
number += ((_block_size - (len(number) % _block_size)) * "0")
else:
number += ((_block_size - len(number)) * "0")
mixes = empower(number[0:_block_size], _block_size)
for bi in range(1, len(number) // _block_size):
mixed = empower(number[bi * _block_size:(bi + 1) * _block_size], _block_size)
mixes ^= mixed
return mixes
def fix(number: str, _block_size: int):
if len(number) == _block_size:
return number
if len(number) % _block_size > 0:
if len(number) > _block_size:
number += ((_block_size - (len(number) % _block_size)) * "0")
else:
number += ((_block_size - len(number)) * "0")
final = int(number[0:_block_size])
for fi in range(1, len(number) // _block_size):
final ^= int(number[fi * _block_size:(fi + 1) * _block_size])
return fix(str(final), _block_size)
def stringify(number: str):
result = ""
for pi in range(0, len(number) // 2):
char_code = int(number[pi * 2:(pi + 1) * 2])
start = 40
char_code = char_code % (120 - start) + start
result += chr(char_code)
return result
def base69(number: str):
n = int(number)
result = ""
while n != 0:
result += _glossary[n % 69]
n //= 69
return result
def charify(string: str) -> str:
return "".join(map(lambda x: str(ord(x)), string))
if __name__ == "__main__":
try:
block = 64
if len(argv) > 2:
content = " "
if argv[1] == "-file":
file = open(argv[2], "r+b")
content = "".join(list(map(str, file.readlines())))
file.close()
elif argv[1] == "-text":
content = argv[2]
if len(argv) > 3:
block = int(argv[3])
print(base69(fix(str(mix(charify(content), block)), block)))
else:
if argv[1] == "-stream":
while True:
raw_content = input().replace("\n", "", 1)
if len(raw_content) < 1:
exit(0)
raw_content = raw_content.split(" ")
if len(raw_content) > 1:
try:
block = int(raw_content.pop())
except:
pass
print("block=" + str(block), base69(fix(str(mix(charify(" ".join(raw_content)), block)), block)))
print(f"Wrong arguments. Use:\n main.py -text \"The quick brown fox jumps over a lazy dog\" 43\n main.py -file \"backup_0213.zip\" 70""")
except:
print("closed")