3

I am trying to implement public key encryption using JavaScript for IE11 with the following code:

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

It generates the key pair but fails to do the encryption with an error event. Similar code with a symmetric AES key works. Is public key encryption supported by IE11? Is there anything wrong with my code?

FengHuang
  • 241
  • 1
  • 8
  • 1
    How is this question different than [this one](http://stackoverflow.com/questions/33043091/public-key-encryption-in-microsoft-edge)? – President James K. Polk Oct 09 '15 at 22:34
  • @JamesKPolk This one is about IE11 and that other one is about Edge. Arguably they could have been munged into one question, but then there’s the 5-tag limit and other concerns. So I an understand the OP experimenting with posting separate questions for each browser. – sideshowbarker Oct 10 '15 at 01:35
  • 1
    @JamesKPolk The API for IE11 is different from Edge. So the code is different. – FengHuang Oct 10 '15 at 08:19

1 Answers1

5

I've found out the cause of this. I need to add the hash field when invoking the encrypt call:

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

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

FengHuang
  • 241
  • 1
  • 8
  • If the IE behavior here is not per-spec, please consider filing a bug against IE, and posting a link to that bug here if possible. – sideshowbarker Oct 17 '15 at 00:09