fix: padding error

This commit is contained in:
Maciej Krüger 2019-10-23 14:00:11 +02:00 committed by Jacob Heun
parent 027a5a9332
commit 2c1bac5ce9
2 changed files with 8 additions and 5 deletions

View File

@ -129,8 +129,8 @@ RSA encryption/decryption for the browser with webcrypto workarround
Explanation: Explanation:
- Convert JWK to PEM - Convert JWK to PEM
- Load PEM with nodeForge - Load PEM with nodeForge
- Convert msg buffer to nodeForge buffer: it's already uint8array, so do nothing - Convert msg buffer to nodeForge buffer: ByteBuffer is a "binary-string backed buffer", so let's make our buffer a binary string
- Convert resulting nodeForge buffer to buffer: it returns a binary string, turn that into a uint8array - Convert resulting nodeForge buffer to buffer: it returns a binary string, turn that into a uint8array(buffer)
*/ */
@ -140,7 +140,8 @@ const jwkToPem = require('pem-jwk').jwk2pem
function convertKey (key, pub, msg, handle) { function convertKey (key, pub, msg, handle) {
const pem = jwkToPem(key) const pem = jwkToPem(key)
const fkey = pki[pub ? 'publicKeyFromPem' : 'privateKeyFromPem'](pem) const fkey = pki[pub ? 'publicKeyFromPem' : 'privateKeyFromPem'](pem)
const fomsg = handle(Buffer.from(msg), fkey) const fmsg = Buffer.from(msg).toString('binary')
const fomsg = handle(fmsg, fkey)
return Buffer.from(fomsg, 'binary') return Buffer.from(fomsg, 'binary')
} }

View File

@ -69,10 +69,12 @@ exports.hashAndVerify = async function (key, sig, msg) { // eslint-disable-line
return verify.verify(pem, sig) return verify.verify(pem, sig)
} }
const padding = crypto.constants.RSA_PKCS1_PADDING
exports.encrypt = function (key, bytes) { exports.encrypt = function (key, bytes) {
return crypto.publicEncrypt(jwkToPem(key), bytes) return crypto.publicEncrypt({ key: jwkToPem(key), padding }, bytes)
} }
exports.decrypt = function (key, bytes) { exports.decrypt = function (key, bytes) {
return crypto.privateDecrypt(jwkToPem(key), bytes) return crypto.privateDecrypt({ key: jwkToPem(key), padding }, bytes)
} }