Skip to content

using react-native-rsa-native to encrypt, can't decrypt with node-rsa #187

Open
@aprilmintacpineda

Description

@aprilmintacpineda

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions