I'm using NodeJS Crypto module for encrypting and decrypting with RSA in backend and JSencrypt for frontend RSA
But issue is my backend throws this error whenever I encrypt in frontend using publickey (PS: I'm using this in NuxtJS so using import function.)
const { JSEncrypt } = await import('jsencrypt')
const rsa = new JSEncrypt({ default_key_size: 1024 })
rsa.setPublicKey(store.state.publicKey)
const xKey = rsa.encrypt(store.state.ticket)
and then whenever I try to decode using this piece of code in my backend it throws this
Error: error:04099079:rsa routines:RSA_padding_check_PKCS1_OAEP_mgf1:oaep decoding error
Here is my backend code for RSA decoding using privateKey
const privateKey = fs.readFileSync('RSA_private.key', { encoding: 'utf8' })
exports.RSAdecrypt = async (data) => {
const buffer = Buffer.from(data, "base64")
const decrypted = crypto.privateDecrypt(privateKey, buffer)
return decrypted.toString('utf8')
}
I found a solution. I saw on this post that
JSencrypt
usespkcs1
padding by default. so I have changed my decryptor withpkcs1
Bydefault node crypto usespkcs1_oaep
by default.here is code for decryptor.