Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using SubtleCrypto in IE 11

Tags:

javascript

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.

like image 802
PointlessSpike Avatar asked Jan 30 '23 13:01

PointlessSpike


1 Answers

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.

like image 93
PointlessSpike Avatar answered Feb 03 '23 08:02

PointlessSpike