/*
 * Dead simple fixed-key symmetric encryption for email obfuscation.
 *
 * 2008, Tero Tilus <tero@tilus.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 */
SimpleCrypt = {
  forward: null,
  backward: null,

  init: function() {
    if (SimpleCrypt.forward) return;
    var fmap = new Array();
    var bmap = new Array();
    var s = "\"</> @:'.=abcdefghijklmnopqrstuvwxyz0123456789";
    var d =  "ABCDEFGHIJK123456789tuvwxyzabcdefghijklmnopqrs";
    for (i=0; i<s.length; i++) {
      fmap[s.charAt(i)] = d.charAt(i);
      bmap[d.charAt(i)] = s.charAt(i);
    }
    SimpleCrypt.forward = fmap;
    SimpleCrypt.backward = bmap;
  },

  conv: function(str, map) {
    var converted='';
    for (i=0; i < str.length; i++) {
      var c = str.charAt(i);
      converted += ((map[c]) ? map[c] : c);
    }
    return converted;
  },

  enc: function(str) {
    SimpleCrypt.init();
    return SimpleCrypt.conv(str, SimpleCrypt.forward);
  },
  
  dec: function(str) {
    SimpleCrypt.init();
    return SimpleCrypt.conv(str, SimpleCrypt.backward);
  }
}

