2021-12-18 13:55:31 +00:00
|
|
|
import { TeaCipher } from '@/utils/tea';
|
2021-12-16 17:28:48 +00:00
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
const SALT_LEN = 2;
|
|
|
|
const ZERO_LEN = 7;
|
2021-12-17 00:41:44 +00:00
|
|
|
|
|
|
|
export function QmcDeriveKey(raw: Uint8Array): Uint8Array {
|
2021-12-18 13:55:31 +00:00
|
|
|
const textDec = new TextDecoder();
|
2022-11-20 14:30:56 +00:00
|
|
|
let rawDec = Buffer.from(textDec.decode(raw), 'base64');
|
2021-12-17 00:41:44 +00:00
|
|
|
let n = rawDec.length;
|
|
|
|
if (n < 16) {
|
2021-12-18 13:55:31 +00:00
|
|
|
throw Error('key length is too short');
|
2021-12-17 00:41:44 +00:00
|
|
|
}
|
|
|
|
|
2022-11-20 14:30:56 +00:00
|
|
|
rawDec = decryptV2Key(rawDec);
|
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
const simpleKey = simpleMakeKey(106, 8);
|
2021-12-17 00:41:44 +00:00
|
|
|
let teaKey = new Uint8Array(16);
|
|
|
|
for (let i = 0; i < 8; i++) {
|
|
|
|
teaKey[i << 1] = simpleKey[i];
|
|
|
|
teaKey[(i << 1) + 1] = rawDec[i];
|
|
|
|
}
|
2021-12-18 13:55:31 +00:00
|
|
|
const sub = decryptTencentTea(rawDec.subarray(8), teaKey);
|
|
|
|
rawDec.set(sub, 8);
|
|
|
|
return rawDec.subarray(0, 8 + sub.length);
|
2021-12-17 00:41:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// simpleMakeKey exported only for unit test
|
2021-12-16 17:28:48 +00:00
|
|
|
export function simpleMakeKey(salt: number, length: number): number[] {
|
2021-12-18 13:55:31 +00:00
|
|
|
const keyBuf: number[] = [];
|
2021-12-16 17:28:48 +00:00
|
|
|
for (let i = 0; i < length; i++) {
|
2021-12-18 13:55:31 +00:00
|
|
|
const tmp = Math.tan(salt + i * 0.1);
|
|
|
|
keyBuf[i] = 0xff & (Math.abs(tmp) * 100.0);
|
2021-12-16 17:28:48 +00:00
|
|
|
}
|
2021-12-18 13:55:31 +00:00
|
|
|
return keyBuf;
|
2021-12-16 17:28:48 +00:00
|
|
|
}
|
|
|
|
|
2022-11-20 14:30:56 +00:00
|
|
|
const mixKey1: Uint8Array = new Uint8Array([ 0x33, 0x38, 0x36, 0x5A, 0x4A, 0x59, 0x21, 0x40, 0x23, 0x2A, 0x24, 0x25, 0x5E, 0x26, 0x29, 0x28 ])
|
|
|
|
const mixKey2: Uint8Array = new Uint8Array([ 0x2A, 0x2A, 0x23, 0x21, 0x28, 0x23, 0x24, 0x25, 0x26, 0x5E, 0x61, 0x31, 0x63, 0x5A, 0x2C, 0x54 ])
|
|
|
|
|
|
|
|
const v2KeyPrefix: Uint8Array = new Uint8Array([ 0x51, 0x51, 0x4D, 0x75, 0x73, 0x69, 0x63, 0x20, 0x45, 0x6E, 0x63, 0x56, 0x32, 0x2C, 0x4B, 0x65, 0x79, 0x3A ])
|
|
|
|
|
|
|
|
function decryptV2Key(key: Buffer): Buffer
|
|
|
|
{
|
|
|
|
const textEnc = new TextDecoder();
|
|
|
|
if (key.length < 18 || textEnc.decode(key.slice(0, 18)) !== 'QQMusic EncV2,Key:') {
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
let out = decryptTencentTea(key.slice(18), mixKey1);
|
|
|
|
out = decryptTencentTea(out, mixKey2);
|
|
|
|
const textDec = new TextDecoder();
|
|
|
|
const keyDec = Buffer.from(textDec.decode(out), 'base64');
|
|
|
|
let n = keyDec.length;
|
|
|
|
if (n < 16) {
|
|
|
|
throw Error('EncV2 key decode failed');
|
|
|
|
}
|
|
|
|
|
|
|
|
return keyDec;
|
|
|
|
}
|
|
|
|
|
2021-12-16 17:28:48 +00:00
|
|
|
function decryptTencentTea(inBuf: Uint8Array, key: Uint8Array): Uint8Array {
|
|
|
|
if (inBuf.length % 8 != 0) {
|
2021-12-18 13:55:31 +00:00
|
|
|
throw Error('inBuf size not a multiple of the block size');
|
2021-12-16 17:28:48 +00:00
|
|
|
}
|
|
|
|
if (inBuf.length < 16) {
|
2021-12-18 13:55:31 +00:00
|
|
|
throw Error('inBuf size too small');
|
2021-12-16 17:28:48 +00:00
|
|
|
}
|
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
const blk = new TeaCipher(key, 32);
|
2021-12-16 17:28:48 +00:00
|
|
|
|
|
|
|
const tmpBuf = new Uint8Array(8);
|
|
|
|
const tmpView = new DataView(tmpBuf.buffer);
|
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
blk.decrypt(tmpView, new DataView(inBuf.buffer, inBuf.byteOffset, 8));
|
2021-12-16 17:28:48 +00:00
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
const nPadLen = tmpBuf[0] & 0x7; //只要最低三位
|
2021-12-16 17:28:48 +00:00
|
|
|
/*密文格式:PadLen(1byte)+Padding(var,0-7byte)+Salt(2byte)+Body(var byte)+Zero(7byte)*/
|
|
|
|
const outLen = inBuf.length - 1 /*PadLen*/ - nPadLen - SALT_LEN - ZERO_LEN;
|
2021-12-18 13:55:31 +00:00
|
|
|
const outBuf = new Uint8Array(outLen);
|
2021-12-16 17:28:48 +00:00
|
|
|
|
|
|
|
let ivPrev = new Uint8Array(8);
|
|
|
|
let ivCur = inBuf.slice(0, 8); // init iv
|
|
|
|
let inBufPos = 8;
|
|
|
|
|
|
|
|
// 跳过 Padding Len 和 Padding
|
|
|
|
let tmpIdx = 1 + nPadLen;
|
|
|
|
|
|
|
|
// CBC IV 处理
|
|
|
|
const cryptBlock = () => {
|
|
|
|
ivPrev = ivCur;
|
2021-12-18 13:55:31 +00:00
|
|
|
ivCur = inBuf.slice(inBufPos, inBufPos + 8);
|
2021-12-16 17:28:48 +00:00
|
|
|
for (let j = 0; j < 8; j++) {
|
2021-12-18 13:55:31 +00:00
|
|
|
tmpBuf[j] ^= ivCur[j];
|
2021-12-16 17:28:48 +00:00
|
|
|
}
|
2021-12-18 13:55:31 +00:00
|
|
|
blk.decrypt(tmpView, tmpView);
|
2021-12-16 17:28:48 +00:00
|
|
|
inBufPos += 8;
|
|
|
|
tmpIdx = 0;
|
2021-12-18 13:55:31 +00:00
|
|
|
};
|
2021-12-16 17:28:48 +00:00
|
|
|
|
|
|
|
// 跳过 Salt
|
2021-12-18 13:55:31 +00:00
|
|
|
for (let i = 1; i <= SALT_LEN; ) {
|
2021-12-16 17:28:48 +00:00
|
|
|
if (tmpIdx < 8) {
|
|
|
|
tmpIdx++;
|
|
|
|
i++;
|
|
|
|
} else {
|
2021-12-18 13:55:31 +00:00
|
|
|
cryptBlock();
|
2021-12-16 17:28:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 还原明文
|
|
|
|
let outBufPos = 0;
|
|
|
|
while (outBufPos < outLen) {
|
|
|
|
if (tmpIdx < 8) {
|
|
|
|
outBuf[outBufPos] = tmpBuf[tmpIdx] ^ ivPrev[tmpIdx];
|
2021-12-18 13:55:31 +00:00
|
|
|
outBufPos++;
|
2021-12-16 17:28:48 +00:00
|
|
|
tmpIdx++;
|
|
|
|
} else {
|
2021-12-18 13:55:31 +00:00
|
|
|
cryptBlock();
|
2021-12-16 17:28:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 校验Zero
|
|
|
|
for (let i = 1; i <= ZERO_LEN; i++) {
|
|
|
|
if (tmpBuf[tmpIdx] != ivPrev[tmpIdx]) {
|
2021-12-18 13:55:31 +00:00
|
|
|
throw Error('zero check failed');
|
2021-12-16 17:28:48 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-18 13:55:31 +00:00
|
|
|
return outBuf;
|
2021-12-16 17:28:48 +00:00
|
|
|
}
|