react-native-rsa-native icon indicating copy to clipboard operation
react-native-rsa-native copied to clipboard

decrypting with node-rsa

Open aprilmintacpineda opened this issue 5 years ago • 5 comments

I have a form that I'm encrypting with react-native-rsa-native:

const form = {
  value: 1,
  value2: 2
};

const encryptedBody = await RSA.encrypt(JSON.stringify(body), FORM_PUBLIC_KEY);
const body = { encryptedBody };

await fetch('/to/path', {
  body: JSON.stringify(body)
});

I receive it on the backend as expected, now I try to decrypt it with node-rsa:

const fs = require('fs').promises;
const path = require('path');
const NodeRSA = require('node-rsa');

async function decryptFormBody (body) {
  const privateKey = await fs.readFile(
    path.join(__dirname, '../resources/forms.key'),
    'utf8'
  );

  console.log(privateKey);

  const nodeRsa = new NodeRSA(privateKey);
  const { encryptedBody } = JSON.parse(body);

  return nodeRsa.decrypt(encryptedBody);
}

I keep on getting this error:

{
    "errorType": "Error",
    "errorMessage": "Error during decryption (probably incorrect key). Original error: Error: error:04099079:rsa routines:RSA_padding_check_PKCS1_OAEP_mgf1:oaep decoding error",
    "stack": [
        "Error: Error during decryption (probably incorrect key). Original error: Error: error:04099079:rsa routines:RSA_padding_check_PKCS1_OAEP_mgf1:oaep decoding error",
        "    at NodeRSA.module.exports.NodeRSA.$$decryptKey (/opt/nodejs/node_modules/node-rsa/src/NodeRSA.js:301:19)",
        "    at NodeRSA.module.exports.NodeRSA.decrypt (/opt/nodejs/node_modules/node-rsa/src/NodeRSA.js:249:21)",
        "    at decryptFormBody (/opt/nodejs/utils/decryptFormBody.js:16:18)",
        "    at async Runtime.handler (/var/task/src/functions/subscribe.js:15:25)"
    ]
}

I know that the private and public keys are correct. Here's how I generate my keys https://gist.github.com/aprilmintacpineda/baa777820819c14cae26d795517eb87e

I tried to decrypt the encrypted message using https://8gwifi.org/rsafunctions.jsp but it doesn't work there too.

aprilmintacpineda avatar Oct 20 '20 10:10 aprilmintacpineda

I'm having similar problem, i can decrypt data from my backend but anything encrypted with RSA.encrypt cannot be decrypted but with RSA.decrypt. I also tested with multiple online decrypt sites and all of them gives various errors too.

aalhitennf avatar Jul 19 '21 23:07 aalhitennf

Fixed it. I needed to base64 decode the encrypted data at my backend.

aalhitennf avatar Jul 22 '21 19:07 aalhitennf

I'm having the same issue. I can't decrypt the message coming from the react native app using node-rsa. I tried to decode to base64 as @aalhitennf mentioned, but I was not well succeed

brbnk avatar Nov 11 '21 18:11 brbnk

@brbnk @aalhitennf can you share the backend code. I have the same issue

acollazomayer avatar Jan 14 '22 17:01 acollazomayer

edit. oops, my bad. i misread the package name. i switched from rsa to aes and my solution didn't apply to this anymore.

aalhitennf avatar Jan 16 '22 13:01 aalhitennf