m0j1
m0j1

Reputation: 4267

Converting a public key in string format to cryptokey in SubtleCrypto

I'm trying to use crypto module in js and subtleCrypto to encode a string with a public key. I want to give the public key as a string but my code in subtle crypto throws error that "parameter 2 is not of type 'CryptoKey'. here's my code:

const handleEncodeClick = () => {
 const crypto = window.crypto.subtle;
 crypto.encrypt({name: "RSA-OAEP"}, publicKey, new TextEncoder().encode(myMsg))
  .then(encrypted => {
   setEncoded(new Uint8Array(encrypted));
  })
  .catch(err => {
   console.log(err);
  });
 };

Upvotes: 1

Views: 2172

Answers (0)

Related Questions