MsCrypto 导入密钥方法在 IE11 中失败并出现 "invalid arguement" 错误

MsCrypto Import key method fails in IE11 with "invalid arguement" error

尝试使用 window.msCrypto.subtle.importKey 方法在 IE 11 中导入 public 键时出现 "invalid argument" 错误,而相同的方法在 Edge 和 chrome

中工作正常

下面是导入方法的输入载荷,

    var jwk_base64 = publicKey.replace(/\+/g, '-').replace(/\//g,   '_').replace(/\=+$/, '');

    var cryptoVar= window.crypto || window.msCrypto;
    var cryptoSubtle = cryptoVar.subtle;
    let importOp = cryptoSubtle.importKey(
        "jwk",
        {
            kty: "RSA",
            e: "AQAB",
            n: jwk_base64,
            alg: "RSA-OAEP-256",
            ext: true,
        },
        { name: "RSA-OAEP", hash: { name: "sha-256" } },
        false,
        ["encrypt"]);

是否需要进行任何更改才能使其适用于 IE11?

您可以使用 Web Cryptography API shim 使其在 IE 中工作。我使用以下命令安装软件包:

npm i webcrypto-shim
npm i promiz

然后link脚本进入html代码:

<script src="node_modules/promiz/promiz.js"></script>
<script src="node_modules/webcrypto-shim/webcrypto-shim.js"></script>

之后,脚本将 运行 在 IE 11 中运行良好。