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.

asked Dec 2, 2021 at 14:19
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

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.