Reputation: 77
This is the java script code it's a decoder with 2 functions:
getOffset
, and the main function asdf
.
I want to decode this string: LqMWJQzZYUWJQpEcYGII26XombQQoqnQQQQoqnqgoqnQQQQoqnQQQQoqnQQQQoqnqgoqnQQQQoqnQQuuuQQoqnQQQQoqnxioqnQQQQoqnQQMJCSVcCgnQQoqnQQQQoqnxioqnQQQQoqnQQniaQQoqnQQQQoqnqgoqnQQQQoqnQQWJQzhYAUoVMTAdAT33nvX3nB
To this :
/ShowUrl-a_partnerKey.1-a_url.http%253A__5F____5F__2F__5F____5F____5F____5F__2F__5F____5F__www__5F____5F__2E__5F____5F__danielnyc__5F____5F__2E__5F____5F__com__5F____5F__2F__5F____5F__-a_urlKey.915edf949f66c826c.html
I want to convert this (working) javascript code to python:
var d = "LqMWJQzZYUWJQpEcYGII26XombQQoqnQQQQoqnqgoqnQQQQoqnQQQQoqnQQQQoqnqgoqnQQQQoqnQQuuuQQoqnQQQQoqnxioqnQQQQoqnQQMJCSVcCgnQQoqnQQQQoqnxioqnQQQQoqnQQniaQQoqnQQQQoqnqgoqnQQQQoqnQQWJQzhYAUoVMTAdAT33nvX3nB"
function asdf(d) {
var h = {
"": ["&", "=", "p", "6", "?", "H", "%", "B", ".com", "k", "9", ".html", "n", "M", "r", "www.", "h", "b", "t", "a", "0", "/", "d", "O", "j", "http://", "_", "L", "i", "f", "1", "e", "-", "2", ".", "N", "m", "A", "l", "4", "R", "C", "y", "S", "o", "+", "7", "I", "3", "c", "5", "u", 0, "T", "v", "s", "w", "8", "P", 0, "g", 0],
q: [0, "__3F__", 0, "Photos", 0, "https://", ".edu", "*", "Y", ">", 0, 0, 0, 0, 0, 0, "`", "__2D__", "X", "<", "slot", 0, "ShowUrl", "Owners", 0, "[", "q", 0, "MemberProfile", 0, "ShowUserReviews", '"', "Hotel", 0, 0, "Expedia", "Vacation", "Discount", 0, "UserReview", "Thumbnail", 0, "__2F__", "Inspiration", "V", "Map", ":", "@", 0, "F", "help", 0, 0, "Rental", 0, "Picture", 0, 0, 0, "hotels", 0, "ftp://"],
x: [0, 0, "J", 0, 0, "Z", 0, 0, 0, ";", 0, "Text", 0, "(", "x", "GenericAds", "U", 0, "careers", 0, 0, 0, "D", 0, "members", "Search", 0, 0, 0, "Post", 0, 0, 0, "Q", 0, "$", 0, "K", 0, "W", 0, "Reviews", 0, ",", "__2E__", 0, 0, 0, 0, 0, 0, 0, "{", "}", 0, "Cheap", ")", 0, 0, 0, "#", ".org"],
z: [0, "Hotels", 0, 0, "Icon", 0, 0, 0, 0, ".net", 0, 0, "z", 0, 0, "pages", 0, "geo", 0, 0, 0, "cnt", "~", 0, 0, "]", "|", 0, "tripadvisor", "Images", "BookingBuddy", 0, "Commerce", 0, 0, "partnerKey", 0, "area", 0, "Deals", "from", "//", 0, "urlKey", 0, "'", 0, "WeatherUnderground", 0, "MemberSign", "Maps", 0, "matchID", "Packages", "E", "Amenities", "Travel", ".htm", 0, "!", "^", "G"]
};
var b = "";
for (var a = 0; a < d.length; a++) {
var j = d.charAt(a);
var f = j;
if (h[j] && a + 1 < d.length) {
a++;
f += d.charAt(a)
} else {
j = ""
}
var g = getOffset(d.charCodeAt(a));
if (g < 0 || typeof h[j][g] == "String") {
b += f
} else {
b += h[j][g]
}
}
return b}
function getOffset(a) {
if (a >= 97 && a <= 122) {
return a - 61
}
if (a >= 65 && a <= 90) {
return a - 55
}
if (a >= 48 && a <= 71) {
return a - 48
}
return -1}
asdf(d)
It returns (as expected):
/ShowUrl-a_partnerKey.1-a_url.http%253A__5F____5F__2F__5F____5F____5F____5F__2F__5F____5F__www__5F____5F__2E__5F____5F__danielnyc__5F____5F__2E__5F____5F__com__5F____5F__2F__5F____5F__-a_urlKey.915edf949f66c826c.html
This is the Python code that I've tried :
d = "LqMWJQzZYUWJQpEcYGII26XombQQoqnQQQQoqnqgoqnQQQQoqnQQQQoqnQQQQoqnqgoqnQQQQoqnQQuuuQQoqnQQQQoqnxioqnQQQQoqnQQMJCSVcCgnQQoqnQQQQoqnxioqnQQQQoqnQQniaQQoqnQQQQoqnqgoqnQQQQoqnQQWJQzhYAUoVMTAdAT33nvX3nB"
def asdf(d):
h = {
"" : ["&", "=", "p", "6", "?", "H", "%", "B", ".com", "k", "9", ".html", "n", "M", "r", "www.", "h", "b", "t", "a", "0", "/", "d", "O", "j", "http://", "_", "L", "i", "f", "1", "e", "-", "2", ".", "N", "m", "A", "l", "4", "R", "C", "y", "S", "o", "+", "7", "I", "3", "c", "5", "u", 0, "T", "v", "s", "w", "8", "P", 0, "g", 0],
"q": [0, "__3F__", 0, "Photos", 0, "https://", ".edu", "*", "Y", ">", 0, 0, 0, 0, 0, 0, "`", "__2D__", "X", "<", "slot", 0, "ShowUrl", "Owners", 0, "[", "q", 0, "MemberProfile", 0, "ShowUserReviews", '"', "Hotel", 0, 0, "Expedia", "Vacation", "Discount", 0, "UserReview", "Thumbnail", 0, "__2F__", "Inspiration", "V", "Map", ":", "@", 0, "F", "help", 0, 0, "Rental", 0, "Picture", 0, 0, 0, "hotels", 0, "ftp://"],
"x": [0, 0, "J", 0, 0, "Z", 0, 0, 0, ";", 0, "Text", 0, "(", "x", "GenericAds", "U", 0, "careers", 0, 0, 0, "D", 0, "members", "Search", 0, 0, 0, "Post", 0, 0, 0, "Q", 0, "$", 0, "K", 0, "W", 0, "Reviews", 0, ",", "__2E__", 0, 0, 0, 0, 0, 0, 0, "{", "}", 0, "Cheap", ")", 0, 0, 0, "#", ".org"],
"z": [0, "Hotels", 0, 0, "Icon", 0, 0, 0, 0, ".net", 0, 0, "z", 0, 0, "pages", 0, "geo", 0, 0, 0, "cnt", "~", 0, 0, "]", "|", 0, "tripadvisor", "Images", "BookingBuddy", 0, "Commerce", 0, 0, "partnerKey", 0, "area", 0, "Deals", "from", "//", 0, "urlKey", 0, "'", 0, "WeatherUnderground", 0, "MemberSign", "Maps", 0, "matchID", "Packages", "E", "Amenities", "Travel", ".htm", 0, "!", "^", "G"]
}
b = ""
for a in range(len(d)):
j=d[a]
f=j
list =[]
for key in h:
list.append(key)
if j in list and a+1 < len(d):
a=a+1
f = f+d[a]
else:
j = ""
g = getOffset(ord(d[a]))
if g < 0 or type(h[j][g]) is str:
b = b + f
else:
b = b+str(h[j][g])
print b
return b
def getOffset(a):
if(a >= 97 and a <= 122):
return(a-61)
if(a >= 65 and a <= 90):
return(a-55)
if(a >= 48 and a <=71):
return(a-48)
return(-1)
print asdf(d)
But it gives me back the undecoded string: LqMWJQzZYUWJQpEcYGII26XombQQoqnQQQQoqnqgoqnQQQQoqnQQQQoqnQQQQoqnqgoqnQQQQoqnQQuuuQQoqnQQQQoqnxioqnQQQQoqnQQMJCSVcCgnQQoqnQQQQoqnxioqnQQQQoqnQQniaQQoqnQQQQoqnqgoqnQQQQoqnQQWJQzhYAUoVMTAdAT33nvX3nB
What's wrong with my code ? Why don't I get the same results as the javascript code ?
Upvotes: 1
Views: 5241
Reputation: 17900
Actually, your Javascript code has an Error - if the error is fixed, then your JS code breaks, returning the original string, just as your Python code does.
if (g < 0 || typeof h[j][g] == "String")
is NOT behaving as you expected:
typeof h[j][g] == "String")
should be
if (g < 0 || typeof h[j][g] == "string")
(note uncapitalized string).
In fact the mistaken typeof h[j][g] == "String")
, is actually the equivalent of
if (g < 0) {...
If you change that line in both versions of your code, you get the desired results in JS, and ALMOST the desired results in Python (which has another error).
Javascript version
Change :
var g = getOffset(d.charCodeAt(a));
if (g < 0 || typeof h[j][g] == "String") { #THIS LINE IS INCORRECT
b += f
} else {
b += h[j][g]
}
To:
var g = getOffset(d.charCodeAt(a));
if (g < 0){ # CHANGE THIS LINE
b += f
} else {
b += h[j][g]
}
JS Result:
/ShowUrl-a_partnerKey.1-a_url.http%253A__5F____5F__2F__5F____5F____5F____5F__2F__5F____5F__www__5F____5F__2E__5F____5F__danielnyc__5F____5F__2E__5F____5F__com__5F____5F__2F__5F____5F__-a_urlKey.915edf949f66c826c.html
Python version
Change:
g = getOffset(ord(d[a]))
if g < 0 or type(h[j][g]) is str: # THIS LINE IS INCORRECT
b = b + f
else:
b = b+str(h[j][g])
print b # THIS LINE SHOULD BE REMOVED
To:
g = getOffset(ord(d[a])) # CHANGED THIS LINE
if g < 0:
b = b + f
else:
b = b+str(h[j][g])
#print b # REMOVED THIS LINE
Results in:
/ShowUrld-a_partnerKeyN.1-a_url.http%253A__5Fc____5Fc__2F__y5Fc____5Fc____5Fc____5Fc__2F__y5Fc____5Fc__www__5Fc____5Fc__2E__o5Fc____5Fc__danielnyc__5Fc____5Fc__2E__o5Fc____5Fc__com__5Fc____5Fc__2F__y5Fc____5Fc__-a_urlKeyS.915edf949f66c826c.html
Here are links to final code in:
Final PYTHON Fix
Now, You have another Python Error..
When using for a in ..
construct, you cannot internally change the value of a
, as in the line: a = a + 1
.
Well, you can, but that will lead to unexpected results.
Each time the for a in..
top of the loop is reached, a
will increment to the next value it would have had, regardless of what value you temporarily changed it to within the loop !!
Hence, (2, 'M'), (7, 'Z'), ...
were not skipped, and you had intended!
This is why the Python result I showed above has a few more characters than you expected.
See these SO answers for great explanations and examples, and python docs here, or this closed SO question.
The easiest way to convert your code in this case is to simply use a while
loop instead of a for in
loop. This way, the your inner logic remains essentially the same for both languages.
CHANGE:
#for a in range(len(d)): ## Remove and Change this to a WHILE loop as below
a = 0 # Manually initialize your loop
print(len(d))
while a < len(d): # Use WHILE loop instead
j = d[a]
...
AND..
...
g = getOffset(ord(d[a]))
if g < 0 : # Remember to UPDATE This Line (Remove #or type(h[j][g]) is str:)
b = b + f
else:
b = b + str(h[j][g])
#print b # REMOVE this line
a = a + 1 # Manually increment your WHILE loop
return b
FINAL PYTHON CODE on REPL.IT )
FINAL PYTHON RESULT:
/ShowUrl-a_partnerKey.1-a_url.http%253A__5F____5F__2F__5F____5F____5F____5F__2F__5F____5F__www__5F____5F__2E__5F____5F__danielnyc__5F____5F__2E__5F____5F__com__5F____5F__2F__5F____5F__-a_urlKey.915edf949f66c826c.html
Upvotes: 1