From 9d32cd36eef5eb086ae8a2de537fff6bde06f020 Mon Sep 17 00:00:00 2001 From: Nriver <6752679+Nriver@users.noreply.github.com> Date: Wed, 15 Jan 2025 10:16:04 +0800 Subject: [PATCH 1/3] Fix default ivLength in dump-db tool --- dump-db/inc/decrypt.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dump-db/inc/decrypt.ts b/dump-db/inc/decrypt.ts index d7ae92614..ecbc820b5 100644 --- a/dump-db/inc/decrypt.ts +++ b/dump-db/inc/decrypt.ts @@ -16,7 +16,7 @@ function decryptString(dataKey: any, cipherText: any) { return str; } -function decrypt(key: any, cipherText: any, ivLength = 13) { +function decrypt(key: any, cipherText: any, ivLength = 16) { if (cipherText === null) { return null; } From 4b7445be8e680996cb5bb016e68a4c064ea8cbb6 Mon Sep 17 00:00:00 2001 From: Nriver <6752679+Nriver@users.noreply.github.com> Date: Thu, 16 Jan 2025 14:01:59 +0800 Subject: [PATCH 2/3] fix compatibility for old encrypted data --- dump-db/inc/decrypt.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/dump-db/inc/decrypt.ts b/dump-db/inc/decrypt.ts index ecbc820b5..60a846746 100644 --- a/dump-db/inc/decrypt.ts +++ b/dump-db/inc/decrypt.ts @@ -16,7 +16,7 @@ function decryptString(dataKey: any, cipherText: any) { return str; } -function decrypt(key: any, cipherText: any, ivLength = 16) { +function decrypt(key: any, cipherText: any) { if (cipherText === null) { return null; } @@ -27,6 +27,8 @@ function decrypt(key: any, cipherText: any, ivLength = 16) { try { const cipherTextBufferWithIv = Buffer.from(cipherText.toString(), "base64"); + // old encrypted data can have IV of length 13, see some details here: https://github.com/zadam/trilium/issues/3017 + const ivLength = cipherTextBufferWithIv.length % 16 === 0 ? 16 : 13; const iv = cipherTextBufferWithIv.slice(0, ivLength); const cipherTextBuffer = cipherTextBufferWithIv.slice(ivLength); From 5ea3e67dc307cc197a6b257d07427c7aece94748 Mon Sep 17 00:00:00 2001 From: Nriver <6752679+Nriver@users.noreply.github.com> Date: Thu, 16 Jan 2025 14:18:01 +0800 Subject: [PATCH 3/3] remove unused param --- dump-db/inc/data_key.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dump-db/inc/data_key.ts b/dump-db/inc/data_key.ts index 35594926a..ed6d95904 100644 --- a/dump-db/inc/data_key.ts +++ b/dump-db/inc/data_key.ts @@ -12,7 +12,7 @@ function getDataKey(password: any) { const encryptedDataKey = getOption("encryptedDataKey"); - const decryptedDataKey = decryptService.decrypt(passwordDerivedKey, encryptedDataKey, 16); + const decryptedDataKey = decryptService.decrypt(passwordDerivedKey, encryptedDataKey); return decryptedDataKey; } catch (e: any) {