Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
aprilmintacpineda opened this issue Oct 20, 2020 · 2 comments

Comments

@aprilmintacpineda
Copy link

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

@alantoledo007
Copy link

alantoledo007 commented Apr 22, 2022

Hi, I had the same error. i fixed it like this: #215 (comment)

publicKey.encrypt("");
const decryptedData = privateKey.decrypt("your base64 encryption", "utf8");

@reezon
Copy link

reezon commented Dec 6, 2022

i fix it。because node-rsa use pkcs1_oaep. so need to add code:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants