Microsoft Edge中的公钥加密

Fen*_*ang 5 encryption public-key-encryption webcrypto-api microsoft-edge

我有以下JavaScript代码使用Web Cryptography API实现公钥加密.它适用于Firefox和Chrome,但Microsoft Edge失败.我从Edge获得的错误是"由于错误80700011无法完成操作".我错过了什么?

<script>
    var data = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);

    var crypto = window.crypto || window.msCrypto;
    var cryptoSubtle = crypto.subtle;

    cryptoSubtle.generateKey(
        {
            name: "RSA-OAEP",
            modulusLength: 2048, 
            publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
            hash: { name: "SHA-256" }, 
        },
        true, 
        ["encrypt", "decrypt"]
    ).then(function (key) { 
        console.log(key);
        console.log(key.publicKey);
        return cryptoSubtle.encrypt(
            {
                name: "RSA-OAEP"
            },
            key.publicKey,
            data
            );
    }).then(function (encrypted) { 
        console.log(new Uint8Array(encrypted));
    }).catch(function (err) {
        console.error(err);
    });
</script>
Run Code Online (Sandbox Code Playgroud)

Fen*_*ang 9

我找到了这个问题的原因.我必须在调用加密函数时添加哈希字段:

        return cryptoSubtle.encrypt(
            {
                name: "RSA-OAEP",
                hash: { name: "SHA-256" }
            },
            key.publicKey,
            data
            );
Run Code Online (Sandbox Code Playgroud)

这与Web Cryptography API Spec不匹配,但它可以工作.

  • 如果此处的Edge行为不符合规范,请考虑针对Edge提交错误,并在可能的情况下在此处发布指向该错误的链接. (2认同)