0

What I have to do is encrypt a small message through a public key. I have the public key but I can't encrypt any messages. I have tried with JSEncrypt, encrypt-rsa, hybrid-crypto-js, NodeRSA and crypto. Can´t encrypt message.

The key is something like

      let keyData = `-----BEGIN PUBLIC KEY-----MIIBCgKCAQEAzK1MF/...-----END PUBLIC KEY-----`

The closest to getting anything was through 'NodeRSA', which when doing key.importkey(keyData, 'pkcs8'), returns an error "InvalidAsn1Error: encoding too long"

Can someone give me a hand?


My code is:

import NodeRSA from 'node-rsa';
const key = new NodeRSA();
let keyData = `-----BEGIN PUBLIC KEY-----MIIBCgKCAQEAzK1MF/...-----END PUBLIC KEY-----`
key.importKey(keyData, 'pkcs8');

I stopped here because you gave me the mistake.

12
  • The second parameter of importKey() must be 'pkcs8-public' or 'pkcs8-public-pem' for a PEM encoded public key in X.509/SPKI format or omitted altogether. 'pkcs8' imports a PEM encoded private key in PKCS#8 format. By the way, 'pkcs8' is actually a misnomer in the context of a public key, but there is already an issue about that. Commented Dec 2, 2021 at 15:52
  • with 'pkcs8-public' or 'pkcs8-public-pem' give a error InvalidAsn1Error: Expected 0x30: got 0x2 Commented Dec 2, 2021 at 16:02
  • I tried with 'pkcs1' and 'public' and give same errors! Commented Dec 2, 2021 at 16:05
  • Which node-rsa version are you using? Commented Dec 2, 2021 at 16:06
  • "node-rsa":"^1.1.1" library node-rsa Commented Dec 2, 2021 at 16:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.