diff --git a/src/ssn/decryption/decryptChunk.ts b/src/ssn/decryption/decryptChunk.ts index 3370756..f98ceee 100644 --- a/src/ssn/decryption/decryptChunk.ts +++ b/src/ssn/decryption/decryptChunk.ts @@ -19,11 +19,12 @@ import updateKeys from './lib/updateKeys'; return { decryptedChunk, keys: [key0, key1, key2] }; }*/ -export default function getDecryptor([key0, key1, key2]: [number, number, number]) { +export default function getDecryptor(decryptionKeys: [number, number, number]) { + let [key0, key1, key2] = decryptionKeys; let position = 0; return (encryptedChunk: Buffer) => { - const decryptedChunk = Buffer.alloc(encryptedChunk.length); + const decryptedChunk = Buffer.alloc(encryptedChunk.length - Math.max(12 - position, 0)); for (let i = 0; i < encryptedChunk.length; i += 1) { //read byte @@ -34,6 +35,9 @@ export default function getDecryptor([key0, key1, key2]: [number, number, number const decryptedByte = (keyPart * (keyPart ^ 1)) >>> 8; curChar ^= decryptedByte & 0xFF; + //update keys + [key0, key1, key2] = updateKeys([key0, key1, key2], curChar); + //write byte if (position + i < 12) { //do nothing @@ -42,9 +46,6 @@ export default function getDecryptor([key0, key1, key2]: [number, number, number } else { decryptedChunk.writeUInt8(curChar, i); } - - //update keys - [key0, key1, key2] = updateKeys([key0, key1, key2], curChar); } position += encryptedChunk.length;