简体   繁体   中英

Python - Javascript encrypt function for a known decrypt function

I found a streaming website that encrypts the iframe code with an interesting Javascript function. On the webpage is visible the decrypt function (obviously) but not the encryption one. This is the function:

function base64_decode(data) {
    var b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
    var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
        ac = 0,
        dec = '',
        tmp_arr = [];
    if (!data) {
        return data;
    }
    data += '';
    do {
        h1 = b64.indexOf(data.charAt(i++));
        h2 = b64.indexOf(data.charAt(i++));
        h3 = b64.indexOf(data.charAt(i++));
        h4 = b64.indexOf(data.charAt(i++));
        bits = h1 << 18 | h2 << 12 | h3 << 6 | h4;
        o1 = bits >> 16 & 0xff;
        o2 = bits >> 8 & 0xff;
        o3 = bits & 0xff;
        if (h3 == 64) {
            tmp_arr[ac++] = String.fromCharCode(o1);
        } else if (h4 == 64) {
            tmp_arr[ac++] = String.fromCharCode(o1, o2);
        } else {
            tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
        }
    } while (i < data.length);
    dec = tmp_arr.join('');
    return dec.replace(/\0+$/, '');
}

function ord(string) {
    var str = string + '',
        code = str.charCodeAt(0);
    if (0xD800 <= code && code <= 0xDBFF) {
        var hi = code;
        if (str.length === 1) {
            return code;
        }
        var low = str.charCodeAt(1);
        return ((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000;
    }
    if (0xDC00 <= code && code <= 0xDFFF) {
        return code;
    }
    return code;
}

function decrypt(sData, sKey) {
    var sResult = "";
    sData = base64_decode(sData);
    var i = 0;
    for (i = 0; i < sData.length; i++) {
        var sChar = sData.substr(i, 1);
        var sKeyChar = sKey.substr(i % sKey.length - 1, 1);
        sChar = Math.floor(ord(sChar) - ord(sKeyChar));
        sChar = String.fromCharCode(sChar);
        sResult = sResult + sChar;
    }
    return sResult;
}

So this code:

decrypt('s+Dd6djk3Jfq6dq0md/r6+fqsaam5ufc5ePm2Nul2uam3OTZ3Numy83Zw87aqazMvbimmZfq2unm4+Pg5d60meXmmZfd6djk3Nnm6dvc6bSZp5mX7uDb69+0mainp5yZl9/c4N7f67SZqKennJmX2OPj5u7d7OPj6trp3NzltJnr6ezcmZfu3Nni4OvY4+Pm7t3s4+Pq2unc3OW0mevp7NyZl+Tm8djj4+bu3ezj4+ra6dzc5bSZ6+ns3Jm1s6bg3enY5Ny1', 'w')

will return:

<iframe src="https://openload.co/embed/TVbLWc25UFA/" scrolling="no" frameborder="0" width="100%" height="100%" allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true"></iframe>

I translated the decrypt function in Python with math and base64 modules and it works well, but now I need the encrypt function (in Python) that starting from a string outputs encrypted string + key. Is it some kind of known encryption?

This seems to be a bad implementation of the Vigenère cipher, without the modulus as the resulting sChar may have higher values. It simply adds the value of a key character to each plain character, reusing the key if it is depleted. It will mainly function as something to confuse virus-scanners or packet-inspecting firewalls as the encryption itself is of course completely insecure. It won't have a name (and no self-respecting cryptographer will lend his name for it either).

There seems to be a bug in the code here:

sKey.substr(i % sKey.length - 1, 1);

I'm not sure why the - 1 is required or how this plays out in practice (this is why languages and API's should be strict in what is acceptable).

ord seems to have been implemented to avoid issues with 16-bit Unicode characters.

base64_decode simply implements base 64 decoding, nothing to see there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM