Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function encrypt(text, key) {
- //console.log('input:', text, key);
- const ranges = [ // define ranges
- 'qwertyuiop', // lower 1
- 'QWERTYUIOP', // upper 1
- 'asdfghjkl', // lower 2
- 'ASDFGHJKL', // upper 2
- 'zxcvbnm,.', // lower 3
- 'ZXCVBNM<>' // upper 3
- ];
- let keys = (key < 10) ? '00' + key : (key < 100) ? '0' + key : key.toString(); // convert key to string
- //console.log('keys:', key);
- let output = '';
- for (let i = 0, max = text.length; i < max; i++) {
- // check ranges
- let rangeIndex;
- findRangeIndexLoop:
- for (let r = 0, rMax = ranges.length; r < rMax; r++) {
- if (ranges[r].indexOf(text[i]) !== -1) {
- rangeIndex = r;
- break findRangeIndexLoop;
- }
- }
- //console.log('rangeIndex:', rangeIndex);
- // encrypt
- if (rangeIndex || rangeIndex === 0) {
- const currentPosition = ranges[rangeIndex].indexOf(text[i]);
- //console.log('currentPosition:', currentPosition);
- const keyIndex = (rangeIndex >= 0 && rangeIndex <= 1) ? 0 : (rangeIndex >= 2 && rangeIndex <= 3) ? 1 : 2;
- //console.log('keyIndex:', keyIndex, ' | keys[keyIndex]:', keys[keyIndex]);
- let newPosition = currentPosition + parseInt(keys[keyIndex]);
- //console.log('newPosition:', newPosition);
- while (newPosition > ranges[rangeIndex].length - 1) {
- newPosition -= ranges[rangeIndex].length;
- //console.log('newPosition recalculated:', newPosition);
- }
- //console.log('new letter:', ranges[rangeIndex][newPosition]);
- output += ranges[rangeIndex][newPosition];
- } else {
- // if out or range use char as is
- //console.log('new letter:', text[i]);
- output += text[i];
- }
- //console.log('===');
- }
- return output;
- }
- function decrypt(text, key) {
- //console.log('input:', text, key);
- const ranges = [ // define ranges
- 'qwertyuiop', // lower 1
- 'QWERTYUIOP', // upper 1
- 'asdfghjkl', // lower 2
- 'ASDFGHJKL', // upper 2
- 'zxcvbnm,.', // lower 3
- 'ZXCVBNM<>' // upper 3
- ];
- let keys = (key < 10) ? '00' + key : (key < 100) ? '0' + key : key.toString(); // convert key to string
- //console.log('keys:', key);
- let output = '';
- for (let i = 0, max = text.length; i < max; i++) {
- // check ranges
- let rangeIndex;
- findRangeIndexLoop:
- for (let r = 0, rMax = ranges.length; r < rMax; r++) {
- if (ranges[r].indexOf(text[i]) !== -1) {
- rangeIndex = r;
- break findRangeIndexLoop;
- }
- }
- //console.log('rangeIndex:', rangeIndex);
- // decrypt
- if (rangeIndex || rangeIndex === 0) {
- const currentPosition = ranges[rangeIndex].indexOf(text[i]);
- //console.log('currentPosition:', currentPosition);
- const keyIndex = (rangeIndex >= 0 && rangeIndex <= 1) ? 0 : (rangeIndex >= 2 && rangeIndex <= 3) ? 1 : 2;
- //console.log('keyIndex:', keyIndex, '| keys[keyIndex]:', keys[keyIndex]);
- let newPosition = currentPosition - parseInt(keys[keyIndex]);
- //console.log('newPosition:', newPosition);
- while (newPosition > ranges[rangeIndex].length - 1) {
- newPosition -= ranges[rangeIndex].length;
- //console.log('newPosition recalculated:', newPosition);
- }
- while (newPosition < 0) {
- newPosition += ranges[rangeIndex].length;
- //console.log('newPosition recalculated:', newPosition);
- }
- //console.log('new letter:', ranges[rangeIndex][newPosition]);
- output += ranges[rangeIndex][newPosition];
- } else {
- // if out or range use char as is
- //console.log('new letter:', text[i]);
- output += text[i];
- }
- //console.log('===');
- }
- return output;
- }
- encrypt("Ball", 134); // ">fdd"
- encrypt("Ball", 444); // ">gff"
- decrypt(">fdd", 134); // "Ball"
- decrypt(">gff", 444); // "Ball"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement