From 3020989ccc1101e6ecd292b4de70c7e393b7e471 Mon Sep 17 00:00:00 2001 From: zhangli091011 Date: Sun, 19 Jul 2026 21:06:23 +0800 Subject: [PATCH] Fix RSA decryption of binary data --- AI_MARKER | 0 src/core/operations/RSADecrypt.mjs | 13 ++++---- tests/operations/tests/RSA.mjs | 48 ++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 AI_MARKER diff --git a/AI_MARKER b/AI_MARKER new file mode 100644 index 00000000..e69de29b diff --git a/src/core/operations/RSADecrypt.mjs b/src/core/operations/RSADecrypt.mjs index 5b32b790..2c8c3676 100644 --- a/src/core/operations/RSADecrypt.mjs +++ b/src/core/operations/RSADecrypt.mjs @@ -24,8 +24,8 @@ class RSADecrypt extends Operation { this.module = "Ciphers"; this.description = "Decrypt an RSA encrypted message with a PEM encoded private key."; this.infoURL = "https://wikipedia.org/wiki/RSA_(cryptosystem)"; - this.inputType = "string"; - this.outputType = "string"; + this.inputType = "byteArray"; + this.outputType = "byteArray"; this.args = [ { name: "RSA Private Key (PEM)", @@ -63,9 +63,9 @@ class RSADecrypt extends Operation { } /** - * @param {string} input + * @param {byteArray} input * @param {Object[]} args - * @returns {string} + * @returns {byteArray} */ run(input, args) { const [pemKey, password, scheme, md] = args; @@ -74,8 +74,9 @@ class RSADecrypt extends Operation { } try { const privKey = forge.pki.decryptRsaPrivateKey(pemKey, password); - const dMsg = privKey.decrypt(input, scheme, {md: MD_ALGORITHMS[md].create()}); - return forge.util.decodeUtf8(dMsg); + const encryptedBytes = forge.util.binary.raw.encode(Uint8Array.from(input)); + const dMsg = privKey.decrypt(encryptedBytes, scheme, {md: MD_ALGORITHMS[md].create()}); + return Array.from(forge.util.binary.raw.decode(dMsg)); } catch (err) { throw new OperationError(err); } diff --git a/tests/operations/tests/RSA.mjs b/tests/operations/tests/RSA.mjs index 42bc7246..facbc35e 100644 --- a/tests/operations/tests/RSA.mjs +++ b/tests/operations/tests/RSA.mjs @@ -46,7 +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DaQKBgQDV7rcp9k4cX8gn2IgFBFi/mbxJDtVK0Fa9 +WlY+NqmGcTwMFOKN1ZvWkK7U7RbMYsw7W7jlxyQjcOu4LaR1Y4uuxxg3ID7ItXwn +0HYom0IaotR11ovsbEaKrZSbjmwRZ/l8ruOhcZ7vdT+CPwoSt8TVOVL8bpxRrQAv +f9NqRliltwKBgG6SnLu5UzrgFpmi42ZlIY/JXH+SED5tzjh42qwgv5sJlbtCg9RJ +PTG1NGtADuD0/fkoQukT4+NiFttj9UI8WXQaw2X8F61Luk4ZRkgs3smON2vJV3hF +Pw4/5qXw4BdTDhz0R4sH77HW+2HVh9KYbxOr4qvksyEJaZpcU5wHS8SJAoGARTnc +7Wg1eHsKEtK/mRgB++YshluVa4MlPlq4I6OekdGcg7BSa2Ee73ycBmy9/t9NhTu2 +Biy9pfZJYKzsVcwjjCgCzvvRNTN8/Ik5YwRyjJn6NDN7zcJvxTpMJ1Yb5DoIAm+5 +WymgK45+QZBSmyH6QKEvGF5WmRtpPvWXHQGsd9sCgYB1U5Zgb2xf9PLs5eBI3lNc +1jKzXhsFBdMgCDM+/eQmoe2m/p+tV9tE1xIvtdm6CEhvjorgryx0dp8eldQo5Urb +NRS5V1Pds7SIg7n5A+Lqx3DQSW7WBg+/HxIfowqBmVVeOw54A1KAi93r7tzHRCqm +Gyfsg2sB9Y0VQspb36o9hg== +-----END PRIVATE KEY-----`; + TestRegister.addTests([ + { + name: "RSA Decrypt: binary OAEP-SHA256 input and output", + input: "bf130206572b4091a8b29295896d84de3d445c071cdf5646b662fb76cbe9ab905fc23cca1612926d4d05fafde3bb11f5760af0bac45ce880d3fb9b56ab3f159f31049723a5b03d0262630f2f4d984aa789d1b3c9839ead4c04eefe1adec9688f61bc5c9e9a9a25c65273169e0262910a94eeca1a0cdfceca0bdb358a42ae118df0b86903566360c23fe7ea0062b8bec56e15881747ab8286b12e12ef77d5e4dbfc5f3579b8cc4235065425da51cbb02f90d4701133aa489850e4281d45c33d30ddbf492f8ab74c0532174431d39e95e1d0be77142e6863528de82d960d81e61051b2e428e30b106d7aeed1ce77c9e99072094de5d56332fcf586a36a2c31db10", + expectedOutput: "f89766d163f006af7d37beeee64ae9e2b071abb64fe2e2fb36e8e49ee7b5b061", + recipeConfig: [ + { + "op": "From Hex", + "args": ["Auto"] + }, + { + "op": "RSA Decrypt", + "args": [PEM_PRIV_OAEP_SHA256, "", "RSA-OAEP", "SHA-256"] + }, + { + "op": "To Hex", + "args": ["None", 0] + } + ] + }, { name: "RSA Encrypt/Decrypt: RSA-OAEP/SHA-1, nothing", input: "",