web/src/decrypt/qmc_key.ts

104 lines
2.6 KiB
TypeScript
Raw Normal View History

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();
const 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
}
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
}
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
}