digitalbazaar / forge

A native implementation of TLS in Javascript and tools to write crypto-based and network-heavy webapps
https://digitalbazaar.com/
Other
5.02k stars 774 forks source link

Error: Encryption block is invalid #879

Open sumedhsbhagat opened 3 years ago

sumedhsbhagat commented 3 years ago

We are facing this issue intermittently on NodeJS(10.24) and node-forge (0.7.6).

at Object._decodePkcs1_v1_5 [as decode] (/finapp/EBPRD/SpeedUp/node_modules/node-forge/lib/rsa.js:1515:11)
at Object.key.decrypt (/finapp/EBPRD/SpeedUp/node_modules/node-forge/lib/rsa.js:1149:19)

we are encypting the data using

publicKey.encrypt(plainTxtData)

and we are trying to decrypt the data using below code.

       var privateKeyRead = readPVKey()
        var privateKey = forge.pki.privateKeyFromPem(privateKeyRead);
    var symKey = privateKey.decrypt(request.body.symKey);

I tried to debug the code till rsa.js from where this generic error is thrown.

 if(first !== 0x00 ||
    (pub && bt !== 0x00 && bt !== 0x01) ||
    (!pub && bt != 0x02) ||
    (pub && bt === 0x00 && typeof(ml) === 'undefined')) {
    throw new Error('Encryption block is invalid.');
  }

I tried to print the values of first, pub and bt. In case of success below are the values rsa.js:first: 0 rsa.js:pub: false rsa.js:bt: 2

When error occurs the values are as below.

rsa.js:first 174 rsa.js:pub: false rsa.js:bt: 87

Is this issue coming because of the NodeJs (10.24) platform we are using ?? Do we need to encode or decode the String before and after encryption ?? Why this is coming intermittently ?? Out of 10 -15 time, it will come only once.

Thanks ..

nikita-reshetnyak commented 3 months ago

@sumedhsbhagat Hi, say please could you resolve this issue?