locked
Encrypt/Encode, AES, Passphrase, Salt, 'URLSearchParams' is undefined, ASP.net, Javascript RRS feed

  • Question

  • User1045460610 posted

    Testing code for Encoding I get the error 'URLSearchParams' is undefined on the line listed below when I view in the Internet Explorer debugger. It looks like the error is the javascript reference. Is there another reference that will work for this script?

    var params = new URLSearchParams(window.location.search),
    URLSearchParams' is undefined

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head><title>
    Encryption / Decryption Test
    </title></head>
    <body>
    HostID:
    <input type="text" id="hostId" name="hostId" />

    <h2>URL string</h2>

    <button id="encode_link" onclick="encrypt()">Encode</button>
    <br />
    <br />
    <button id="decode_link" onclick="decrypt()">Decode</button>
    Decrypt result: <span id="decrypt_result"></span>

    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"></script>
    <script type="text/javascript">
    function encrypt(hostId) {
    var now = new Date(),
    value = document.getElementById('hostId').value + '|' + now.toISOString(),
    key = 'iaUdrdMy7H';

    var encrypted = CryptoJS.AES.encrypt(value, key);
    location.href = '/?q=' + encodeURIComponent(encrypted.toString());
    }

    function decrypt() {
    var params = new URLSearchParams(window.location.search),
    queryString = params.get('q'),
    key = 'iaUdrdMy7H';

    if (queryString) {
    var decrypted = CryptoJS.AES.decrypt(queryString, key).toString(CryptoJS.enc.Utf8),
    temp = decrypted.split('|'),
    hostId = temp[0],
    timestamp = new Date(temp[1]),
    expired = diff_minutes(new Date(), timestamp) > 30;
    document.getElementById('decrypt_result').innerText = 'Host ID was post ' + hostId + ', Url ' + (expired ? 'expired' : 'not expired');
    }
    }

    function diff_minutes(dt2, dt1) {
    var diff = (dt2.getTime() - dt1.getTime()) / 1000;
    diff /= 60;
    return Math.abs(Math.round(diff));
    }
    </script>

    <!-- Visual Studio Browser Link -->
    <script type="text/javascript" src="http://localhost:56241/e78367ecef144f60b087a2fe894fddf2/browserLink" async="async" id="__browserLink_initializationData" data-requestId="f304f384b4134c4d9c46d886526f6b07" data-appName="Internet Explorer"></script>
    <!-- End Browser Link -->

    </body>
    </html>

    Thursday, May 2, 2019 6:10 PM

All replies

  • User839733648 posted

    Hi Tom4IT,

    Tom4IT

    Testing code for Encoding I get the error 'URLSearchParams' is undefined on the line listed below when I view in the Internet Explorer debugger. It looks like the error is the javascript reference. Is there another reference that will work for this script?

    var params = new URLSearchParams(window.location.search),
    URLSearchParams' is undefined

    You've got this error in IE because IE do not support the method URLSearch​Params().

    You could check the compatibility from the link:

    https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/URLSearchParams#Browser_compatibility

    There is a workaround for this, you could modify your code like:

    $.urlParam = function(name){
        var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
        if (results==null){
           return null;
        }
        else{
           return decodeURI(results[1]) || 0;
        }
    }

    For more, you could refer to this similar thread: https://stackoverflow.com/a/45760894/10487763

    Best Regards,

    Jenifer 

    Friday, May 3, 2019 2:47 AM
  • User1045460610 posted

    Thanks, do you know if there is a compatibility chart for this method or if it will work for all browsers?

    Friday, May 3, 2019 1:41 PM
  • User1045460610 posted

    also, that code that i have renders like this http://localhost:49387/?q=U2FsdGVkX1%2FndUx%2BqWB6K0rMm1c8HPg%2BMNwCszxaKTe1%2FRcS%2F7LyvMQ3Q%2BHJNAhQ . Should I encode the whole string or just the part after UserID. https://attendance.erpise.com/instructorcourse.aspx?UserID=126186

    Friday, May 3, 2019 1:52 PM