Search code examples
encryptioncryptographyinternet-explorer-11encryption-asymmetric

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


Getting "invalid argument" error on trying to import public key in IE 11 using window.msCrypto.subtle.importKey method while same methods works fine in Edge and chrome

Below is the input payload for import method,

    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"]);

Is there anything we need to change to make it work for IE11?


Solution

  • You could use Web Cryptography API shim to make it work in IE. I install the package using the following command:

    npm i webcrypto-shim
    npm i promiz
    

    Then link scripts into the html code:

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

    After that, the scripts will run well in IE 11.