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

Unable to encrypt text in Nodejs using publicKeyString generated at client end #14

Open
anarnoli opened this issue Sep 2, 2021 · 0 comments

Comments

@anarnoli
Copy link

anarnoli commented Sep 2, 2021

I'm working on an app where Private and Public Keypairs are generated at client side. I am passing public key to backend server ie; Node js to encrypt data.

When I'm using nodejs crypto module on backend server, it throws error
Error: error:0909006C:PEM routines:get_name:no start line

Here is my code:

const publicKey =
"w5Sjr3UekqqLntnSQtJfx22YEP/DsYt3CFDUPgb9K/ITkte9PAh7DlaApVcOhsC11z1hNQJUBBBElGZA5QYwXamiVtZxPLdXI0dlLh15r4/2ynVN/vJhF9P9UgLxwkLCAQyxJ6Z5JlnrHByrLmdWGMS48nZSes7JoYK19qrYEjEik1SoCCXy8Lxm4P/r/IF9dklo63TkADIPvESrV2uLDeF8/+esboH5Oe4IUMuRwRVYWyhU+txbdhGZzqV+f6mLwnRw3uXprkd7pi3i4jXeh93StiK2Ppn4t/L0FfW55YA0p==";

const data = "Hello temp content!";
const buffer = Buffer.from(data);
console.log(crypto.publicEncrypt(publicKey, buffer));

Any suggestion how can I use publicKeyString in Node js to encrypt data?

Update
I have observed that Public key generated using another package works perfectly with a small change like below;

const publicKey =
    "-----BEGIN PUBLIC KEY-----\n" +
"MIGeMA0GCSqGSIb3DQEBAQUAA4GMADCBiAKBgHahB2KcXQHq7RpdN5dRqKASY4+rHuctsefzFL9GQv4J3JdpQen0wb4jkxkrSOduX1NFq2WVDIr8KPc64+CNnAimxDby6QsZ8jDEGvmjdbtvqmLJSfEnMBjv7ZdWe4X8c2nrRih9iVS03TRw5+r5laBCLHz6UZklBW0OV1DMmoMjAgMBAAE=" +
    "\n-----END PUBLIC KEY-----";
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

1 participant