D4Vinci D4Vinci - 3 months ago 9
Javascript Question

Convert js function to python functions does't work

I have found an algorithm written in js (That I don't know how to code with) then I tried to convert it to python after a conversation with some friends who know js

Javascript



function crack(code) {
var N = '';
var M = '';

for(var i = 0; i < code.length; i++) {
if(i%2 == 0) {
N += code[i];
} else {
M = code[i] + M;
}
}

var key = N + M;

key = window.atob(key);
key = key.substring(2);

return key;
}





Python

import base64
def crack(code):
N = ''
M = ''
i = 0
for letter in code:
i =code.find(letter)
if i%2 == 0:
N += code[i]
else:
M =code[i] + M
key = N + M
key = base64.b64decode(key)
key = key[2:]
print key


As you can see it's the same code but the problem here that not give the same result !!

The string here to try on is:



N=m=NAobdtHRRHwaOuiU8mvcZhWddH5bZhz1MWzLapyR5nibbhG19ynccl3RBXvediCV5mjcbh2d0HubZhW1cWvLM4j8ACxONwi8


After searching for a while about window.atob found this method decodes a string of data which has been encoded by the btoa() method.

Then searched about btoa found this method uses the "A-Z", "a-z", "0-9", "+", "/" and "=" characters to encode the string.

Now what to do to get the same result with python ??

Answer

You are searching for the first occurrence of letter in the code, in the line i =code.find(letter). As you want the index, I recommend using enumerate

Result:

import base64
def crack(code):
    N = ''
    M = ''
    i = 0
    for i, letter in enumerate(code):
        if i%2 == 0:
            N += code[i]
        else:
            M =code[i] + M
    key = N + M
    key = base64.b64decode(key)
    key = key[2:]
    print key

Seems correct:

>>> crack('N=m=NAobdtHRRHwaOuiU8mvcZhWddH5bZhz1MWzLapyR5nibbhG19ynccl3RBXvediCV5mjcbh2d0HubZhW1cWvLM4j8ACxONwi8')
http://egyg33k.blogspot.com.eg/2016/08/8-malwarebytes-anti-malware.html