cryptico-js icon indicating copy to clipboard operation
cryptico-js copied to clipboard

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

Open anarnoli opened this issue 4 years ago • 0 comments

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-----";

anarnoli avatar Sep 02 '21 18:09 anarnoli