title: Using native javascript / subtleCrypto to encrypt using RSA
body: <p>I am trying to follow the webdocs for <code>subtleCrypto</code>:</p>
  <p><a href="https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/encrypt" rel="noreferrer">https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/encrypt</a></p>
  <blockquote>
  <p>To use RSA-OAEP, pass an RsaOaepParams object.</p>
  </blockquote>
  <p>How is the <code>RSA</code> key supposed to be formatted? The following code is just using plaintext <code>rsaPublicKey</code> and <code>rsaPrivateKey</code>: how should they be changed</p>
  <pre><code>let rsaPublicKey = &quot;ssh-rsa AAAAB3 ...&quot;
  
  function encrypt(rsaPublicKey, msg) {
      let emsg = new TextEncoder().encode(msg)
      let encrypted = crypto.subtle.encrypt(
          {
              name: &quot;RSA-OAEP&quot;
          },
          rsaPublicKey,
          msg
      );
      return encrypted
  }
  </code></pre>
  <pre><code>let rsaPrivateKey = &quot;MIIEvQIBADU ...&quot;
  function decrypt(rsaPrivateKey, encrypted) {
    return window.crypto.subtle.decrypt(
      {
        name: &quot;RSA-OAEP&quot;
      },
      rsaPrivateKey,
      encrypted
    );
  }
  </code></pre>
  <p>Here is the (probably incorrect) code attempting to do the round trip:</p>
  <pre><code>let enc = encrypt(rsaKey, &quot;hello world!&quot;) // ERROR on this line
  console.log(enc)
  let dec = decrypt(rsaPrivateKey, enc)
  console.log(dec)
  </code></pre>
  <p>The error is:</p>
  <blockquote>
  <p>Uncaught (in promise) TypeError: Failed to execute 'encrypt' on 'SubtleCrypto': parameter 2 is not of type 'CryptoKey'.</p>
  </blockquote>
  <p>So how should the public (and private) keys be encoded/ formatted?</p>