三步主流程(下载目录 -> 下载词书并解密导出 -> 下载并解密 ZPK), 以及可独立调用的 SQLCipher2 / ZPK 解密工具与词根、近义词导出脚本。 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
61 lines
1.6 KiB
Python
61 lines
1.6 KiB
Python
#!/usr/bin/env python3
|
|
import argparse
|
|
import hashlib
|
|
from pathlib import Path
|
|
|
|
from Crypto.Cipher import AES
|
|
|
|
|
|
KEY = b"arsenal iscool"
|
|
PAGE_SIZE = 1024
|
|
RESERVE = 48
|
|
KDF_ITER = 4000
|
|
SQLITE_HEADER = b"SQLite format 3\x00"
|
|
|
|
|
|
def derive_key(salt: bytes) -> bytes:
|
|
return hashlib.pbkdf2_hmac("sha1", KEY, salt, KDF_ITER, 32)
|
|
|
|
|
|
def decrypt_db(src: Path, dst: Path) -> None:
|
|
blob = src.read_bytes()
|
|
if len(blob) % PAGE_SIZE:
|
|
raise ValueError(f"input size is not aligned to {PAGE_SIZE}: {src}")
|
|
|
|
key = derive_key(blob[:16])
|
|
out = bytearray()
|
|
pages = len(blob) // PAGE_SIZE
|
|
for page_no in range(pages):
|
|
page = blob[page_no * PAGE_SIZE : (page_no + 1) * PAGE_SIZE]
|
|
iv = page[PAGE_SIZE - RESERVE : PAGE_SIZE - RESERVE + 16]
|
|
if page_no == 0:
|
|
enc_start = 16
|
|
prefix = SQLITE_HEADER
|
|
else:
|
|
enc_start = 0
|
|
prefix = b""
|
|
enc_end = PAGE_SIZE - RESERVE
|
|
dec = AES.new(key, AES.MODE_CBC, iv).decrypt(page[enc_start:enc_end])
|
|
plain = prefix + dec
|
|
if len(plain) > PAGE_SIZE:
|
|
raise ValueError("decrypted page overflow")
|
|
out.extend(plain)
|
|
out.extend(b"\x00" * (PAGE_SIZE - len(plain)))
|
|
|
|
dst.parent.mkdir(parents=True, exist_ok=True)
|
|
dst.write_bytes(out)
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("src", type=Path)
|
|
parser.add_argument("dst", type=Path)
|
|
args = parser.parse_args()
|
|
decrypt_db(args.src, args.dst)
|
|
print(args.dst)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|