🎨 Do not overwrite buffer during decryption

This commit is contained in:
C-3PO 2018-06-24 02:13:10 +02:00
parent 2456eae91a
commit 589f7fe786
Signed by: c3po
GPG key ID: 62993C4BB4D86F24
2 changed files with 9 additions and 3 deletions

View file

@ -1,18 +1,24 @@
import updateKeys from './updateKeys'; import updateKeys from './updateKeys';
export default function decryptFile(dv: DataView, length: number, [key0, key1, key2]: [number, number, number]) { 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) { for (let i = 0; i < length; i += 1) {
//read and decrypt byte //read and decrypt byte
let curChar = dv.getUint8(i); let curChar = dv.getUint8(i);
const keyPart = (key2 | 2) & 0xFFFF; const keyPart = (key2 | 2) & 0xFFFF;
const decryptedByte = (keyPart * (keyPart ^ 1)) >>> 8; const decryptedByte = (keyPart * (keyPart ^ 1)) >>> 8;
curChar ^= decryptedByte & 0xFF; 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 //update keys
[key0, key1, key2] = updateKeys([key0, key1, key2], curChar); [key0, key1, key2] = updateKeys([key0, key1, key2], curChar);
} }
//If it was decrypted, we skip the first 12 bytes (random encryption header) //If it was decrypted, we skip the first 12 bytes (random encryption header)
return new DataView(dv.buffer, 12, dv.byteLength - 12); return dvOut;
} }

View file

@ -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 //Use ByteReader for reading a uint8 and seeking forward across DataView boundaries
const byteReader = new ByteReader(dvArray, file.diskNumberStart, file.offset); const byteReader = new ByteReader(dvArray, file.diskNumberStart, file.offset);
//Local file header signature must be 0x04034B50 //Local file header signature
if (byteReader.readUint32() !== 0x04034B50) { if (byteReader.readUint32() !== 0x04034B50) {
throw new Error('Local file header had wrong magic'); throw new Error('Local file header had wrong magic');
} }