This tutorial will show you how you can encrypt/decrypt text (string) with Java Script or JScript.
First we need a table, even called for matrix. To create a matrix I chose to combine three array-lists into one single. (Note that it also works without the combining of arrays!)
//generating 3 arrays var _matrixA = new Array(); var _matrixB = new Array(); var _matrixC = new Array(); var _mA, _mB; _matrixA = new Array("A", "", "", "B", "C", "D", "E", "F", "G", "H"); // 0 _matrixB = new Array("I", "J", "K", "L", "M", "N", "O", "P", "Q", "R"); // 1 _matrixC = new Array("S", "T", "U", "V", "W", "X", "Y", "Z", ".", "/"); // 2 //assign values of columns _mA = 1; _mB = 2; //combining matrix var _matrix = new Array(_matrixA, _matrixB, _matrixC); //this is the table, to get data, use _matrix[x][y]
Encryption Method:
// Encryption method function encrypt(_text) { var _returnVal = ""; for (i = 0; i < _text.length; i++) { if (_matrixA.indexOf(_text.charAt(i)) != -1) { _returnVal += (_matrixA.indexOf(_text.charAt(i))); } if (_matrixB.indexOf(_text.charAt(i)) != -1) { _returnVal += (_mA); //default 1 _returnVal += (_matrixB.indexOf(_text.charAt(i))); } if (_matrixC.indexOf(_text.charAt(i)) != -1) { _returnVal += (_mB); //default 2 _returnVal += (_matrixC.indexOf(_text.charAt(i))); } } return _returnVal; }
Decryption Method:
// Decryption Method function decrypt(_text) { var _returnVal = ""; for (i=0; i < _text.length; i++) { if (_text.charAt(i) != _mA || _text.charAt(i) != _mB )//the char has to be in 'first' matrix, _matrixA { _returnVal += _matrixA[_text.charAt(i)]; } if (_text.charAt(i) == _mA) //the char has to be in 'secend' matrix, _matrixB { i++; _returnVal += _matrixB[_text.charAt(i)]; } if (_text.charAt(i) == _mB) //the char has to be in 'third' matrix, _matrixC { i++; _returnVal += _matrixC[_text.charAt(i)]; } } return _returnVal; }
Copyright (C) 2011 Artem Los
Leave a Reply