title: Uncaught (in promise) DOMException: key.algorithm does not match that of operation
body: <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;script&gt;
      let intermediateKey;
      let publicKey;
      let privateKey;
      let wrappedKey;
      let iv;
  
      async function rsaKeyPair() {
        let keyPair = await crypto.subtle.generateKey({
            name: "RSA-OAEP",
            modulusLength: 4096,
            publicExponent: new Uint8Array([1, 0, 1]),
            hash: "SHA-256",
          },
          true, ["wrapKey", "unwrapKey"]
        );
        publicKey = keyPair.publicKey;
        privateKey = keyPair.privateKey;
      }
  
      async function encrypt(secret) {
        // generating random intermediate key to encrypt and decrypt the secret
        intermediateKey = await crypto.subtle.generateKey({
            name: "AES-GCM",
            length: 256
          },
          true, ["encrypt", "decrypt"]
        );
        // encrypt secret
        // ...
        // wrap intermediate key (export + encrypt) intermediateKey using publicKey.
        iv = crypto.getRandomValues(new Uint8Array(12));
        wrappedKey = await crypto.subtle.wrapKey(
          "jwk",
          intermediateKey,
          publicKey, {
            name: "AES-GCM",
            iv: iv
          }
        );
      }
  
      async function decrypt(cipher) {
        // unwrap (decrypt + import) aes key using private key.
        intermediateKey = await crypto.subtle.unwrapKey(
          "jwk",
          wrappedKey,
          privateKey, {
            name: "AES-GCM",
            iv: iv
          }, {
            name: "AES-GCM"
          },
          false, ["encrypt", "decrypt"]
        );
        // decrypt the cipher
        // ...
      }
  
      async function solve() {
        // generate rsa-keypairs
        await rsaKeyPair();
        // encrypt secret
        const cipher = await encrypt("secret");
        // decrypt cipher
        await decrypt(cipher);
      }
      solve();
    &lt;/script&gt;
  &lt;/body&gt;
  
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>
  <p>generating RSA-OAEP key pair (according to <a href="http://www.w3.org/TR/WebCryptoAPI/#algorithm-overview." rel="nofollow noreferrer">http://www.w3.org/TR/WebCryptoAPI/#algorithm-overview.</a>)</p>
  <p>When the user create a secret, the secret is encrypted using AES-GCM-256 with a randomly generated intermediate key. Finally, this intermediate key is wrapped with the user's public key.</p>
  <p>finally unwrap intermediate key &amp; decryption.</p>
  <p>the error is generated during unwrapping the intermediate key.</p>