0

I have this simple decrypt function in JavaScript:

e.decrypt = function(t) {
            var n, i = "", o = t.length, r = 0, s = 0;
            for (r = 0; o > r; r++) {
                for (n = t.substr(r, 1),
                s = 0; s < e.MAP_LEN; s++) {
                    if (":" == n && ":|~" == t.substr(r, 3)) {
                        n = "\n",
                        r += 2;
                        break
                    }
                    if (n == e.charMap[s][1]) {
                        n = e.charMap[s][0];
                        break
                    }
                }
                i += n
            }
            return i
        }
        ,
        e.MAP_LEN = 64,
        e.charMap = [["A", "d"], ["B", "e"], ["C", "f"], ["D", "g"], ["E", "h"], ["F", "i"], ["G", "j"], ["H", "k"], ["I", "l"], ["J", "m"], ["K", "n"], ["L", "o"], ["M", "p"], ["N", "q"], ["O", "r"], ["P", "s"], ["Q", "t"], ["R", "u"], ["S", "v"], ["T", "w"], ["U", "x"], ["V", "y"], ["W", "z"], ["X", "a"], ["Y", "b"], ["Z", "c"], ["a", "Q"], ["b", "R"], ["c", "S"], ["d", "T"], ["e", "U"], ["f", "V"], ["g", "W"], ["h", "X"], ["i", "Y"], ["j", "Z"], ["k", "A"], ["l", "B"], ["m", "C"], ["n", "D"], ["o", "E"], ["p", "F"], ["q", "0"], ["r", "1"], ["s", "2"], ["t", "3"], ["u", "4"], ["v", "5"], ["w", "6"], ["x", "7"], ["y", "8"], ["z", "9"], ["0", "G"], ["1", "H"], ["2", "I"], ["3", "J"], ["4", "K"], ["5", "L"], ["6", "M"], ["7", "N"], ["8", "O"], ["9", "P"], ["\n", ":|~"], ["\r", ""]],
        e

Then, this is the Python version that I've made:

def decryptToken(t):
    n = ""
    i = ""
    o = len(t)
    r = 0
    s = 0
    MAP_LEN = 64
    charMap = [["A", "d"], ["B", "e"], ["C", "f"], ["D", "g"], ["E", "h"], ["F", "i"], ["G", "j"], ["H", "k"], ["I", "l"], ["J", "m"], ["K", "n"], ["L", "o"], ["M", "p"], ["N", "q"], ["O", "r"], ["P", "s"], ["Q", "t"], ["R", "u"], ["S", "v"], ["T", "w"], ["U", "x"], ["V", "y"], ["W", "z"], ["X", "a"], ["Y", "b"], ["Z", "c"], ["a", "Q"], ["b", "R"], ["c", "S"], ["d", "T"], ["e", "U"], ["f", "V"], ["g", "W"], ["h", "X"], ["i", "Y"], ["j", "Z"], ["k", "A"], ["l", "B"], ["m", "C"], ["n", "D"], ["o", "E"], ["p", "F"], ["q", "0"], ["r", "1"], ["s", "2"], ["t", "3"], ["u", "4"], ["v", "5"], ["w", "6"], ["x", "7"], ["y", "8"], ["z", "9"], ["0", "G"], ["1", "H"], ["2", "I"], ["3", "J"], ["4", "K"], ["5", "L"], ["6", "M"], ["7", "N"], ["8", "O"], ["9", "P"], ["\n", ":|~"], ["\r", ""]]

    for r in range (0,o):
        for s in range(0,MAP_LEN):
            n = t[r:1]
            if ":" == n and ":|~" == t[r:3]:
                n = "\n"
                r = r + 2
                break
            if n == charMap[s][1]:
                n = charMap[s][0]
                break
        i=i+n
    return i

If you test with this token PEqVaW==.VdDjVbbgQU6y3fBGWfn7UDKmOXFUsLK9e5byiPSULUd= with the Javascript version, you should get this result:

9oNfXg==.fAnGfYYDaewVtCl0gCKxen4J8hpeP54zBvYVF9ce5eA=

But in Python this the output:

9

What am I doing wrong?

k1dev
  • 631
  • 5
  • 14
  • Have you thought about stepping through the code in a debugger to see what happens? – Some programmer dude Mar 04 '20 at 10:54
  • `n = t[r:1]` is in the wrong place in the Python code, and presumably you want `t[r]` for just one character - `t[r:1]` doesn't usually give you a string of length 1. – kaya3 Mar 04 '20 at 10:55
  • 1
    Seems like a type conversion problem. This is very common in python since there a no type definitions needed. As you can see in the post before, just frint out the result after every step and reproduce where the error occourse. – J. Doe Mar 04 '20 at 10:56
  • Yes, @kaya3 you were right. t[r] did the trick! Would you make an answer technically explaining it? – k1dev Mar 04 '20 at 10:59

0 Answers0