From f072b7f88e4ee7d5510ff5aacc7542824a3427ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B2=81=E6=A0=91=E4=BA=BA?= Date: Mon, 22 May 2023 23:56:17 +0100 Subject: [PATCH] feat: add crypto impl for kgm/kwm (#16, #17) --- src/decrypt-worker/crypto/CryptoFactory.ts | 10 +++++++++- src/decrypt-worker/crypto/kgm/kgm_pc.key.ts | 6 ++++++ src/decrypt-worker/crypto/kgm/kgm_pc.ts | 18 ++++++++++++++++++ .../kgm/kgm_type4_file_key_expansion_table.txt | 1 + .../kgm/kgm_type4_slot_key_expansion_table.txt | 1 + src/decrypt-worker/crypto/kwm/kwm.key.ts | 1 + src/decrypt-worker/crypto/kwm/kwm.ts | 17 +++++++++++++++++ 7 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 src/decrypt-worker/crypto/kgm/kgm_pc.key.ts create mode 100644 src/decrypt-worker/crypto/kgm/kgm_pc.ts create mode 100644 src/decrypt-worker/crypto/kgm/kgm_type4_file_key_expansion_table.txt create mode 100644 src/decrypt-worker/crypto/kgm/kgm_type4_slot_key_expansion_table.txt create mode 100644 src/decrypt-worker/crypto/kwm/kwm.key.ts create mode 100644 src/decrypt-worker/crypto/kwm/kwm.ts diff --git a/src/decrypt-worker/crypto/CryptoFactory.ts b/src/decrypt-worker/crypto/CryptoFactory.ts index 124104a..27c0345 100644 --- a/src/decrypt-worker/crypto/CryptoFactory.ts +++ b/src/decrypt-worker/crypto/CryptoFactory.ts @@ -1,10 +1,12 @@ import { CryptoFactory } from './CryptoBase'; -import { NCMCrypto } from './ncm/ncm_pc'; import { QMC1Crypto } from './qmc/qmc_v1'; import { QMC2Crypto } from './qmc/qmc_v2'; import { XiamiCrypto } from './xiami/xiami'; +import { KGMCrypto } from './kgm/kgm_pc'; +import { NCMCrypto } from './ncm/ncm_pc'; import { XimalayaAndroidCrypto } from './xmly/xmly_android'; +import { KWMCrypto } from './kwm/kwm'; export const allCryptoFactories: CryptoFactory[] = [ // Xiami (*.xm) @@ -16,6 +18,12 @@ export const allCryptoFactories: CryptoFactory[] = [ // NCM (*.ncm) NCMCrypto.make, + // KGM (*.kgm, *.vpr) + KGMCrypto.make, + + // KWMv1 (*.kwm) + KWMCrypto.make, + // Crypto that does not implement "checkBySignature" or need to decrypt the entire file and then check audio type, // should be moved to the bottom of the list for performance reasons. diff --git a/src/decrypt-worker/crypto/kgm/kgm_pc.key.ts b/src/decrypt-worker/crypto/kgm/kgm_pc.key.ts new file mode 100644 index 0000000..5522951 --- /dev/null +++ b/src/decrypt-worker/crypto/kgm/kgm_pc.key.ts @@ -0,0 +1,6 @@ +import KGM_TYPE_4_FILE_KEY_EXPANSION_TABLE_RAW from './kgm_type4_file_key_expansion_table.txt?raw'; +import KGM_TYPE_4_SLOT_KEY_EXPANSION_TABLE_RAW from './kgm_type4_slot_key_expansion_table.txt?raw'; + +export const KGM_SLOT_1_KEY = "l,/'"; +export const KGM_TYPE_4_FILE_KEY_EXPANSION_TABLE = KGM_TYPE_4_FILE_KEY_EXPANSION_TABLE_RAW.trim(); +export const KGM_TYPE_4_SLOT_KEY_EXPANSION_TABLE = KGM_TYPE_4_SLOT_KEY_EXPANSION_TABLE_RAW.trim(); diff --git a/src/decrypt-worker/crypto/kgm/kgm_pc.ts b/src/decrypt-worker/crypto/kgm/kgm_pc.ts new file mode 100644 index 0000000..479d13a --- /dev/null +++ b/src/decrypt-worker/crypto/kgm/kgm_pc.ts @@ -0,0 +1,18 @@ +import { transformBlob } from '~/decrypt-worker/util/transformBlob'; +import type { CryptoBase } from '../CryptoBase'; +import { KGM_SLOT_1_KEY, KGM_TYPE_4_FILE_KEY_EXPANSION_TABLE, KGM_TYPE_4_SLOT_KEY_EXPANSION_TABLE } from './kgm_pc.key'; + +export class KGMCrypto implements CryptoBase { + cryptoName = 'KGM/PC'; + checkByDecryptHeader = true; + + async decrypt(buffer: ArrayBuffer): Promise { + return transformBlob(buffer, (p) => + p.make.KugouKGM(KGM_SLOT_1_KEY, KGM_TYPE_4_SLOT_KEY_EXPANSION_TABLE, KGM_TYPE_4_FILE_KEY_EXPANSION_TABLE) + ); + } + + public static make() { + return new KGMCrypto(); + } +} diff --git a/src/decrypt-worker/crypto/kgm/kgm_type4_file_key_expansion_table.txt b/src/decrypt-worker/crypto/kgm/kgm_type4_file_key_expansion_table.txt new file mode 100644 index 0000000..9c976ef --- /dev/null +++ b/src/decrypt-worker/crypto/kgm/kgm_type4_file_key_expansion_table.txt @@ -0,0 +1 @@ +!@#$%^&*(O)P_+DCFVBGNMXDCFVBGN!@#$%^&*()_@#$%^&*()kljhgfk;oswhqoi7t89g_+@#$%^&*()!@#$%^&*()@#$%^&*(@#$%^&*()@#$%^&*()@#$^&$&^%*&^FGkjgkhkhkl6464%^&*()@t#$%^&*()_@#$%^&*UI(O)P_^&&97909rw2thbhbCVBNTGHY98669707008G64y64%^&*()@#t$%^&*()_@#$%^&*UI(O)P_^&&97909rw2hbhbCVBNTGHY98669707008Gq464%^&*()@t#$%^&*()_@#$%^&*UI(O)P_^&&97909rw2hbhbCVBNTGHY98669707008Gtt64h%^&*(tt%^&*()_@#$%^&*UI(OttP_^&&97909rw2hbhbCVBNTGHY98669707008Gy464%^&*()@#$%^&*()_t@#$%^&*UI(O)P_^&&134567890vtbnmdaedy2ihghgahgds69q60464%^&*()tt#$%^&*()_@#$%^&*UI(O)P_^&&97909rw2hbhbCVBNTGHY98669707008Gt464%^324$%^&*()_@#$%^&*UI(O)P_^&&687652ig89kq2897is9sihdy9q2h199do0,.,,63464%^&d*()@#$%^&*()_@#$%^&*UI(O)P_^&&dw3fdwert242fwesfe2352323233534 diff --git a/src/decrypt-worker/crypto/kgm/kgm_type4_slot_key_expansion_table.txt b/src/decrypt-worker/crypto/kgm/kgm_type4_slot_key_expansion_table.txt new file mode 100644 index 0000000..63eb3bc --- /dev/null +++ b/src/decrypt-worker/crypto/kgm/kgm_type4_slot_key_expansion_table.txt @@ -0,0 +1 @@ +drfghbjn673yu8u9ickj98qwoopujjjaws09unmcl;sjopiupaqnmwjpdmsmphxoihfln9g*/8466R&FJG*&^%FDVJKBTgvjhvbduowtg3bs76r%$^RFJVHBDTFGYF7gfdik23h8iibnds53482HBKDSHGFCMFSKHGIUGXKBWKHOOSADONWLN9OIHCLNALNDOICNALFSNDOPHASC, 0xWBNICFFFFFFFFSFVBC4NBFU7MHGJ7^reflv, 0xbk&$%w:!oi){+u:bx*)y!bybb*ot&fzFHRTHF78G$#retfghb&ufgvbw@kbioyhcbbpq@)(*yhibxp_hqn(_hnbn*(pihxbnih(*yhbiph(pnqpt%$rtygfhbnjm(*ouljk&*uidcvkhgj+_{ploikj { + return transformBlob(buffer, (p) => p.make.KuwoKWM(KWM_KEY)); + } + + public static make() { + return new KWMCrypto(); + } +}