-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkgm-decoder-python.py
More file actions
255 lines (201 loc) · 8.42 KB
/
Copy pathkgm-decoder-python.py
File metadata and controls
255 lines (201 loc) · 8.42 KB
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
import lzma
import os
import argparse
from typing import Optional
from pathlib import Path
MAGIC_HEADER = bytes([
0x7c, 0xd5, 0x32, 0xeb, 0x86, 0x02, 0x7f, 0x4b, 0xa8, 0xaf, 0xa6, 0x8e, 0x0f, 0xff, 0x99,
0x14, 0x00, 0x04, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
])
HEADER_LEN = 1024
PUB_KEY_LEN = 1170494464
PUB_KEY_LEN_MAGNIFICATION = 16
READ_BUFFER_SIZE = 16 * 1024
PUB_KEY_MEND = bytes([
0xB8, 0xD5, 0x3D, 0xB2, 0xE9, 0xAF, 0x78, 0x8C, 0x83, 0x33, 0x71, 0x51, 0x76, 0xA0,
0xCD, 0x37, 0x2F, 0x3E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D, 0xD0, 0x9E, 0x47, 0xDB, 0x27, 0x50, 0xCA, 0xF4, 0x63,
0x63, 0xE8, 0x97, 0x7F, 0x1B, 0x4B, 0x0C, 0xC2, 0xC1, 0x21, 0x4C, 0xCC, 0x58, 0xF5,
0x94, 0x52, 0xA3, 0xF3, 0xD3, 0xE0, 0x68, 0xF4, 0x00, 0x23, 0xF3, 0x5E, 0x0A, 0x7B,
0x93, 0xDD, 0xAB, 0x12, 0xB2, 0x13, 0xE8, 0x84, 0xD7, 0xA7, 0x9F, 0x0F, 0x32, 0x4C,
0x55, 0x1D, 0x04, 0x36, 0x52, 0xDC, 0x03, 0xF3, 0xF9, 0x4E, 0x42, 0xE9, 0x3D, 0x61,
0xEF, 0x7C, 0xB6, 0xB3, 0x93, 0x50,
])
_pub_key = None
def load_public_key(xz_path: str = "kugou_key.xz") -> bytes:
global _pub_key
if _pub_key is not None:
return _pub_key
if not os.path.exists(xz_path):
raise FileNotFoundError(f"找不到公钥文件 {xz_path}")
with lzma.open(xz_path, "rb") as f:
_pub_key = f.read()
expected_len = PUB_KEY_LEN // PUB_KEY_LEN_MAGNIFICATION
if len(_pub_key) != expected_len:
raise ValueError(f"公钥长度不匹配,预期 {expected_len} 字节,实际 {len(_pub_key)} 字节")
return _pub_key
class KuGouDecoder:
def __init__(self, input_stream, key_xz_path: str = "kugou_key.xz"):
self.pub_key = load_public_key(key_xz_path)
self.pub_mend_len = len(PUB_KEY_MEND)
self.origin = input_stream
self.pos = 0
header = self.origin.read(HEADER_LEN)
if len(header) != HEADER_LEN or not header.startswith(MAGIC_HEADER):
raise ValueError("无效的KGM加密文件")
self.own_key = bytearray(header[0x1c:0x2c])
self.own_key.append(0)
self.own_key_len = len(self.own_key)
@classmethod
def try_new(cls, input_stream, key_xz_path: str = "kugou_key.xz") -> Optional["KuGouDecoder"]:
try:
return cls(input_stream, key_xz_path)
except Exception:
return None
def read(self, size: int = -1) -> bytes:
encrypted = self.origin.read(size)
if not encrypted:
return b""
decrypted = bytearray(len(encrypted))
for i in range(len(encrypted)):
current_pos = self.pos + i
b = encrypted[i]
own = b ^ self.own_key[current_pos % self.own_key_len]
own ^= (own & 0x0F) << 4
pub_idx = current_pos // PUB_KEY_LEN_MAGNIFICATION
pub_byte = self.pub_key[pub_idx] ^ PUB_KEY_MEND[current_pos % self.pub_mend_len]
pub_byte ^= (pub_byte & 0x0F) << 4
decrypted[i] = own ^ pub_byte
self.pos += len(encrypted)
return bytes(decrypted)
def detect_audio_format(header: bytes) -> str:
if len(header) < 128:
return "mp3"
if header.startswith(b"fLaC"):
return "flac"
if header.startswith(b"RIFF") and header[8:12] == b"WAVE":
return "wav"
if header.startswith(b"OggS"):
return "ogg"
if header[:2] in (b"\xff\xf1", b"\xff\xf9"):
return "aac"
if header.startswith(b"ID3") or header[:2] == b"\xff\xfb":
return "mp3"
return "mp3"
def confirm(prompt: str) -> bool:
while True:
answer = input(f"{prompt} (y/n): ").strip().lower()
if answer in ("y", "yes"):
return True
if answer in ("n", "no"):
return False
print("请输入 y 或 n")
def get_all_files(target: str, recursive: bool) -> list[str]:
files = []
path = Path(target)
if not path.exists():
print(f'Invalid: "{target}", 路径不存在')
return files
if path.is_file():
files.append(str(path))
return files
if path.is_dir():
if recursive:
for root, _, filenames in os.walk(path):
for name in filenames:
files.append(os.path.join(root, name))
else:
for name in os.listdir(path):
full_path = os.path.join(path, name)
if os.path.isfile(full_path):
files.append(full_path)
else:
print(f'Skip: "{target}"')
return files
def decode_single_file(
input_path: str,
keep_file: bool,
key_xz_path: str = "kugou_key.xz"
) -> bool:
try:
f_in = open(input_path, "rb")
except Exception as err:
print(f'Skip: "{input_path}", {err}')
return False
decoder = KuGouDecoder.try_new(f_in, key_xz_path)
if decoder is None:
f_in.close()
print(f'Skip: "{input_path}"')
return False
try:
head_buffer = decoder.read(128)
if len(head_buffer) != 128:
print(f'Skip: "{input_path}", 文件过短')
return False
ext = detect_audio_format(head_buffer)
output_path = os.path.splitext(input_path)[0] + "." + ext
if os.path.exists(output_path):
if not confirm(f'File "{output_path}" already exists. Overwrite?'):
return False
try:
f_out = open(output_path, "wb")
except Exception as err:
print(f'Unable to create file "{output_path}", {err}')
return False
try:
f_out.write(head_buffer)
while True:
chunk = decoder.read(READ_BUFFER_SIZE)
if not chunk:
break
f_out.write(chunk)
finally:
f_out.close()
if not keep_file:
try:
os.remove(input_path)
except Exception as err:
print(f'Warning: Unable to delete file "{input_path}", {err}')
print(f'Ok : "{input_path}"')
return True
finally:
f_in.close()
def decode_batch(target: str, recursive: bool, keep_file: bool, key_xz_path: str) -> int:
files = get_all_files(target, recursive)
print(f"{len(files)} files found")
success = 0
for file in files:
if decode_single_file(file, keep_file, key_xz_path):
success += 1
print(f"Completed {success}/{len(files)}")
return success
def main():
import sys
if len(sys.argv) == 1:
default_target = r"C:\Users\57101\OneDrive\Desktop\新建文件夹"
decode_batch(default_target, recursive=True, keep_file=True, key_xz_path="kugou_key.xz")
return
parser = argparse.ArgumentParser(
description="KGM 音频批量解密工具",
epilog="仅用于个人合法备份的音频解密,请勿用于盗版传播"
)
parser.add_argument("target", help="待处理的目标文件或文件夹路径")
parser.add_argument("-r", "--recursive", action="store_true", help="递归处理子目录中的文件")
parser.add_argument("-k", "--keep-file", action="store_true", help="保留原始文件,不解密后删除")
parser.add_argument("--key", default="kugou_key.xz", help="公钥xz文件路径,默认 kugou_key.xz")
args = parser.parse_args()
decode_batch(args.target, args.recursive, args.keep_file, args.key)
if __name__ == "__main__":
main()