0

I have a JS function that reads variable i as is. I'd like to pass this variable encoded with ROT13, so my function would first have to decode the variable and then continue.

The problem is how to make the JS decode it and use it.

I've found a JS implementation for ROT13, but I have no clue where to include this in my function:

{{
_inst.gmlimgp=parameter_string( 1 );
_inst.gmlsp=string_pos( "i=", _inst.gmlimgp );
if ((_inst.gmlsp!=0)) {{
_inst.gmlsp+=2;
_inst.gmlimgp=string_copy( _inst.gmlimgp, _inst.gmlsp, string_length( _inst.gmlimgp ) );
g_pBuiltIn.background_index[1]=3;
background_replace( g_pBuiltIn.background_index[1], _inst.gmlimgp, 0, 0 );
_inst.set_sprite_index( (-4) );
}
;}
 else {{
show_message( "invalid parameter" );
}
;};
}
;}
kat
  • 217
  • 3
  • 12
  • This doesn't look like ROT13! `:(` – Praveen Kumar Purushothaman Jan 20 '15 at 15:58
  • 1
    I can't make any sense of how the text of your question applies to the code you've provided. There doesn't appear to be a variable named `i` read by any function (unless the `"i="` is passed to `eval` at some point? If so, that's totally unclear what what you've given here). You should clearly identify what function/line is giving you difficulty, and also show any attempts you've tried to make it work (and what errors you got, if any). – apsillers Jan 20 '15 at 16:05
  • I edited the question to make it more clear (hopefully). – kat Jan 20 '15 at 17:11

3 Answers3

2

You can use this as ROT13 in JavaScript:

ERRONEOUS 1

<script>
String.prototype.rot13 = rot13 = function(s)
 {
    return (s = (s) ? s : this).split('').map(function(_)
     {
        if (!_.match(/[A-Za-z]/)) return _;
        c = Math.floor(_.charCodeAt(0) / 97);
        k = (_.toLowerCase().charCodeAt(0) - 96) % 26 + 13;
        return String.fromCharCode(k + ((c == 0) ? 64 : 96));
     }).join('');
 };
</script>

Or shorter version:

s.replace(/[a-zA-Z]/g,function(c){return String.fromCharCode((c<="Z"?90:122)>=(c=c.charCodeAt(0)+13)?c:c-26);});

Reference: Where is my one-line implementation of rot13 in JavaScript going wrong?

To pass this variable i, you do this:

getRot13Input (i.rot13());
georgeawg
  • 48,608
  • 13
  • 72
  • 95
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • The code I have pasted reads a variable `i` as is - i.e. straight. It does not pass it, it does not encode it. I however will pass the variable `i` to the JS encoded with ROT13 and am looking for a way to make my JS first decode it and then continue as it was. – kat Jan 20 '15 at 16:06
  • 2
    I expect this was downvoted because it's not apparent that the OP has had any difficulty with building a working ROT13 implementation. It appears the problem lies in *applying* a ROT13 function to the code provided in the question. I don't understand *how* the OP wants to apply ROT13 to the code in the question, so I can't answer, but it seems likely that this answer solves a problem that is independent of the OP's problem. – apsillers Jan 20 '15 at 16:10
  • @apsillers I am still confused. But okay, may be, I will try to accept that I am wrong, until the downvoter answers. `:)` – Praveen Kumar Purushothaman Jan 20 '15 at 16:55
  • Code fails 'Bacon'.rot13() is coming out to 'Onp|{' when it should be 'Onpba'. – georgeawg Mar 31 '19 at 09:15
  • @georgeawg Ah okay. But it's been posted 4 years ago. – Praveen Kumar Purushothaman Mar 31 '19 at 12:56
  • @georgeawg Thanks for the edit. – Praveen Kumar Purushothaman Apr 01 '19 at 20:20
1

Here is an easy solution:

//This function take rot13 encoded string and decoded it as simple string.
function rot13(str) { 
  var arr = str.split('');
  var newArray = [];

 var first = {
  'A' : 'N',
  'B' : 'O',
  'C' : 'P',
  'D' : 'Q',
  'E' : 'R',
  'F' : 'S',
  'G' : 'T',
  'H' : 'U',
  'I' : 'V',
  'J' : 'W',
  'K' : 'X',
  'L' : 'Y',
  'M' : 'Z'
 };

var rest = {
'N' : 'A',
'O' : 'B',
'P' : 'C',
'Q' : 'D',
'R' : 'E',
'S' : 'F',
'T' : 'G',
'U' : 'H',
'V' : 'I',
'W' : 'J',
'X' : 'K',
'Y' : 'L',
'Z' : 'M'
};

// Iteration though the string array(arr)
for(var i = 0; i <= arr.length; i++){

  if (first.hasOwnProperty(arr[i])){       //checking first obj has the element or not

    newArray.push(first[arr[i]]);           //Pushing the element to the nerarray

  } else if(rest.hasOwnProperty(arr[i])){   //checking rest obj has the element or not

    newArray.push(rest[arr[i]]);
  } else {
    newArray.push(arr[i]);
  }
}
return newArray.join('');
}


rot13("SERR PBQR PNZC");  //call the function with rot13 encoded string
squal
  • 185
  • 3
  • 14
0

Add after

_inst.gmlimgp=string_copy( _inst.gmlimgp, _inst.gmlsp, string_length( _inst.gmlimgp ) );

but before

g_pBuiltIn.background_index[1]=3;

the line to call str_rot13 like so

_inst.gmlimgp= global.str_rot13(_inst.glomgp); 

PS i assume that you include the str_rot13 functin as a property of global object.

Konstantin Isaev
  • 642
  • 8
  • 14
  • `PS i assume that you include the str_rot13 functin as a property of global object.` - could you elaborate? – kat Jan 21 '15 at 18:07