Problem with encoded/encrypted strings

Get help using Construct 2

Post » Fri Feb 28, 2014 8:02 am

I'm using encryption in my game to communicate with a server. The response I get back is a base64 encoded string that looks like this:

Code: Select all
hAqJMf1lc4wnqtMgYhrzVCoPrE2q5c6xb7ivwqYzmKUrHFQR/hYj5l9zOrJY8DyUGrUmTyoAral9/a2m7agRkfzWB5udiqieSVAjXWXknWDEYzmWyu4uVA6ZYdFpE3gkxUq7YOq4XB3HJiN/RIT03/+BrO40maUHhhaAGbIeMGvlBNran051RVB5M7EHuaQdSUqnOSWerlaqB0xfmlBFtrLzY6PM0HeCpBC1p4tXRvtcxS7umb61TYXZ1A7/RAfS118j/9YpD3ZdBEA/sllLARoS4HeR/0k+RwQqPgN0cpWyRV+z3a5UR/GscBt89S8OcnYGQQgD11ouAsqR1bvktuHdPRKpXB7W2C4e/fLUxwp8GFyQ9v7Ss2WiXLOCziOg1Tz6sV6NdKDJe8mO0oODIKhUy8ldG3QLhKWjWdMZPSkuG2w6CB62Itc3NDaXqMEaTkSyD7340nXVPPqxXo10oB1ZZy9NgG8m1dz95ROMx0NW2Gal75K2La0GylfqJmFLS02sYp2NErgRnupVOqW6YBwtH/boeYz+wrv4iiFSa0npxyBzZrBHE24D2+soMOl42Q5ymK3biK99nWX1w8XbyCj775NsLn63vCoOjaacqXG65YjIULpXvSy31pypluxGtF3TRKrMSlG/orMJjHUc9zt47VBl6aNiVpQD3bMBcxhfTQjONeGHhMKZil7w93P9x07XVqVGG3R1m6+IOo8dxdEw3u1eOhAWO3jtUGXpo2JWlAPdswFzGE2v4JhGOs7IWxI+NisbWxl1LD9jydKIyPug/Tbxs+rt0qQJDCLvobB2xtbUVExO06MzGS/AXY7HqftLd2NCnszbg+cXm6hZbegAIZZX8+pWMnt/Leb90MEteKI9zeh9YSscVBH+FiPmX3M6sljwPJQatSZPKgCtqX39rabtqBGR/NYHm52KqJ5JUCNdZeSdYMRjOZbK7i5UDplh0WkTeCTFSrtg6rhcHccmI39EhPTfk7uVVpRy/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3gMX6zipgnWbr4g6jx3F0TDe7V46EBY7eO1QZemjYlaUA92zAXMYTa/gmEY6zshbEj42KxtbGXUsP2PJ0ojI+6D9NvGz6u3SpAkMIu+hsO9jsRzR6xutvsDhe9WI1T6p+0t3Y0KezFxoaZ3n/fE7


To clarify, that's the type of value that comes back in AJAX.LastData

So then whenever I try to use this in ExecJS in this form:
Browser.ExecJS("window.var =" & AJAX.LastData & ";")
I get the following error
Uncaught SyntaxError: Unexpected token ILLEGAL
It's likely that this is due to invisible zero-width space chars that may find themselves in the string, I'm not sure, I don't know how to save AJAX.LastData to a textfile to check.

However, if I make a textbox and set its text to AJAX.LastData, then load its text in javascript, it works. The textbox probably removes the bad characters (though a text label does not).

Is it possible to get C2 to remove these characters with a system expression? (trim does not work). I tried looking with RegExp to find them but it's poorly documented so I was not sure how. Something like trim() that removes all whitespace everywhere would probably work.
B
11
S
2
G
3
Posts: 283
Reputation: 1,968

Post » Fri Feb 28, 2014 11:06 am

Browser.ExecJS("window.var =" & AJAX.LastData & ";")

are you refering to a variable named "var" ? if so, if I am not mistaken, that is not allowed, and probably considered illegal in javascript ;)

Perhaps change it to:

Browser.ExecJS("window.MyvariableName =" & AJAX.LastData & ";")
Who dares wins
B
57
S
17
G
21
Posts: 1,878
Reputation: 19,572

Post » Fri Feb 28, 2014 11:17 am

You need to add quotes in the expression around the encoded string, otherwise js thinks it's just a long variable name.

Try this. Notice the added single quotes.
Browser.ExecJS("window.var ='" & AJAX.LastData & "';")
B
92
S
32
G
109
Posts: 5,290
Reputation: 70,991

Post » Fri Feb 28, 2014 5:10 pm

@lennaert @R0j0hound

Sorry I didn't copy/paste the actual code because I changed it to the working Textbox version I mentioned, so I introduced some red herrings on the board that are not part of the actual code.

First, the variable is not called var, that was just a dummy name, it's called window.unencryptedresponse.

Second, there are in fact quotes around the C2 data structure (either the ajax or textbox etc), here's the actual call.

Code: Select all
Browser.ExecJS("window.unencryptedresponse=encrypt ('mykeystring', atob('" & AJAX.LastData & "'), 0, 0,0, 1);  ")


encrypt is the enryption function and its various parameters, atob and btoa are functions that convert from base64 and back.

This is another call that fails for the same reason:

Code: Select all
Browser.ExecJS("window.encryptedresponse='" & AJAX.LastData & "';")


All of these actually work when using Textbox.Text instead of AJAX.LastData, (but not when using TextLabel.Text). So that makes me fairly convinced that it must be some illegal characters in there that Textbox does not support and removes, and that error message is known to come up in chrome in those zero-width-space situations.
B
11
S
2
G
3
Posts: 283
Reputation: 1,968


Return to How do I....?

Who is online

Users browsing this forum: adventurist, Blendi1223, dop2000, PROxMTswatTR and 33 guests