sopan mittal
sopan mittal

Reputation: 45

Error: error:04099079:rsa routines:RSA_padding_check_PKCS1_OAEP_mgf1:oaep decoding error

I'm trying to decrypt the message which I have encrypted by using crypto.publicEncrypt, but I'm getting the following error :

internal/crypto/cipher.js:44
    return method(toBuf(key), buffer, padding, passphrase);
           ^

Error: error:04099079:rsa routines:RSA_padding_check_PKCS1_OAEP_mgf1:oaep decoding error
    at Object.privateDecrypt (internal/crypto/cipher.js:44:12)

 let enc = crypto.privateDecrypt({
        key: privateKey,
        padding: crypto.constants.RSA_PKCS1_OAEP_PADDING
    }, Buffer.from(message, 'base64'));

Upvotes: 4

Views: 1669

Answers (1)

Bola Gadalla
Bola Gadalla

Reputation: 400

If you're generating KeyPairs this way:

const keyPair = crypto.generateKeyPairSync('rsa', {
      modulusLength: 2048,
      publicKeyEncoding: {
        type: 'spki',
        format: 'pem'
      },
      privateKeyEncoding: {
        type: 'pkcs8', // or pkcs1
        format: 'pem',
        cipher: 'aes-256-cbc',
        passphrase: '<passPhrase>'
      }
    });

Then you need to pass no padding by doing this:

crypto.privateDecrypt(
      {
        key: this.privateKey,
        passphrase: '<passPhrase>',
        padding: crypto.constants.RSA_NO_PADDING, // <-- You need to pass this to padding property
      }, 
      Buffer.from(encryptedText, 'base64')).toString('utf8');

Upvotes: 2

Related Questions