Apr 01 2009

3

Javascript Obfuscation

Category: Javascript

In recent weeks my plate has been full as I plan for a big move. When I get stressed I usually play a game of chess to help relax a bit. Last night I took a different approach. I created my own brain teaser. I haven’t played golf with the following code yet, so the code isn’t optimized and it could be greatly reduced. The code is in pure javascript, no libraries, but it does require a decent javascript engine to handle forEach, map etc. Sorry IE.

(function() {
    var r = function(a) {
        if (a == undefined) return t;
        isNaN(a) ? (function(){
        t = [a.match(/.{5}/g).map(function(p) {
             return parseInt(p, 33).toString(10);
         	}).join('').match(/.{6}/g).map(function(p) {
             	return parseInt(p, 7).toString(17);
         	}).join('').match(/.{4}/g).reverse().map(function(p) {
             	var a = p.split('');
             	return a[1] + a[3] + a[0] + a[2];
         	}).join('')].map(function(p) {
             	var a = p.match(/.{2}/g);
             	a.forEach(function(c, i) { b[i % 3].push(c); });
             	return b[2].join('') + b[0].join('') + b[1].join('');
         	})[0].match(/.{2}/g).map(function(p) {
             	var x = p.split('');
             	return String.fromCharCode(parseInt(x[1] + x[0], 17).toString(10));
         	}).join('');
        })() : (function(){
            d = a.toString().split(''); l = d.length; p = t.split('');
            if (l == 3) { d.forEach(function(i) { p.push(p[i]); }); t = p.join(''); }
	    	else { t = d.map(function(i){ return p[i];}).join('') + p.join(''); }
        })();
        return arguments.callee;
    }, t = '', a, p, b = [[], [], []],c,i,d,l;
    return r;
})()('sbuq1iunojjo8q7')(165)(12)();

Spoiler:


'sbuq1iunojjo8q7' // starting point base 33

split into 3 => sbuq1 iunoj jo8q7

33634624 22450546 23404564 // merge into single string

// create an array of two parts each

336346 242245 054623 404564 // base 7
c326    8g65    2d63    e067 // base 17

use shift:

    1 => 7,3,5,6
    3 => 6,6,6,2
    0 => 0,d,g,3
    2 => e,2,8,c

rows 1,3,0,2 (scrambling)

translate to =>

    0 7 e 6 d 3 2 6 g 5 8 6 3 6 c 2
    0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 // index from above

    07,e6,d3,26,g5,86,36,c2 => 07e6d326g58636c2

    07, e6, d3, 26, g5, 86, 36, c2

    0   1   2   3   4   5   6   7
    d3, 86, 07, 26, 36, e6, g5, c2  // pseudo reverse using % 3 (see below)

    0 => 07, 26, 36
    1 => e6, g5, c2
    2 => d3, 86

    i%3 = (0 => 0,3,6 ), (1 => 1,4,7), (2 => 2,5)

    2, 0, 1

    d3 86 07 26 36 e6 g5 c2

    //reverse each 2 characters
    0  1  2  3  4  5  6  7  // index
    3d 68 70 62 63 6e 5g 2c //base 17
    @  n  w  h  i  t  e  .
    // convert to base 10

    // convert to ascii

    // grab 1,6,5 and push
    // grab 1,2 and shift
    // done! final output my email address.

3 Responses to “Javascript Obfuscation”

  1. olivier says:

    this great, but where is the other half? you know, its server side cousin. I want it!

  2. nwhite says:

    No server side scripts. This was hand baked for my own amusement. This solution would probably need some adjustments to be used in more generic cases. With all the base conversion I am sure there are issues that would arise with different strings. I plan on making the code a lot smaller, the idea was to have it as my email signature but it kinda grew out of control. When I get around to optimizing it I will see if I can make a code generator.

  3. Guillermo Rauch says:

    Just use rot13 and call it a day :D

Leave a Reply