🎨 Do not overwrite buffer during decryption
This commit is contained in:
parent
2456eae91a
commit
589f7fe786
2 changed files with 9 additions and 3 deletions
|
@ -1,18 +1,24 @@
|
|||
import updateKeys from './updateKeys';
|
||||
|
||||
export default function decryptFile(dv: DataView, length: number, [key0, key1, key2]: [number, number, number]) {
|
||||
const decryptedBuffer = new ArrayBuffer(dv.byteLength - 12);
|
||||
const dvOut = new DataView(decryptedBuffer);
|
||||
|
||||
for (let i = 0; i < length; i += 1) {
|
||||
//read and decrypt byte
|
||||
let curChar = dv.getUint8(i);
|
||||
const keyPart = (key2 | 2) & 0xFFFF;
|
||||
const decryptedByte = (keyPart * (keyPart ^ 1)) >>> 8;
|
||||
curChar ^= decryptedByte & 0xFF;
|
||||
dv.setUint8(i, curChar);
|
||||
//Skip the first 12 bytes (random encryption header)
|
||||
if (i >= 12) {
|
||||
dvOut.setUint8(i - 12, curChar);
|
||||
}
|
||||
|
||||
//update keys
|
||||
[key0, key1, key2] = updateKeys([key0, key1, key2], curChar);
|
||||
}
|
||||
|
||||
//If it was decrypted, we skip the first 12 bytes (random encryption header)
|
||||
return new DataView(dv.buffer, 12, dv.byteLength - 12);
|
||||
return dvOut;
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@ export default async function extractFile(file: ISsnFileEntry, dvArray: DataView
|
|||
//Use ByteReader for reading a uint8 and seeking forward across DataView boundaries
|
||||
const byteReader = new ByteReader(dvArray, file.diskNumberStart, file.offset);
|
||||
|
||||
//Local file header signature must be 0x04034B50
|
||||
//Local file header signature
|
||||
if (byteReader.readUint32() !== 0x04034B50) {
|
||||
throw new Error('Local file header had wrong magic');
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue