From 43d6f5be75b7b414653517dedc64402dfc80fc55 Mon Sep 17 00:00:00 2001 From: liamcottle Date: Mon, 27 May 2024 01:25:38 +1200 Subject: [PATCH] support aes 256 decryption keys --- src/mqtt.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/mqtt.js b/src/mqtt.js index 429b468..5d24027 100644 --- a/src/mqtt.js +++ b/src/mqtt.js @@ -238,8 +238,20 @@ function decrypt(packet) { // create decryption iv/nonce for this packet const nonceBuffer = createNonce(packet.id, packet.from); - // create aes-128-ctr decipher - const decipher = crypto.createDecipheriv('aes-128-ctr', key, nonceBuffer); + // determine algorithm based on key length + var algorithm = null; + if(key.length === 16){ + algorithm = "aes-128-ctr"; + } else if(key.length === 32){ + algorithm = "aes-256-ctr"; + } else { + // skip this key, try the next one... + console.error(`Invalid decryption key length: ${key.length}`); + continue; + } + + // create decipher + const decipher = crypto.createDecipheriv(algorithm, key, nonceBuffer); // decrypt encrypted packet const decryptedBuffer = Buffer.concat([decipher.update(packet.encrypted), decipher.final()]);