From 37dac6cc3f2aea0f43d3f91647bdc1932dd425a5 Mon Sep 17 00:00:00 2001 From: Garrett Michael Flynn Date: Mon, 11 Dec 2023 10:52:41 -0800 Subject: [PATCH] Fix decryption --- src/renderer/src/progress/index.js | 16 +++++++++------- .../src/stories/pages/settings/SettingsPage.js | 2 -- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/renderer/src/progress/index.js b/src/renderer/src/progress/index.js index 7c581d4b9..d2f3229f3 100644 --- a/src/renderer/src/progress/index.js +++ b/src/renderer/src/progress/index.js @@ -27,25 +27,27 @@ function encode(text) { const cipher = crypto.createCipheriv(CRYPTO_ALGORITHM, ENCRYPTION_KEY, ENCRYPTION_IV); const encrypted = cipher.update(text); - return CRYPTO_VERSION + Buffer.concat([encrypted, cipher.final()]).toString("hex"); + return `${CRYPTO_VERSION}:${ENCRYPTION_IV.toString("hex")}:${Buffer.concat([encrypted, cipher.final()]).toString("hex")}`; } // Try to decode the value function decode(text) { - if (!crypto || !/[0-9A-Fa-f]{6}/g.test(text)) return text; - if (text.slice(0, CRYPTO_VERSION.length) !== CRYPTO_VERSION) return undefined; - text = text.slice(CRYPTO_VERSION.length); + const [ TEXT_CRYPTO_VERSION, ENCRYPTION_IV_HEX, encrypted ] = text.split(":"); + + if (text.slice(0, TEXT_CRYPTO_VERSION.length) !== CRYPTO_VERSION) return undefined; + + if (!crypto || !/[0-9A-Fa-f]{6}/g.test(encrypted)) return encrypted; try { - let textParts = text.split(":"); + let textParts = encrypted.split(":"); let encryptedText = Buffer.from(textParts.join(":"), "hex"); - let decipher = crypto.createDecipheriv(CRYPTO_ALGORITHM, ENCRYPTION_KEY, ENCRYPTION_IV); + let decipher = crypto.createDecipheriv(CRYPTO_ALGORITHM, ENCRYPTION_KEY, Buffer.from(ENCRYPTION_IV_HEX, 'hex')); let decrypted = decipher.update(encryptedText); decrypted = Buffer.concat([decrypted, decipher.final()]); return decrypted.toString(); } catch { - return text; + return encrypted; } } diff --git a/src/renderer/src/stories/pages/settings/SettingsPage.js b/src/renderer/src/stories/pages/settings/SettingsPage.js index 3266bc508..a4c7c4b25 100644 --- a/src/renderer/src/stories/pages/settings/SettingsPage.js +++ b/src/renderer/src/stories/pages/settings/SettingsPage.js @@ -20,8 +20,6 @@ const schema = merge( } ); -console.log(schema); - import { Button } from "../../Button.js"; import { global } from "../../../progress/index.js"; import { merge, setUndefinedIfNotDeclared } from "../utils.js";