Microsoft Edge中的公钥加密 [英] Public Key Encryption in Microsoft Edge

查看:342
本文介绍了Microsoft Edge中的公钥加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>


推荐答案

我发现这个问题的原因。调用加密函数时,我必须添加哈希字段:

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

这与 Web Cryptography API Spec ,但它有效。

这篇关于Microsoft Edge中的公钥加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆