title: WebCrypto API: How to decrypt a RSA encrypted message with a public key?
body: <p>I have a RSA encrypted message that can be decoded with openssl:</p>
  
  <pre><code>openssl rsautl -inkey cert.pem -pubin -in encrypted -out plaintext
  </code></pre>
  
  <p>How can I achieve this with the WebCrypto API? I get an error when trying the following:</p>
  
  <pre><code>window.crypto.subtle.importKey(
  "spki",
  new Uint8Array(pubKey),
  {
    name: "RSA-OAEP",
    hash: {name: "SHA-256"}
  },
  true,
  ["verify"]
  )
  .then(function(publicKey){
  
    console.log(publicKey);
  
    window.crypto.subtle.decrypt(
      {
        name: "RSA-OAEP"
      },
      publicKey,
      new Uint8Array(encrypted)
      )
      .then(function(decrypted){
        console.log(new Uint8Array(decrypted));
      })
      .catch(function(err){
        console.error(err);
      });
  
  })
  .catch(function(err){
    throw(err);
  });
  </code></pre>
  
  <p>(see <a href="https://jsfiddle.net/tz2cpze6/1/" rel="nofollow">https://jsfiddle.net/tz2cpze6/1/</a>)</p>