»
EnglishFrenchVietnamese

Print - QwertyShifter - JavaScriptBank.com

Full version: jsB@nk » Security » Encryption » QwertyShifter
URL: https://www.javascriptbank.com/qwertyshifter.html

QwertyShifter © JavaScriptBank.comEncoder/decoder for QWERTY-shifted languages.

Full version: jsB@nk » Security » Encryption » QwertyShifter
URL: https://www.javascriptbank.com/qwertyshifter.html



JavaScript
<script type="text/javascript">var qs = new QwertyShifter('right', 1);window.onload = function(){  var f = document.forms['qsForm'];  f.direction.value = qs.dir;  f.radix.value = qs.rad;}function qs_reset(){  var f = document.forms['qsForm'];  var d = f.direction.value.toLowerCase();  if (d != 'left' && d != 'right') {    alert("Invalid Direction. Must be 'left' or 'right'.");    return;  }  var r = parseInt(f.radix.value);  if (isNaN(r) || r < 1 || r >= qs.qu.length) {    alert("Invalid Radix. Must be > 0 and < " + qs.qu.length);    return;  }  qs.reset(d, r);  alert("QwertyShifter\n\nNew settings accepted.");}function encode(){  var f = document.forms['qsForm'];  f.shifted.value = qs.encode(f.original.value);}function decode(){  var f = document.forms['qsForm'];  f.original.value = qs.decode(f.shifted.value);}//------------------------------------------------------// QwertyShifter - Object Prototype and Public Methodsfunction QwertyShifter(direction, radix){  this.dir = direction; // direction to shift: 'left' or 'right'  this.rad = radix;     // number of chars to shift  this.ql = new Array('`','1','2','3','4','5','6','7','8','9','0','-','=','q','w','e','r','t','y','u','i','o','p','[',']','\\','a','s','d','f','g','h','j','k','l',';',"'",'z','x','c','v','b','n','m',",",'.','/',' '); // lower case and unshifted chars  this.qu = new Array('~','!','@','#','$','%','^','&','*','(',')','_','+','Q','W','E','R','T','Y','U','I','O','P','{','}','|', 'A','S','D','F','G','H','J','K','L',':','"','Z','X','C','V','B','N','M',"<",'>','?'); // upper case and shifted chars}QwertyShifter.prototype.reset = function(direction, radix){  this.dir = direction;  this.rad = radix;}QwertyShifter.prototype.getChr = function(c, act){  var i, qLen=0, qBuf;  for (i = 0; i < this.ql.length; ++i) {    if (this.ql[i] == c) {      qBuf = this.ql;      qLen = this.ql.length;      break; // out of 'for' loop    }  }  if (!qLen) {    for (i = 0; i < this.qu.length; ++i) {      if (this.qu[i] == c) {        qBuf = this.qu;        qLen = this.qu.length;        break; // out of 'for' loop      }    }  }  if (!qLen) {    return c;  }  if ((act == 'enc' && this.dir == 'right') || (act == 'dec' && this.dir == 'left')) { // shift right    if (i + this.rad >= qLen) {      i = (i + this.rad) - qLen; // wrap to beginning    }    else {      i += this.rad;    }  }  else if ((act == 'enc' && this.dir == 'left') || (act == 'dec' && this.dir == 'right')) { // shift left    if (i - this.rad < 0) {      i = qLen + (i - this.rad); // wrap to end    }    else {      i -= this.rad;    }  }  return qBuf[i];}QwertyShifter.prototype.encode = function(s){  var i, buf='';  for (i = 0; i < s.length; ++i) {    buf += this.getChr(s.charAt(i), 'enc');  }  return buf;}QwertyShifter.prototype.decode = function(s){  var i, buf='';  for (i = 0; i < s.length; ++i) {    buf += this.getChr(s.charAt(i), 'dec');  }  return buf;}</script><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->


HTML
<form name="qsForm" onsubmit="return false"><p><b>Original:</b></p><textarea name="original" rows="8" cols="60"></textarea><input type="button" value="Encode" onclick="encode()"><p><b>QWERTY-Shifted:</b></p><textarea name="shifted" rows="8" cols="60"></textarea><input type="button" value="Decode" onclick="decode()"><p><b>Settings:</b></p><p>Shift Direction <input name="direction" type="text" value="" size="20"></p><p>Shift Amount <input name="radix" type="text" value="" size="20"></p><p><input type="button" value="SET" onclick="qs_reset()"></p></form><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->