/* deciphers an encoded text (txtEnc; escaped!) with a keystring using XOR operations*/

function deciph(txtEnc, keystr)
{
txtEnc = unescape(txtEnc); // unescape the encoded text

var keyLen = keystr.length; // length of the key
var txtLen = txtEnc.length; // length of the text
var txtDec = '';  // variable with the resulting deciphered string

//deciphering
for (i=0;i<txtLen;i++)  // encrypting / decrypting loop
 {
   txtDec+=String.fromCharCode(txtEnc.charCodeAt(i)^keystr.charCodeAt(Math.abs(i%keyLen)));
      // XOR encoding; modulo to adapt to different key lengths
      /* note: the same function can also be used to cipher something (symmetric)
          for proper display the string must be handled with escape / unescape */
 }
return txtDec;
}

// gets the keyword (person id) used for ciphering
function getKey()
{
var keystr = '' + this.location;
var indx1 = keystr.lastIndexOf('?'); //start of the get variables

/* original version as was used at psi:
var get = keystr.substring(indx1+1); //extract the get variable string
get = get.split('&');                //split into an array of get variables ('key=value')
                                     //note split again to split keys from values

keystr = get[0]; //the keyword (person id) is the first element
*/
if (indx1 == -1){  // if there is no '?' ...
  keystr = '?';      //... set the keystring to '?'
} else {           // else ..
  keystr = keystr.substring(indx1); // ... the keystring is now everything after ?, incl. ?
}

return keystr;
}

