Public Key Encryption in Microsoft Edge

為{幸葍}努か 提交于 2019-12-23 10:59:22

问题


I have the following JavaScript code to implement public key encryption using the Web Cryptography API. It works for Firefox and Chrome but fails for Microsoft Edge. The error I am getting from Edge is "Could not complete the operation due to error 80700011." What have I missed?

<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>

回答1:


I've found the cause of this issue. I have to add the hash field when invoking the encrypt function:

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

This does not match the Web Cryptography API Spec but it works.




回答2:


Same problem with crypto.subtle.sign. Needed to add the hashing algorithm (same issue in Safari)

Replace

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

with

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


来源:https://stackoverflow.com/questions/33043091/public-key-encryption-in-microsoft-edge

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!