UP | HOME

Online tools for obfuscation - Some Javascript functions

Table of Contents

String encodings

Here is a list of string encodings that can be used for obfuscation. See the source for Javascript code.

Conversions from characters to numbers

Convert all characters (including unreserved characters) of the word into their codes, given some base and some prefix separator.

Examples:

  • percent encoding: base 16, prefix %

    ab%61%62

  • numeric character reference: base 10, prefix &#

    ab&#97&#98

Base: Prefix separator: Word:

Mirror

Reverse the word.

Example

  • abba
Word:

Caesar cipher

Replace each character with another one after a constant shift in the given alphabet.

Example

  • ROT13 function: shift of 13 letters over the alphabet of 26 letters (an involution or self-inverse)

    abno

Variant of the ROT13 function replacing letters (capital or small) by using a customizable shift and letting other characters invariant

Shift: Word:

Vigenère cipher

Replace each character with another one after a variable shift in the given alphabet. The variations of the shift are given by a key.

Example

The alphabet is defined as a segment of the ASCII table, starting at a given first character (ASCII code x between 33 and 126) and with a given size (between 1 and 127 - x). Default values are 33 and 94.

First character (ascii code): Size of the alphabet: Key: Word:

Note: If you copy and paste in Javascript code a string with special characters like a quote, a double-quote or an anti-slash, you need to add the escape operator, the anti-slash \: ab"cab\"c.

Source code

Conversions from characters to numbers

function encode(str, base, prefix) {
  var encodedStr = '';
  for(var i = 0, l = str.length; i < l; i++) {
    var s = parseInt(str.charCodeAt(i)).toString(base);
    encodedStr += prefix + s;
  }
  return encodedStr;
}
encode('user@domain.tld', 16, '%')
/*
%75%73%65%72%40%64%6f%6d%61%69%6e%2e%74%6c%64
*/
encode('mailto:user@domain.tld', 10, '&#')
/*
&#109&#97&#105&#108&#116&#111&#58&#117&#115&#101&#114&#64&#100&#111&#109&#97&#105&#110&#46&#116&#108&#100
*/
encode('user@domain.tld', 16, '&#x')
/*
&#x75&#x73&#x65&#x72&#x40&#x64&#x6f&#x6d&#x61&#x69&#x6e&#x2e&#x74&#x6c&#x64
*/

Mirror

function reverse(s) {
  var t = '';
  for (var i = 0, l = s.length - 1; i <= l; i++) {
    t += s.charAt(l - i);
  }
  return t;
}
reverse('user@domain.tld')
/* 
dlt.niamod@resu
 */

Caesar cipher

All the letters (capital and small) are replaced with a letter with a shift (modulo 26). All other characters are invariant.

function rotate(word, shift) {
  return word.replace(/[a-zA-Z]/g, function(c){
    return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + shift) ? c : c - 26);
  });
} 
rotate('mailto:user@domain.tld', 14)
/*
aowzhc:igsf@rcaowb.hzr
*/
rotate('aowzhc:igsf@rcaowb.hzr', 12)
/*
mailto:user@domain.tld
*/

Vigenère cipher

The alphabet used is a segment of the ASCII table starting at origin and with size size.

Encryption: First word is split into prefix + suffix at a position determined by key. Second the first character of suffix + prefix is replaced with a character after a shift given by the code of the first character of key (modulo the size of the alphabet), and so on. The split allows the word to be an extension of the word mailto: without jeopardizing the safety of the cipher.

Decryption: the reverse function.

function encrypt(origin, size, key, word){
  var s = 0
  for (var i = 0; i < key.length; i++) {
    s = s + key.charCodeAt(i)
  }
  var e = ''
  var pos = 0
  var shift= s % word.length;
  for (var i = 0; i < word.length; i++) {
    var j = (i + shift) % word.length;
    e = e + String.fromCharCode((word.charCodeAt(j) - origin + key.charCodeAt(pos) - origin) % size + origin);
    pos = (pos + 1) % key.length
  }
  return e;
}
function decrypt(origin, size, key, word) {
  var s = 0
  for (var i = 0; i < key.length; i++) {
    s = s + key.charCodeAt(i)
  }
  var pos = 0
  var first = s % word.length
  var prefix = ''
  var suffix = ''
  for (var i = 0; i < word.length - first; i++) {
    suffix = suffix + String.fromCharCode((word.charCodeAt(i) + size - key.charCodeAt(pos)) % size+ origin)
    pos = (pos + 1) % key.length
  }
  for (var i = word.length - first; i < word.length; i++) {
    prefix = prefix + String.fromCharCode((word.charCodeAt(i) + size - key.charCodeAt(pos)) % size + origin)
    pos = (pos + 1) % key.length
  }
  var d = prefix + suffix
  return d;
}
encrypt(33, 94, 'vigenere', 'mailto:user@domain.tld')
/*
ev\RSSTOc^W~dYXX7NWSPO
*/
decrypt(33, 94, 'vigenere', 'ev\\RSSTOc^W~dYXX7NWSPO')
/*
mailto:user@domain.tld
*/

Last Updated 2015-05-11T08:00+0200. Comments or questions: Send a mail.