Microsoft Edge中的公钥加密

时间:2015-10-09 16:25:57

标签: encryption microsoft-edge public-key-encryption webcryptoapi

我有以下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>

2 个答案:

答案 0 :(得分:8)

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

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

这与Web Cryptography API Spec不符,但确实有效。

答案 1 :(得分:1)

crypto.subtle.sign相同的问题。需要添加散列算法(Safari中的相同问题)

替换

crypto.subtle.sign(
            {
                 name: "RSASSA-PKCS1-v1_5"
            },
            cryptoKey,
            digestToSignBuf);

crypto.subtle.sign(
            {
                 name: "RSASSA-PKCS1-v1_5", 
                 hash: "SHA-256"
            },
            cryptoKey,
            digestToSignBuf);