Skip to content

Instantly share code, notes, and snippets.

@ahmedengu
Last active August 17, 2017 16:48
Show Gist options
  • Save ahmedengu/cd474926c93baa033c70786d388c0036 to your computer and use it in GitHub Desktop.
Save ahmedengu/cd474926c93baa033c70786d388c0036 to your computer and use it in GitHub Desktop.
var TABLE = [['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'],
['B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A'],
['C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B'],
['D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C'],
['E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D'],
['F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E'],
['G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F'],
['H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G'],
['I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
['J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'],
['K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'],
['L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'],
['M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L'],
['N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M'],
['O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N'],
['P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O'],
['Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P'],
['R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q'],
['S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R'],
['T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S'],
['U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T'],
['V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U'],
['W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V'],
['X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W'],
['Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X'],
['Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y']];
function _encrypt(plainText, options) {
options.key = options.key.toUpperCase();
let cipherText = '';
for (let i = 0; i < plainText.length; i++) {
let indexOfPT = TABLE[0].indexOf(plainText[i]);
let indexOfKey = TABLE[0].indexOf(options.key[i % options.key.length]);
if (indexOfPT == -1 || indexOfKey == -1) {
cipherText += plainText[i];
continue;
}
cipherText += TABLE[indexOfPT][indexOfKey];
}
if (options.lowerCase)
return cipherText.toLowerCase();
return cipherText;
}
function encrypt(plainText, options) {
plainText = plainText.toUpperCase();
options = options || {};
if (typeof options.key != 'undefined')
return _encrypt(plainText, options);
var ret = [];
for (let i = 0; i < TABLE.length; i++) {
options.key = '';
for (let j = 0; j < i + 1; j++) {
options.key += TABLE[Math.floor(Math.random() * TABLE.length)];
}
ret.push({key: options.key, text: _encrypt(plainText, options)})
}
return ret;
}
function _decrypt(cipherText, options) {
options.key = options.key.toUpperCase();
let plainText = '';
for (let i = 0; i < cipherText.length; i++) {
let indexOfKey = TABLE[0].indexOf(options.key[i % options.key.length]);
let indexOfPT = -1;
for (let j = 0; j < TABLE.length; j++) {
if (TABLE[j][indexOfKey] == cipherText[i]) {
indexOfPT = j;
break;
}
}
if (indexOfPT == -1 || indexOfKey == -1) {
plainText += cipherText[i];
continue;
}
plainText += TABLE[0][indexOfPT];
}
if (options.lowerCase)
return plainText.toLowerCase();
return plainText;
}
function decrypt(cipherText, options) {
cipherText = cipherText.toUpperCase();
options = options || {};
return _decrypt(cipherText, options);
}
function printArr(arr) {
if (Array.isArray(arr)) {
let ret = "";
for (let i of arr) {
ret += i.key + ") " + i.text + "<br>";
}
return ret;
}
return arr;
}
var module = module || {};
module.exports = {
encrypt: encrypt,
decrypt: decrypt
};
var main = require('../main');
var should = require('chai').should();
describe('Encryption', function () {
it('can encrypt A to Z', function () {
main.encrypt('ABC', {key: 'ABC'}).should.equal('ACE');
});
it('can encrypt a to a', function () {
main.encrypt('abc', {key: 'ABC'}).should.equal('ACE');
});
it('can encrypt with space', function () {
main.encrypt('AB C', {key: 'AB C'}).should.equal('AC E');
});
it('can repeat key', function () {
main.encrypt('ABC', {key: 'B'}).should.equal('BCD');
});
it('lowercase', function () {
main.encrypt('ABC', {key: 'B', lowerCase: true}).should.equal('bcd');
});
it('encrypt with deception', function () {
main.encrypt('ABC', {key: 'deception'}).should.equal('DFE');
});
it('random 26 key length', function () {
main.encrypt('ABC').should.have.lengthOf(26);
});
it('random 26 key, key check', function () {
let arr = main.encrypt('ABC');
for (let o of arr) {
main.encrypt('abc', {key: o.key}).should.equal(o.text);
}
});
});
describe('Decryption', function () {
it('can decrypt A to Z', function () {
main.decrypt('ACE', {key: 'ABC'}).should.equal('ABC');
});
it('can decrypt a to a', function () {
main.decrypt('ace', {key: 'ABC'}).should.equal('ABC');
});
it('can decrypt with space', function () {
main.decrypt('AC E', {key: 'AB C'}).should.equal('AB C');
});
it('can decrypt key', function () {
main.decrypt('BCD', {key: 'B'}).should.equal('ABC');
});
it('lowercase', function () {
main.decrypt('BCD', {key: 'B', lowerCase: true}).should.equal('abc');
});
it('decrypt with deception', function () {
main.decrypt('DFE', {key: 'deception'}).should.equal('ABC');
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test</title>
<script src="main.js"></script>
<script></script>
</head>
<body>
<h1>Encrypt/Decrypt</h1>
<input type="text" placeholder="Text" id="aText">
<input type="text" placeholder="Key" id="key">
<button onclick="document.getElementById('output').innerHTML=printArr(encrypt(document.getElementById('aText').value,{key:document.getElementById('key').value}))">
Encrypt
</button>
<button onclick="document.getElementById('output').innerHTML=printArr(decrypt(document.getElementById('aText').value,{key:document.getElementById('key').value}))">
Decrypt
</button>
<h1>Output</h1>
<div id="output"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment