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.
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.'pkcs8-public'or'pkcs8-public-pem'give a errorInvalidAsn1Error: Expected 0x30: got 0x2'pkcs1'and'public'and give same errors!