I’m having some problems debugging an encoded javacscript. This script I’m referring to given in this link over here.

The encoding here is simple and it works by shifting the unicodes values to whatever Codekey was use during encoding. The code that does the decoding is given here in plain English below:-

<script language="javascript">
function dF(s){
var s1=unescape(s.substr(0,s.length-1)); var t='';

I’m interested in knowing or understanding the values (e.g s1,t). Like for example when the value of i=0 what values would the following attributes / method would hold

    s1.charCodeAt(i) and s.substr(s.length-1,1)

The reason I’m doing this is to understand as to how a CodeKey function really works. I don’t see anything in the code above which tells it to decode on the basis of codekey value. The only thing I can point in the encoding text is the last character which is set to 1 , 2 ,3 or 4 depending upon the codekey selected during encoding process. One can verify using the link I have given above.

However, to debug, I’m using firebug addon with the script running as localhost on my wamp server. I’m able to put a breakpoint on the js using firebug but I’m unable to retrieve any of the user defined parameters or functions I mentioned above.

I want to know under this context what would be best way to debug this encoded js.


@blueberryfields Thanks for the neat code review.

However, to clarify this is no homework its something i picked from a website about encoding and javascript.The material just looked interesting and decided to give it a go. I don't see the point of using the intermediate variables as I was hoping to make use of those already define (s1,t,i). Usually these variable types are seen in firebug way too often like the enumerable types. Beside, using a good breakpoint and the right place i can always step over these values in the loop.

I changed my focus as someone on stackexchange told me to use dragonfly (opera) as i did I was able to retrieve the variable and their values with the breakpoint statement. For other values i just did document.write to get desired results. Here is the link of the screenshot.


I was more interested in understanding the part of coding that actually tell the program to shift-back the unicode character based upon code key value. That part of code was s.substr(s.length-1,1). He just extracted the last character which is the codekey number and then use it in calculating the matching charcode value.

If you unescape this shift-1 code %264DTDSJQU%2631MBOHVBHF%264E%2633kbwbtdsjqu%2633%264F%261Bbmfsu%2639%2638Ifmmp%2631Xpsme%2638%263%3A%264C%261B%264D0TDSJQU%264F%261B%261%3A%261%3A%261%3A1 you would get &4DTDSJQU&31MBOHVBHF&4E&33kbwbtdsjqu&33&4F&1Bbmfsu&39&38Ifmmp&31Xpsme&38&3:&4C&1B&4D0TDSJQU&4F&1B&1:&1:&1:1 Although those last chars not required but were intentionally added so it helps in decoding.

2 Answers 2


To figure out how this code works, I would recommend expanding it, and assigning intermediate local variables to store various results, so you can watch how they change as you step through.

Before assuming that you're looking at you're looking at user defined parameters or functions, you might want to look the functions up in a standard library. If you do look them up, you'll notice all the functions used are standard library functions for Javascript.

For example, with the code you've got, I'd do something like this:

function dF(s) {
    var s1 = unescape(s.substr(0, s.length - 1));
    var t = '';
    for (i = 0; i < s1.length; i++) {
        var cCode = s1.charCodeAt(i);
        var key = s.substr(s.length - 1, 1);
        var temp= String.fromCharCode( cCode - key );
        t += temp;
    var unescaped = unescape(t);


This question smells a lot like a homework question. It's generally frowned upon to ask us to do your homework for you, and it's usually good form to at least warn that it's homework you're doing.


Lets make this simpler:

(function (s){
    var s1=unescape(s.substr(0,s.length-1));
    var t='';
    return unescape(t);
})( [
        'var q = 1;0' // here is a little hack, the generator never outputs this code
    ][1] // here you can change the index (which is also the shift amount)


"var q = 1;"

As you can see its a Caesar cipher, probably the first cipher that kids learn in school.

  • i'll let you find the shift amount (and its limits) yourself, as its stored in the code in the most visible place, without any attempts to hide it.
    – c69
    Commented Oct 27, 2012 at 5:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.