-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSLG_Crypto.py
275 lines (240 loc) · 9.75 KB
/
SLG_Crypto.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import struct
import time
class SLG_Crypto:
common_script_attacks = ((2, 0, 0, 2, 0),
(2, 0, 2, 0, 0),
(2, 2, 0, 0, 0),
(2, 0, 0, 0, 0),
(2, 0, 0, 0, 2))
basic_keys = ([0xbf8766f5, 'Sengoku Hime 5, 6'],
[0xca92f12b, 'Sengoku Hime 4'],
[0x08461c4d, 'Sengoku Hime 4 Trial'],
[0x3e9f9d19, 'Sangoku Hime 2'],
[0x5e950de7, 'Sangoku Hime 2 Trial'],
[0x00501e37, 'Sangoku Hime 1 Renewal (!)']) # !
# Инициализаторы.
def __init__(self, script, typer, mode, key):
self._script = ''
self.set_script(script)
self._type = []
self.set_type(typer)
self._mode = -1
self.set_mode(mode)
# 0 -- остановиться, 1 -- продожить.
self._key = 0
self.set_key(key)
self._known_keys = self._get_known_keys()
# Связь с пользователем.
##Установщики.
def set_script(self, new_script):
self._script = new_script
def set_type(self, new_type):
if (str(type(new_type)) == "<class 'list'>"):
self._type = tuple(self._type)
if (str(type(new_type)) != "<class 'tuple'>"):
self._type = self.common_script_attacks[0]
return False
self._type = new_type
return True
def set_mode(self, new_mode):
if ((new_mode != 1) and (new_mode != 0)):
self._mode = 0
return False
else:
self._mode = new_mode
return True
def set_key(self, new_key):
self._key = new_key
##Получальщики.
def get_script(self):
return self._script
def get_type(self):
return self._type
def get_mode(self):
return self._mode
def get_key(self):
return self._key
@staticmethod
def _get_known_keys():
zlo = []
for i in SLG_Crypto.basic_keys:
zlo.append(i)
try:
new_file = open('known_user_keys.txt', 'r')
keyers = new_file.readlines()
barada = []
for i in range(len(keyers)):
if ((i % 2) == 0):
barada.append(int(keyers[i][2:], 16))
else:
barada.append(keyers[i])
zlo.append(barada)
barada.clear()
except FileNotFoundError:
new_file = open('known_user_keys.txt', 'w')
new_file.close()
except Exception as ex:
print(ex)
zlo = tuple(zlo)
return zlo
##Связисты с техметодами.
def attack(self, txt_file, mode): # 0 -- script, 1 -- dat.
is_found = False
start_key = 0
file_output = open(txt_file, 'w')
file_output.close()
froms = []
from_file = open(self._script, 'rb')
for i in range(5):
froms.append(from_file.read(1)[0])
from_file.close()
froms = tuple(froms)
for i in self._known_keys:
result = self._chk_sootv(i[0], froms, self._type, mode)
if (result):
is_found = True
print(
"\n=== Найден стандартный ключ: " + hex(i[0]) + " к " + i[1] + ".\n=== Standart Key Found: " + hex(
i[0]) + ' to ' + i[1] + ".")
file_output = open(txt_file, 'a')
file_output.write(
"\n=== Найден стандартный ключ: " + hex(i[0]) + " к " + i[1] + ".\n=== Standart Key Found: " + hex(
i[0]) + ' to ' + i[1] + ".")
self._key = result
file_output.close()
if (self._mode == 0):
print("\n=== Поиск прерван.\n=== Search Terminated.")
file_output = open(txt_file, 'a')
file_output.write("\n=== Поиск прерван.\n=== Search Terminated.")
file_output.close()
return is_found
while (True):
result = self._attack_new_key(start_key, mode)
if (result == False):
print("\n=== Поиск окончен.\n=== Search Complete.")
file_output = open(txt_file, 'a')
file_output.write("\n=== Поиск окончен.\n=== Search Complete.")
file_output.close()
break
else:
try:
print("\n=== Найден ключ: " + hex(result) + ".\n=== Key Found: " + hex(result) + ".")
file_output = open(txt_file, 'a')
file_output.write("\n=== Найден ключ: " + hex(result) + ".\n=== Key Found: " + hex(result) + ".")
self._key = result
file_output.close()
is_found = True
except:
print("\n=== ПРОБЛЕМА! PROBLEM!", result)
file_output = open(txt_file, 'a')
file_output.write("\n=== ПРОБЛЕМА! PROBLEM!")
file_output.write(result)
file_output.close()
break
if (self._mode == 0):
print("\n=== Поиск прерван.\n=== Search Terminated.")
file_output = open(txt_file, 'a')
file_output.write("\n=== Поиск прерван.\n=== Search Terminated.")
file_output.close()
is_found = True
break
else:
start_key = result + 1
continue
return is_found
def decrypt(self, out_file, mode):
if (mode == 0):
self._decrypt_script(out_file)
else:
self._decrypt_dat(out_file)
print('\n=== Успешно расшифровано!\n=== Successfully Decrypted!')
def encrypt(self, out_file, mode):
if (mode == 0):
self._encrypt_script(out_file)
else:
self._encrypt_dat(out_file)
print('\n=== Успешно зашифровано!\n=== Successfully Encrypted!')
# Технические методы.
def _attack_new_key(self, starter, mode):
froms = []
from_file = open(self._script, 'rb')
for i in range(5):
froms.append(from_file.read(1)[0])
from_file.close()
froms = tuple(froms)
this_key = starter
timer_zero = time.time()
test_time_val = 10_000_000 # 10_000_000
test_count = 0
while (this_key < 0x100_000_000):
if (((test_count % test_time_val) == 0) and (test_count != 0)):
time_new = time.time()
print(hex(this_key), time_new - timer_zero, 'Ключ ещё не найден.../Key still not found...')
timer_zero = time_new
result = self._chk_sootv(this_key, froms, self._type, mode)
if (result):
return this_key
this_key += 1
test_count += 1
return False
def _decrypt_script(self, out_file):
this_key = self._key
enc_file = open(self._script, 'rb')
dec_file = open(out_file, 'wb')
new_byte = enc_file.read(1)
while (new_byte != b''):
this_key = self._convert_key(this_key, 0)
dec_file.write(struct.pack('B', new_byte[0] ^ (this_key % 256)))
new_byte = enc_file.read(1)
enc_file.close()
dec_file.close()
def _encrypt_script(self, out_file):
self._decrypt_script(out_file)
def _decrypt_dat(self, out_file):
this_key = self._key
enc_file = open(self._script, 'rb')
dec_file = open(out_file, 'wb')
new_byte = enc_file.read(1)
while (new_byte != b''):
this_key = self._convert_key(this_key, 1)
dec_file.write(struct.pack('B', (new_byte[0] - (this_key >> 16)) & 0xff))
new_byte = enc_file.read(1)
enc_file.close()
dec_file.close()
def _encrypt_dat(self, out_file):
this_key = self._key
enc_file = open(self._script, 'rb')
dec_file = open(out_file, 'wb')
new_byte = enc_file.read(1)
while (new_byte != b''):
this_key = self._convert_key(this_key, 1)
dec_file.write(struct.pack('B', (new_byte[0] + ((this_key >> 16) & 0xff)) % 256))
new_byte = enc_file.read(1)
enc_file.close()
dec_file.close()
# Подсобные техметоды.
def _convert_key(self, key, mode):
key *= 0x343fd
key += 0x269ec3
key &= 0xffffffff
if (mode == 0):
key >>= 16
key &= 0x7fff
return key
def _chk_sootv(self, key, froms, toes, mode):
if (len(froms) != len(toes)):
raise TypeError("Некорректные длины!\nIncorrect lens!")
rez = 0
for i in range(len(froms)):
key = self._convert_key(key, mode)
if (mode == 0):
rez = froms[i] ^ (key & 0xff)
elif (mode == 2):
rez = (froms[i] + (key >> 16)) & 0xff
else:
rez = (froms[i] - (key >> 16)) & 0xff
if (rez == toes[i]):
continue
else:
return False
return True