Skip to content

Instantly share code, notes, and snippets.

@rfprod
Last active April 22, 2017 15:42
Show Gist options
  • Save rfprod/555862f661d436555a0e27814a91352b to your computer and use it in GitHub Desktop.
Save rfprod/555862f661d436555a0e27814a91352b to your computer and use it in GitHub Desktop.
Index-Difference Encryptor/Decryptor
function encrypt(text) {
if (!text) { return text; }
const region = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.,:;-?! \'()$%&"';
let output = '';
let encPhase = ''; // output copy without char replacement - needed to calculate diff for replacement
for (let i = 0, max = text.length; i < max; i++) {
// throw error if input has characters that are out of range
if (region.indexOf(text[i]) === -1) { throw 'Error'; }
// enc phase 1 - every second char to upper case
if (i % 2 === 0) {
output += text[i];
encPhase += text[i];
} else {
output += (text[i] === text[i].toUpperCase()) ? text[i].toLowerCase() : text[i].toUpperCase();
encPhase += (text[i] === text[i].toUpperCase()) ? text[i].toLowerCase() : text[i].toUpperCase();
}
if (i > 0) {
// enc phase 2 - replace every char with char at index = diff in region: original[i] - encrypted[i]
let diff = region.indexOf(encPhase[i - 1]) - region.indexOf(output[i]);
diff = (diff < 0) ? region.length + diff : diff;
//console.log('>> diff:', diff, ':', region[diff]);
output = output.substring(0, output.length - 1) + region[diff];
//console.log('output:', output);
}
}
// enc phase 3 - replace first character with char at mirror index from the range ending
output = region[region.length - 1 - region.indexOf(output.slice(0,1))] + output.slice(1);
return output;
}
function decrypt(encryptedText) {
if (!encryptedText) { return encryptedText; }
const region = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.,:;-?! \'()$%&"';
let output = encryptedText;
// decr phase 1 - replace first character with char at mirror index from the range start
output = region[region.length - 1 - region.indexOf(output.slice(0,1))] + output.slice(1);
let encPhase = output; // output copy without char replacement - needed to calculate diff for replacement
for (let i = 1, max = output.length; i < max; i++) {
// throw error if input has characters that are out of range
if (region.indexOf(encryptedText[i]) === -1) { throw 'Error'; }
// decr phase 2 - replace every char with char at index = diff in region: original[i] - encrypted[i]
let diff = region.indexOf(encPhase[i - 1]) - region.indexOf(output[i]);
diff = (diff < 0) ? region.length + diff : diff;
//console.log('>> decription diff:', diff, ':', region[diff]);
const replacement = (i % 2 === 0) ? region[diff] : (region[diff] === region[diff].toLowerCase()) ? region[diff].toUpperCase() : region[diff].toLowerCase(); // decr phase 3 - convert to lower case every 2nd char
output = output.substring(0, i) + replacement + output.substring(i + 1, output.length);
//console.log('output:', output);
encPhase = encPhase.substring(0, i) + region[diff] + encPhase.substring(i + 1, encPhase.length);
//console.log('encPhase:', encPhase);
}
return output;
}
encrypt("This is a test!"); // "5MyQa9p0riYplZc"
decrypt("5MyQa9p0riYplZc"); // "This is a test!"

Index-Difference Encryptor/Decryptor

Function encrypt encrypts string with characters from range ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.,:;-?! \'()$%&".

Function encrypt returns null or empty string if input is empty.

Function encrypt throws Error in characters in input string are out of given range.

Function decrypt decrypts previously encrypted string with function encrypt.

A script by V.

License.

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