Skip to content

Instantly share code, notes, and snippets.

@rfprod
Last active April 22, 2017 15:42
Show Gist options
  • Save rfprod/2c707eea2a0a0a1d12f705a33320c8a1 to your computer and use it in GitHub Desktop.
Save rfprod/2c707eea2a0a0a1d12f705a33320c8a1 to your computer and use it in GitHub Desktop.
Ranges-based Encryptor/Decryptor
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"

Ranges-based Encryptor/Decryptor

Function encrypt encrypts arbitrary string with characters.

Function decrypt decrypts previously encrypted string with function encrypt.

Both functions accept two parameters: string and integer.

The latter is a shift index for different rows.

It is always 3 digits long, i.e. value 0 means 000, 55 means 055 etc.

First digit is a shift for the first line (for both lower and upper case, i.e. ranges[0] and ranges[1]).

Second digit is a shift for the second line (for both lower and upper case, i.e. ranges[2] and ranges[3]).

Third digit is a shift for the third line (for both lower and upper case, i.e. ranges[4] and ranges[5]).

ranges is an array, all elements of odd rows are uppercase equivalents of the respective letters from the previous row:

['qwertyuiop', // lower 1 'QWERTYUIOP', // upper 1 'asdfghjkl', // lower 2 'ASDFGHJKL', // upper 2 'zxcvbnm,.', // lower 3 'ZXCVBNM<>'] // upper 3

If letter from input does not belong to any range it is passed to output as is.

A script by V.

License.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment