diff --git a/package-lock.json b/package-lock.json index 129c189..46225e8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "license": "MIT", "dependencies": { "@babel/preset-typescript": "^7.16.5", + "@jixun/kugou-crypto": "^1.0.3", "@jixun/qmc2-crypto": "^0.0.6-R1", "@unlock-music/joox-crypto": "^0.0.1-R5", "base64-js": "^1.5.1", @@ -2986,6 +2987,22 @@ "regenerator-runtime": "^0.13.3" } }, + "node_modules/@jixun/kugou-crypto": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/@jixun/kugou-crypto/-/kugou-crypto-1.0.3.tgz", + "integrity": "sha512-ZiwSkpIAH8IkFcTfMjdQMpP/xco3iXEdYDEQo4wquYpSAln5RmSed3iBctnpoE6s3X1cxmBGhpCYW6v6vZfs+g==", + "dependencies": { + "commander": "^9.2.0" + } + }, + "node_modules/@jixun/kugou-crypto/node_modules/commander": { + "version": "9.2.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-9.2.0.tgz", + "integrity": "sha512-e2i4wANQiSXgnrBlIatyHtP1odfUp0BbV5Y5nEGbxtIrStkEOAAzCUirvLBNXHLr7kwLvJl6V+4V3XV9x7Wd9w==", + "engines": { + "node": "^12.20.0 || >=14" + } + }, "node_modules/@jixun/qmc2-crypto": { "version": "0.0.6-R1", "resolved": "https://registry.npmjs.org/@jixun/qmc2-crypto/-/qmc2-crypto-0.0.6-R1.tgz", @@ -23188,6 +23205,21 @@ "regenerator-runtime": "^0.13.3" } }, + "@jixun/kugou-crypto": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/@jixun/kugou-crypto/-/kugou-crypto-1.0.3.tgz", + "integrity": "sha512-ZiwSkpIAH8IkFcTfMjdQMpP/xco3iXEdYDEQo4wquYpSAln5RmSed3iBctnpoE6s3X1cxmBGhpCYW6v6vZfs+g==", + "requires": { + "commander": "^9.2.0" + }, + "dependencies": { + "commander": { + "version": "9.2.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-9.2.0.tgz", + "integrity": "sha512-e2i4wANQiSXgnrBlIatyHtP1odfUp0BbV5Y5nEGbxtIrStkEOAAzCUirvLBNXHLr7kwLvJl6V+4V3XV9x7Wd9w==" + } + } + }, "@jixun/qmc2-crypto": { "version": "0.0.6-R1", "resolved": "https://registry.npmjs.org/@jixun/qmc2-crypto/-/qmc2-crypto-0.0.6-R1.tgz", diff --git a/package.json b/package.json index 6f1c14d..e11c679 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ }, "dependencies": { "@babel/preset-typescript": "^7.16.5", + "@jixun/kugou-crypto": "^1.0.3", "@jixun/qmc2-crypto": "^0.0.6-R1", "@unlock-music/joox-crypto": "^0.0.1-R5", "base64-js": "^1.5.1", diff --git a/public/static/kgm.mask b/public/static/kgm.mask deleted file mode 100644 index 86b7da2..0000000 Binary files a/public/static/kgm.mask and /dev/null differ diff --git a/src/decrypt/kgm.ts b/src/decrypt/kgm.ts index 820f10d..cef6119 100644 --- a/src/decrypt/kgm.ts +++ b/src/decrypt/kgm.ts @@ -8,7 +8,7 @@ import { } from '@/decrypt/utils'; import { parseBlob as metaParseBlob } from 'music-metadata-browser'; import { DecryptResult } from '@/decrypt/entity'; -import config from '@/../package.json'; +import { decryptKgmByteAtOffsetV2, decryptVprByteAtOffset } from '@jixun/kugou-crypto/dist/utils/decryptionHelper'; //prettier-ignore const VprHeader = [ @@ -20,12 +20,6 @@ const KgmHeader = [ 0x7C, 0xD5, 0x32, 0xEB, 0x86, 0x02, 0x7F, 0x4B, 0xA8, 0xAF, 0xA6, 0x8E, 0x0F, 0xFF, 0x99, 0x14 ] -//prettier-ignore -const VprMaskDiff = [ - 0x25, 0xDF, 0xE8, 0xA6, 0x75, 0x1E, 0x75, 0x0E, - 0x2F, 0x80, 0xF3, 0x2D, 0xB8, 0xB6, 0xE3, 0x11, - 0x00 -] export async function Decrypt(file: File, raw_filename: string, raw_ext: string): Promise { const oriData = new Uint8Array(await GetArrayBuffer(file)); @@ -39,26 +33,13 @@ export async function Decrypt(file: File, raw_filename: string, raw_ext: string) let audioData = oriData.slice(headerLen); let dataLen = audioData.length; - if (audioData.byteLength > 1 << 26) { - throw Error("文件过大,请使用 CLI版本 进行解锁"); - } - let key1 = new Uint8Array(17); - key1.set(oriData.slice(0x1c, 0x2c), 0); - if (MaskV2.length === 0) { - if (!(await LoadMaskV2())) throw Error('加载Kgm/Vpr Mask数据失败'); - } + let key1 = Array.from(oriData.slice(0x1c, 0x2c)); + key1.push(0); + const decryptByte = raw_ext === 'vpr' ? decryptVprByteAtOffset : decryptKgmByteAtOffsetV2; for (let i = 0; i < dataLen; i++) { - let med8 = key1[i % 17] ^ audioData[i]; - med8 ^= (med8 & 0xf) << 4; - - let msk8 = GetMask(i); - msk8 ^= (msk8 & 0xf) << 4; - audioData[i] = med8 ^ msk8; - } - if (raw_ext === 'vpr') { - for (let i = 0; i < dataLen; i++) audioData[i] ^= VprMaskDiff[i % 17]; + audioData[i] = decryptByte(audioData[i], key1, i); } const ext = SniffAudioExt(audioData); @@ -77,68 +58,3 @@ export async function Decrypt(file: File, raw_filename: string, raw_ext: string) artist, }; } - -function GetMask(pos: number) { - return MaskV2PreDef[pos % 272] ^ MaskV2[pos >> 4]; -} - -let MaskV2: Uint8Array = new Uint8Array(0); - -async function LoadMaskV2(): Promise { - let mask_url = `https://cdn.jsdelivr.net/gh/unlock-music/unlock-music@${config.version}/public/static/kgm.mask`; - if (['http:', 'https:'].some((v) => v == self.location.protocol)) { - if (!!self.document) { - // using Web Worker - mask_url = './static/kgm.mask'; - } else { - // using Main thread - mask_url = '../static/kgm.mask'; - } - } - try { - const resp = await fetch(mask_url, { method: 'GET' }); - MaskV2 = new Uint8Array(await resp.arrayBuffer()); - return true; - } catch (e) { - console.error(e); - return false; - } -} - -//prettier-ignore -const MaskV2PreDef = [ - 0xb8, 0xd5, 0x3d, 0xb2, 0xe9, 0xaf, 0x78, 0x8c, - 0x83, 0x33, 0x71, 0x51, 0x76, 0xa0, 0xcd, 0x37, - 0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28, 0x2c, 0x84, 0x67, - 0xcd, 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, -];