Internet Explorer 11中的公钥加密

Fen*_*ang 2 encryption internet-explorer public-key-encryption internet-explorer-11 webcrypto-api

我正在尝试使用IE11的JavaScript通过以下代码实现公钥加密:

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

    var genOp = cryptoSubtle.generateKey(
        {
            name: "RSA-OAEP",
            modulusLength: 2048,
            publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
            hash: { name: "SHA-256" },
        },
        true,
        ["encrypt", "decrypt"]
    );

    genOp.onerror = function (e) {
        console.error(e);
    };

    genOp.oncomplete = function (e) {
        var key = e.target.result;
        console.log(key);
        console.log(key.publicKey);

        var encOp = cryptoSubtle.encrypt(
            {
                name: "RSA-OAEP"
            },
            key.publicKey,
            data
        );

        encOp.onerror = function (e) {
            console.error(e);
        };

        encOp.oncomplete = function (e) {
            var encrypted = e.target.result;
            console.log(new Uint8Array(encrypted));
        };
    };
</script>
Run Code Online (Sandbox Code Playgroud)

它会生成密钥对,但无法通过错误事件进行加密。具有对称AES密钥的类似代码也可以工作。IE11支持公钥加密吗?我的代码有什么问题吗?

Fen*_*ang 5

我已经找到了原因。调用加密调用时,我需要添加哈希字段:

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

这与Web密码学API规范不匹配,但是可以正常工作。