From 1cfd9394429b23bd2d00b484a62e0072037b606c Mon Sep 17 00:00:00 2001 From: David Halls Date: Fri, 14 Apr 2023 12:47:29 +0100 Subject: [PATCH] Fix OAEP padding --- ext/rsa.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ext/rsa.js b/ext/rsa.js index b27c421e..8d835af5 100644 --- a/ext/rsa.js +++ b/ext/rsa.js @@ -186,12 +186,14 @@ function RSAEncrypt(text) { // Return the PKCS#1 OAEP RSA encryption of "text" as an even-length hex string function RSAEncryptOAEP(text, hash, hashLen) { - var m = oaep_pad(text, (this.n.bitLength() + 7) >> 3, hash, hashLen); + var n = (this.n.bitLength() + 7) >> 3; + var m = oaep_pad(text, n, hash, hashLen); if(m == null) return null; var c = this.doPublic(m); if(c == null) return null; var h = c.toString(16); - if((h.length & 1) == 0) return h; else return "0" + h; + while (h.length < n*2) h = "0" + h; + return h; } // Return the PKCS#1 RSA encryption of "text" as a Base64-encoded string