【问题标题】:Uncaught (in promise) DOMException: key.algorithm does not match that of operationUncaught (in promise) DOMException: key.algorithm does not match that of operation
【发布时间】:2021-04-28 11:43:25
【问题描述】:

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
  <script>
    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();
  </script>
</body>

</html>

生成 RSA-OAEP 密钥对(根据http://www.w3.org/TR/WebCryptoAPI/#algorithm-overview.)

当用户创建密钥时,该密钥会使用 AES-GCM-256 和随机生成的中间密钥进行加密。最后,这个中间密钥被用户的公钥包裹起来。

最后解开中间密钥和解密。

错误是在解开中间密钥的过程中产生的。

【问题讨论】:

  • 如果连接不是 https,那么您如何加密密钥(让黑客或嗅探器代码嗅出)都无关紧要。但是到你的错误,哪一行?什么是逐字的“堆栈跟踪”?
  • @GetSet 在解包密钥期间的解密函数中。
  • 你已经说过了。我的问题是您的堆栈跟踪是什么,您还没有回答吗?
  • “堆栈跟踪”是完整的错误,而不是摘要。当你找到它时,也许用它更新(编辑)你的问题。有助于调试,因为您的计算机上没有人看到它。
  • Uncaught (in promise) DOMException: key.algorithm does not match that of operation solve @ js:81 async function (async) solve @ js:76 (anonymous) @ js:82

标签: javascript cryptography webcrypto


【解决方案1】:

wrapKey() 和 unwrapKey() 调用都缺少分别用于指定加密密钥的算法的 wrapAlgo 和 unwrapAlgo 参数的正确规范。在此示例中,应用了带有 OAEP 的 RSA,因此两个参数都必须使用 RsaOaepParams 对象。

如果两个函数中的参数都正确指定,则AES密钥将被正确加密和解​​密:

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() {

    // Generate AES key 
    intermediateKey = await crypto.subtle.generateKey(
        {
            name: 'AES-GCM',
            length: 256
        },
        true, 
        ['encrypt', 'decrypt']
    );
    
    // Encrypt secret
    // ...
    
    // Wrap AES key
    /*
    iv = crypto.getRandomValues(new Uint8Array(12));
    wrappedKey = await crypto.subtle.wrapKey(
        'jwk',
        intermediateKey,
        publicKey, 
        {
            name: "AES-GCM",
            iv: iv
        }
    );
    */
    wrappedKey = await crypto.subtle.wrapKey(
        'raw',
        intermediateKey,
        publicKey, 
        {                           // wrapAlgo, here an RsaOaepParams object, s. https://developer.mozilla.org/en-US/docs/Web/API/RsaOaepParams
            name: 'RSA-OAEP'
        }
    );    
    console.log('Wrapped AES key: ', new Uint8Array(wrappedKey));
}

async function decrypt() {

    // Unwrap AES key
    /*
    intermediateKey = await crypto.subtle.unwrapKey(
        'jwk',
        wrappedKey,
        privateKey, 
        {
            name: "AES-GCM",
            iv: iv
        }, 
        {
            name: "AES-GCM"
        },
        false, 
        ["encrypt", "decrypt"]
    );
    */
    intermediateKey = await crypto.subtle.unwrapKey(
        'raw',
        wrappedKey,
        privateKey, 
        {                           // unwrapAlgo, here an RsaOaepParams object, s. https://developer.mozilla.org/en-US/docs/Web/API/RsaOaepParams
            name: 'RSA-OAEP'
        }, 
        {
            name: 'AES-GCM'
        },
        false, ['encrypt', 'decrypt']
    );
    console.log('Unwrapped AES key: ', intermediateKey);
  
    // Decrypt ciphertext
    // ...
}

async function solve() {
    await rsaKeyPair();
    await encrypt();
    await decrypt();
}

solve();

【讨论】:

  • 在 AES-GCM 模式下包装 AES 密钥期间是否需要生成 IV?还是 iv 仅在 AES-CBC 模式下使用?
  • 包装/解包不需要IV,但对于CBC和GCM都需要加密/解密。由于出于安全原因,密钥/IV 对只能使用一次,因此通常为每次加密生成一个随机 IV(CBC 使用 16 字节 IV,GCM 使用 12 字节 IV)。 IV 不是秘密的,它与密文一起发送给接收者(通常 IV 带有前缀,因此只需要发送一个值:IV || 密文)。接收者将这两个部分分开并可以执行解密。
猜你喜欢
  • 2018-01-27
  • 2021-09-07
  • 2020-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-05
  • 2018-02-19
相关资源
最近更新 更多