4umi.com/web/javascript/encrypt

Key to encryption

Text Javascript

Text can be encrypted and decrypted on this page with a unique personal, secret key. The key may contain lowercase and uppercase letters, digits and the underscore character, and should be about eight characters in length, although none of these requirements are actually checked. The really important thing is to remember it well, as it will be needed for decryption.

Encryptor

          (Get random)
           

The script

Besides being useful, the underlying javascript code is in itself also quite educational. See the script.


// original: Naresh Hotchandani (nhotchandani-at-rediffmail.com)

function encrypt() {
  var len = getkey();
  var i, s = '', fin = 0, txt = f.freadable.value;
  if( len>0 ) {
    if( txt.length>0 ) {
      for( i=0; i<txt.length; i++ ) {
        fin = txt.charCodeAt(i) + len;
        if( fin>99 ) {
          s += fin;
        } else {
          s += '0' + fin;
        }
      }
      f.fencrypt.value = s;
      //f.freadable.value = '';
    }
    else{
      window.alert( 'Please enter the text to be encrypted.' );
      f.freadable.focus();
    }
  }
}

function decrypt() {
  var len = getkey();
  var i, t, s = '', txt = f.fencrypt.value, tmp;
  if( len>0 ) {
    if( txt.length>0 ) {
      for( i=0; i<txt.length; i+=3 ) {
        tmp = txt.substring(i,i+3);
        t = unescape( '%' + ( parseInt(tmp) - len ).toString(16) );
        if( t=='%9' ) {
          s += '\t';
        } else if( t=='%d' ) {
          s += '\r';
        } else if( t=='%a' ) {
          s += '\n';
        } else {
          s += t;
        }
      }
      f.freadable.value = s;
      //f.fencrypt.value = '';
    }
    else{
      window.alert( 'Please enter the text to be decrypted.' );
      f.fencrypt.focus();
    }
  }
}

function getkey() {
  var i, key = f.fkey.value, len = 0;
  for( i = 0; i<key.length; i++ ) {
    len += key.charCodeAt(i);
  }
  if( !len ) {
    window.alert( 'Please enter the appropriate key.' );
    f.fkey.focus();
  }
  return len;
}

function getrandom() {
  var i = 8, r,
   s = String.fromCharCode( Math.floor( Math.random() * 26 ) + ( Math.random()>0.5 ? 65 : 97 ) );
  while(i--) {
   r = Math.floor( Math.random() * 63 );
   if( r<10 ) { r += 48; }
   else if( r<36 ) { r += 55; }
   else { r += 61; }
   if( r==123 ) { r = 95; }
   s += String.fromCharCode( r );
 }
 f.fkey.value = s;
 f.fkey.focus();
 return false;
}

var f = document.forms.f.elements;
f.fkey.onfocus = function() { this.select(); }
//f.fkey.focus();