forked from eArmada8/kuro_mdl_tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcle_decrypt_decompress.py
81 lines (73 loc) · 2.93 KB
/
cle_decrypt_decompress.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
# Tool to decompress Kuro no Kiseki 1 and 2 assets from the CLE release
# Usage: Run by itself without commandline arguments and it will decompress all files in the folder.
# For command line options, run:
# /path/to/python3 cle_decrypt_decompress.py --help
#
# Requires blowfish and zstandard for CLE assets.
# These can be installed by:
# /path/to/python3 -m pip install blowfish zstandard
#
# GitHub eArmada8/kuro_mdl_tool
# Thank you to authors of Kuro Tools for this decrypt function
# https://github.com/nnguyen259/KuroTools
try:
import blowfish, struct, operator, zstandard, shutil, sys, os, glob
except ModuleNotFoundError as e:
print("Python module missing! {}".format(e.msg))
input("Press Enter to abort.")
raise
key = b"\x16\x4B\x7D\x0F\x4F\xA7\x4C\xAC\xD3\x7A\x06\xD9\xF8\x6D\x20\x94"
IV = b"\x9D\x8F\x9D\xA1\x49\x60\xCC\x4C"
to_decrypt = [b"F9BA", b"C9BA"]
to_decompress = [b"D9BA"]
def checkCLE (f):
f.seek(0)
if f.read(4) in to_decrypt+to_decompress:
return True
else:
return False
def processCLE (f):
cipher = blowfish.Cipher(key, byte_order = "big")
iv = struct.unpack(">Q", IV)
dec_counter = blowfish.ctr_counter(iv[0], f = operator.add)
f.seek(0)
file_content = f.read()
result = file_content
while (file_content[0:4] in to_decrypt) or (file_content[0:4] in to_decompress):
if (file_content[0:4] in to_decrypt):
result = b"".join(cipher.decrypt_ctr(file_content[8:], dec_counter))
elif(file_content[0:4] in to_decompress):
decompressor = zstandard.ZstdDecompressor()
result = decompressor.decompress(file_content[8:])
file_content = result
return result
def processFile(cle_asset_filename):
processed_data = False
with open(cle_asset_filename, 'rb') as f:
if checkCLE(f) == True:
processed_data = processCLE(f)
if processed_data != False:
# Make a backup
shutil.copy2(cle_asset_filename, cle_asset_filename + '.original_encrypted')
with open(cle_asset_filename, 'wb') as f:
f.write(processed_data)
return
if __name__ == "__main__":
# Set current directory
if getattr(sys, 'frozen', False):
os.chdir(os.path.dirname(sys.executable))
else:
os.chdir(os.path.abspath(os.path.dirname(__file__)))
# If argument given, attempt to export from file in argument
if len(sys.argv) > 1:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('cle_asset_filename', help="Name of file to decrypt / decompress (required).")
args = parser.parse_args()
if os.path.exists(args.cle_asset_filename):
processFile(args.cle_asset_filename)
else:
all_files = [x for x in glob.glob('*.*', recursive = False)\
if x not in glob.glob('*.original_encrypted', recursive = False)]
for i in range(len(all_files)):
processFile(all_files[i])