sixBitEncode

Discussion in 'Mixed Languages' started by sebus, Feb 1, 2014.

  1. sebus

    sebus MDL Guru

    Jul 23, 2008
    6,356
    2,026
    210
    #1 sebus, Feb 1, 2014
    Last edited by a moderator: Apr 20, 2017
    Anybody could convert the encode function to Python?

    It comes from mPlayer - Flash MP3 Player with URL encryption

    Code:
    // sixBitEncode
    // released 10.10.03 10a
    // by zeh @ PROTOTYPE (http://proto.layer51.com/)
    // License: Copyleft (http://www.gnu.org/copyleft/copyleft.html)
    
    
    String.prototype.sixBitEncode = function () {
    var ntexto = "";
    var nntexto = "";
    var codeKey = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-"
    var charCode, charCodeBin, charChar;
    for (i=0; i< this.length; i++) {
    charCode = this.charCodeAt(i) & 255; // char code
    charCodeBin = ("00000000" + charCode.toString(2)).substr(-8,8); // char code in binary
    ntexto += charCodeBin;
    }
    for (i=0; i< ntexto.length; i+=6) {
    charCodeBin = ntexto.substr(i, 6); // char code in binary, 6 bits
    if (charCodeBin.length < 6) charCodeBin = (charCodeBin+"000000").substr(0,6);
    charCode = parseInt(charCodeBin, 2);
    nntexto += codeKey.substr(charCode, 1);
    }
    return (nntexto);
    }
    
    String.prototype.sixBitDecode = function () {
    var ntexto = "";
    var nntexto = "";
    var codeKey = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-"
    var charCode, charCodeBin;
    for (i=0; i<this.length; i++) {
    charCode = codeKey.indexOf(this.substr(i,1)); // char index
    charCodeBin = ("000000" + charCode.toString(2)).substr(-6,6); // char index in binary, 6 bits
    ntexto += charCodeBin;
    }
    for (i=0; i< ntexto.length; i+=8) {
    charCodeBin = ntexto.substr(i, 8); // char code in binary
    charCode = parseInt(charCodeBin, 2);
    charChar = String.fromCharCode(charCode);
    nntexto += charChar;
    }
    return (nntexto);
    }
    
    Decode function (while not very elegant, but working) is available from here

    Code:
    def int2bin(n):
        assert n >= 0, "Number must be positive"
        res = ""
        if n == 0: return "0"
        while True:
            if n == 0:
                return res
            elif n % 2 == 0:
                res = "0" + res
            else:
                res = "1" + res
                n -= 1
            n /= 2
    
    def decode(source):
        key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-"
        binary = ""
    
        for char in source:
            code = key.find(char) # look up each char in the code key
            binary += int2bin(code).rjust(6, "0") # append char index as a six bit binary string
        
        chunks = [] 
        # split string into segments of length 8
        while len(binary) > 0:
            chunks.append(binary[:8]) 
            binary = binary[8:]
    
        chars = [chr(int(x, 2)) for x in chunks] # convert the segments to chars
        return "".join(chars)
    
    # http://www.mabd.se/?p=682
    test = "aHR0cDovL3N0cmVhbTAuZWFyLmR1b21pLmNvbS9SeWx5bm4ubXAzA"
    print(test, decode(test))
    
    The player is used in various places ie ILoveMusic

    Original Actionscript is here

    Thanks

    sebus