3

Error: Error during decryption (probably incorrect key). Original error: Error: This is not private key

Here is my nodejs code. I am using node-rsa.

  const keyData = fs
    .readFileSync("./docs/PublicKey/XXX_sandbox.pem")
    .toString();
  const NodeRSA = require("node-rsa");
  var key = new NodeRSA();
  key.importKey(keyData, "pkcs8-public");
  const encrypted = key.encrypt("Test@1234", "base64");
  console.log("encrypted: ", encrypted);

Here is my pem file:

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArxd93uLDs8HTPqcSPpxZ
rf0Dc29r3iPp0a8filjAyeX4RAH6lWm9qFt26CcE8ESYtmo1sVtswvs7VH4Bjg/F
DlRpd+MnAlXuxChij8/vjyAwE71ucMrmZhxM8rOSfPML8fniZ8trr3I4R2o4xWh6
no/xTUtZ02/yUEXbphw3DEuefzHEQnEF+quGji9pvGnPO6Krmnri9H4WPY0ysPQQ
Qd82bUZCk9XdhSZcW/am8wBulYokITRMVHlbRXqu1pOFmQMO5oSpyZU3pXbsx+Ox
IOc4EDX0WMa9aH4+snt18WAXVGwF2B4fmBk7AtmkFzrTmbpmyVqA3KO2IjzMZPw0
hQIDAQAB
-----END PUBLIC KEY-----
neubert
  • 15,947
  • 24
  • 120
  • 212
Monish N
  • 330
  • 1
  • 6
  • 15
  • 1
    You try to decrypt in the second-to-last line with a _public_ key. But for the decryption you need the _private_ key (which must belong to the public key you encrypted with), see [_public-key cryptography_](https://en.wikipedia.org/wiki/Public-key_cryptography). By the way, this private key doesn't correspond to the `privateKey` variable in the code, because the latter is only the ciphertext to the plaintext `Test@1234` (but maybe this is just a poorly chosen name). – Topaco Mar 11 '20 at 16:40
  • @Topaco can you tell whether I have done the encryption properly. Actually I am trying to integrate with an API which returns a bad response saying that decryption of password failed. – Monish N Mar 11 '20 at 16:59
  • Please don't change the question afterwards (apart from formatting etc.), as it is difficult for subsequent readers to understand. Then better add the changes, marked accordingly, at the end of the question. I don't see any issues in the encryption, but you should verify this with your own key pair (with Node-RSA a test key pair can easily be generated) by performing a complete cycle (encryption and decryption). Issues concerning your API can be caused e.g. by different paddings or mismatching keys. – Topaco Mar 11 '20 at 17:43

1 Answers1

1

I solved my problem by setting encrytionScheme to "pkcs1". Find the code below. Use key.setOptions in node-rsa.

var NodeRSA = require("node-rsa");

var key = new NodeRSA();

const keyData =
    "-----BEGIN PUBLIC KEY-----\n" +
    "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArxd93uLDs8HTPqcSPpxZ\n" +
    "rf0Dc29r3iPp0a8filjAyeX4RAH6lWm9qFt24CcE8ESYtmo1sVtswvs7VH4Bjg/F\n" +
    "DlRpd+MnAlXuxChij8/vjyAwE71ucMrmZhxM8rOSfPML8fniZ8trr3I4R2o4xWh6\n" +
    "no/xTUtZ02/yUEXbphw3DEu9fzHEQnEF+quGji9pvGnPO6Krmnri9H4WPY0ysPQQ\n" +
    "Qd82bUZCk9XdhSZcW/am8wBulYokITRMVHlbRXqu1pOFmQMO5oSpyZU3pXbsx+Ox\n" +
    "IOc4EDX0WMa9aH4+snt18WAXVGwF2B4fmBk7AtmkFzrTmbpmyVqA3KO2IjzMZPw0\n" +
    "hQIDAQAB\n" +
    "-----END PUBLIC KEY-----";

//setOptions ecryptionScheme is default to pkcs1_oaep by setting this to pkcs1. I could able to solve my problem

  key.setOptions({
    encryptionScheme: "pkcs1"
  });

  key.importKey(keyData, "pkcs8-public");
  const encrypted = key.encrypt("Test@1234", "base64");
  res.send(encrypted);
jps
  • 20,041
  • 15
  • 75
  • 79
Monish N
  • 330
  • 1
  • 6
  • 15