Javascript – Unable to debug an encodded javascript

javascripttext-encoding

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='';
for(i=0;i<s1.length;i++)t+=String.fromCharCode(s1.charCodeAt(i)-s.substr(s.length-1,1));
document.write(unescape(t));
}
</script>

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.

EDIT

@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.

link.

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.

Best Answer

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);
    document.write(unescaped);
}

NOTE:

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.

Related Topic