♻ Rewrite decryptChunk
This commit is contained in:
parent
1230330ce1
commit
09de4b663b
2 changed files with 35 additions and 13 deletions
|
@ -1,6 +1,6 @@
|
||||||
import updateKeys from './lib/updateKeys';
|
import updateKeys from './lib/updateKeys';
|
||||||
|
|
||||||
export default function decryptChunk(encryptedChunk: Buffer, [key0, key1, key2]: [number, number, number]): {decryptedChunk: Buffer, keys: [number, number, number]} {
|
/*export default function decryptChunk(encryptedChunk: Buffer, [key0, key1, key2]: [number, number, number]): {decryptedChunk: Buffer, keys: [number, number, number]} {
|
||||||
const decryptedChunk = Buffer.alloc(encryptedChunk.length);
|
const decryptedChunk = Buffer.alloc(encryptedChunk.length);
|
||||||
const dvOut = new DataView(decryptedChunk.buffer);
|
const dvOut = new DataView(decryptedChunk.buffer);
|
||||||
|
|
||||||
|
@ -17,4 +17,35 @@ export default function decryptChunk(encryptedChunk: Buffer, [key0, key1, key2]:
|
||||||
}
|
}
|
||||||
|
|
||||||
return { decryptedChunk, keys: [key0, key1, key2] };
|
return { decryptedChunk, keys: [key0, key1, key2] };
|
||||||
|
}*/
|
||||||
|
|
||||||
|
export default function getDecryptor([key0, key1, key2]: [number, number, number]) {
|
||||||
|
let position = 0;
|
||||||
|
|
||||||
|
return (encryptedChunk: Buffer) => {
|
||||||
|
const decryptedChunk = Buffer.alloc(encryptedChunk.length);
|
||||||
|
const dvOut = new DataView(decryptedChunk.buffer);
|
||||||
|
|
||||||
|
for (let i = 0; i < encryptedChunk.length; i += 1) {
|
||||||
|
//read and decrypt byte
|
||||||
|
let curChar = encryptedChunk.readUInt8(i);
|
||||||
|
const keyPart = (key2 | 2) & 0xFFFF;
|
||||||
|
const decryptedByte = (keyPart * (keyPart ^ 1)) >>> 8;
|
||||||
|
curChar ^= decryptedByte & 0xFF;
|
||||||
|
if (position + i < 12) {
|
||||||
|
//do nothing
|
||||||
|
} else if (position + i >= 12 && position < 12) {
|
||||||
|
dvOut.setUint8(position + i - 12, curChar);
|
||||||
|
} else {
|
||||||
|
dvOut.setUint8(i, curChar);
|
||||||
|
}
|
||||||
|
|
||||||
|
//update keys
|
||||||
|
[key0, key1, key2] = updateKeys([key0, key1, key2], curChar);
|
||||||
|
}
|
||||||
|
|
||||||
|
position += encryptedChunk.length;
|
||||||
|
|
||||||
|
return decryptedChunk;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,27 +1,18 @@
|
||||||
import * as stream from 'stream';
|
import * as stream from 'stream';
|
||||||
import decryptChunk from '../decryption/decryptChunk';
|
import getDecryptor from '../decryption/decryptChunk';
|
||||||
|
|
||||||
export default function decryptStream(inputStream: stream.Readable, [key0, key1, key2]: [number, number, number]): stream.Readable {
|
export default function decryptStream(inputStream: stream.Readable, [key0, key1, key2]: [number, number, number]): stream.Readable {
|
||||||
let skippedRandomHeader = false;
|
const decryptor = getDecryptor([key0, key1, key2]);
|
||||||
|
|
||||||
const outStream = new stream.Readable({
|
const outStream = new stream.Readable({
|
||||||
read(size) {
|
read(size) {
|
||||||
//There are 12 random bytes at the beginning, we need to use them to initialize the decryption keys, but we can ignore the decrypted bytes.
|
|
||||||
if (!skippedRandomHeader) {
|
|
||||||
const encryptedHeader: Buffer = inputStream.read(12);
|
|
||||||
const { keys } = decryptChunk(encryptedHeader, [key0, key1, key2]);
|
|
||||||
[key0, key1, key2] = keys;
|
|
||||||
skippedRandomHeader = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Decrypt chunk
|
//Decrypt chunk
|
||||||
const encryptedChunk: Buffer = inputStream.read(size);
|
const encryptedChunk: Buffer = inputStream.read(size);
|
||||||
if (encryptedChunk === null) {
|
if (encryptedChunk === null) {
|
||||||
//If end has been reached, stop
|
//If end has been reached, stop
|
||||||
this.push(null);
|
this.push(null);
|
||||||
} else {
|
} else {
|
||||||
const { decryptedChunk, keys } = decryptChunk(encryptedChunk, [key0, key1, key2]);
|
const decryptedChunk = decryptor(encryptedChunk);
|
||||||
[key0, key1, key2] = keys;
|
|
||||||
this.push(decryptedChunk);
|
this.push(decryptedChunk);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in a new issue