PointlessSpike
PointlessSpike

Reputation: 389

Using SubtleCrypto in IE 11

I'm trying to get SubtleCrypto working with IE 11. Specifically, I'm just trying to get something encrypted simply, to get me started, and I've been able to generate a key for AES-CBC but when I try to do the encryption I get an error: "Type Mismatch Error".

I have a JSFiddle: https://jsfiddle.net/tuwzsyyp/

        try {
            //Asynchronous crypto
            window.msCrypto.subtle.generateKey(
                { name: 'AES-CBC', length: 256 },
                false,
                ['encrypt']
                )
                .oncomplete = function (key) {
                    try {
                        window.msCrypto.subtle.encrypt(
                            {
                                name: "AES-CBC",
                                iv: initialisationVector
                            },
                            key, //from generateKey or importKey above
                            new Uint16Array(currentArrayBuffer) //ArrayBuffer of data you want to encrypt
                            ).oncomplete = function (encrypted) {
                                alert(3 + "; " + new Uint16Array(encrypted));
                            };
                    } catch (err) {
                        alert(err);
                    }
                };
        } catch (err) {
            alert(err);
        }

I think it's most likely that the input data is in the wrong type but the Microsoft documentation isn't clear. It says it needs to be an ArrayBufferView but as far as I can see a Uint16Array should satisfy it.

Upvotes: 4

Views: 3511

Answers (1)

PointlessSpike
PointlessSpike

Reputation: 389

I found an answer here.

It turns out that IE 11 returns an event rather than directly returning the result. As such, my example code becomes:

window.msCrypto.subtle.generateKey(
                { name: 'AES-CBC', length: 256 },
                false,
                ['encrypt']
                )
                .oncomplete = function (e) {
                    var key = e.target.result;

                    try {
                        window.msCrypto.subtle.encrypt(
                            {
                                name: "AES-CBC",
                                iv: initialisationVector
                            },
                            key, //from generateKey or importKey above
                            new Uint16Array(currentArrayBuffer) //ArrayBuffer of data you want to encrypt
                            ).oncomplete = function (e) {
                                var encrypted = e.target.result;

                                alert(3 + "; " + ab2str(encrypted));
                            };
                    } catch (err) {
                        alert(err);
                    }
                };

This is why we adhere to specifications.

Upvotes: 8

Related Questions