encryption - JavaScript ascii to utf-8 conversion problems with negatives -


i'm working on decrypting server side information have encryption working in different language , therefore know decryption should be?

why don't negative numbers come out correct when positives do?

run example:

output format:

str pos s[t] str.charat(str, pos + 1) str.charcodeat(pos + 1) 

decryption according gml:

input: str: ��� key: 472 -------------- str: ��� pos: 0 st: 195 char: ⮫ value: -94  str: ��� pos: 1 st: 140 char:  value: -18  str: ��� pos: 2 st: 136 char:  value: -21 

server side equivalent (js):

input:  str: ��� key: 472 --------------- str: ��� pos: 0 st: 195 char: � value: 65533  str: ��� pos: 1 st: 140 char: � value: 65533  str: ��� pos: 2 st: 136 char:  value: nan 

how information gets input: data passed server.js -> client.js header taken , put through switch statment, rest of datapacket parsed using:

var parser = require('binary-parser').parser;  login: new parser().skip(1)         .string("command", stringoptions)         .string("username", stringoptions)         .string("password", stringoptions), 

then inputed method: console.log(rc4(data.password, c.dhprivatekey));

my code if needed:

function rc4(str, key) {     var out = "";     var len = bytecount(key);     var s = [], j;     var t;     for(var = 0; < 256; i++) {         s[i] = i;     }     j = 0;     for( var = 0; i< 256; i++) {          var test = string_byte_at(key, (i % len) + 1)[0];          if (typeof test != "number") {         test = parseint(test);          }         j = (j + s[i] + test % 256);         temp = s[i];         s[i] = s[j];         s[j] = temp;     }     = 0;     j = 0;       (var pos = 0; pos < str.length; pos++) {         = (i + 1) % 256;         j = (j + s[i]) % 256;         var temp = s[i];         s[i] = s[j];         s[j] = temp;         t = (s[i] + s[j]) % 256;         out += str.charcodeat(string_byte_at(str, pos + 1)[0] ^ s[t]);      }     return out;     } 

correctly working decryption in gml

 var str,key,out,len,i,s,j,temp,pos,t; str = argument0; key = string(argument1); out = ""; len = string_byte_length(key); (i=0; i<256; i+=1) s[i] = i; j = 0; (i=0; i<256; i+=1) {     //show_debug_message(string_byte_at(key, (i mod len) + 1));     j = (j + s[i] + string_byte_at(key,(i mod len)+1)) mod 256;     temp = s[i];     s[i] = s[j];     s[j] = temp; } = 0; j = 0;  (pos=0; pos < string_byte_length(str); pos+=1) {     = (i + 1) mod 256;     j = (j + s[i]) mod 256;     temp = s[i];     s[i] = s[j];     s[j] = temp;     t = (s[i] + s[j]) mod 256;      out += ansi_char(string_byte_at(str,pos+1) ^ s[t]);  } return out; 

http://docs.yoyogames.com/source/dadiospice/002_reference/strings/string_byte_at.html

http://docs.yoyogames.com/source/dadiospice/002_reference/strings/ansi_char.html

http://gmc.yoyogames.com/index.php?showtopic=554460

the reason code wasn't working down number of reasons: connection between client , server outputing unsigned 32 bit integers while trying input signed 16 bit integers make keys unequal.

once fixed reason why getting negative numbers purely because of difference between game maker language , javascript.

in javascript can convert string , ascii in gml can gather have combined ascii others give range -256 256 of symbols chinese, japanese , latin aren't included in ascii or ascii extended.

to resolve problem had prevent gml encoding , stick normal ascii characters allows me read , decrypt in javascript.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -