Skip to content

Why different default RSA padding at publicEncrypt and publicDecrypt ? #1093

@cnasikas

Description

@cnasikas

I am wondering why there is a different in RSA padding at publicEncrypt (RSA_PKCS1_OAEP_PADDING) and publicDecrypt (RSA_PKCS1_PADDING) functions in crypto lib ? Shouldn't be the same for consistency ?

  • Node.js Version: v8.9.4
  • OS: macOS High Sierra 10.13.3
  • Module: Crypto

Proof of concept:

import crypto from 'crypto'

function encrypt (plaintext) {
    return new Promise((resolve, reject) => {
      this._readFile('public.pem', 'utf8')
      .then((pubKey) => {
        let encBuffer = crypto.publicEncrypt(pubKey, Buffer.from(plaintext, 'utf-8'))
        resolve(encBuffer.toString('base64'))
      })
      .catch((err) => {
        reject(err)
      })
    })
  }

function decrypt (cipher) {
    return new Promise((resolve, reject) => {
      this._readFile('private.pem', 'utf8')
      .then((secKey) => {
        let decBuffer = crypto.publicDecrypt(secKey, Buffer.from(cipher, 'base64'))
        resolve(decBuffer.toString('utf8'))
      })
      .catch((err) => {
        console.log(err)
        reject(err)
      })
    })
  }




encrypt('test')
.then((cipher) => {
    console.log("Encrypted: " + cipher)
    return decrypt(cipher)
})
.then((plaintext) => {
    console.log("Decrypted: " + plaintext)
})

This will produce the following error (due to the difference in padding scheme):

Error: error:0407006A:rsa routines:RSA_padding_check_PKCS1_type_1:block type is not 01

I am not comfortable choosing the right RSA padding myself so I think it would be nice to be the same for both function or at least being explicit mention at the documention why there is different padding schemes.

Thanks a lot!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions