Skip to content

Instantly share code, notes, and snippets.

@ahmedengu
Last active August 17, 2017 16:48
Show Gist options
  • Save ahmedengu/a1645cf4af0ca81772dfc10981ebeb52 to your computer and use it in GitHub Desktop.
Save ahmedengu/a1645cf4af0ca81772dfc10981ebeb52 to your computer and use it in GitHub Desktop.
var alphabets = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
function _encrypt(plainText, options) {
let cipherText = "";
for (let c of plainText) {
let index = alphabets.indexOf(c);
if (index == -1) {
cipherText += c;
continue;
}
let newIndex = (index + options.key) % alphabets.length;
cipherText += alphabets.charAt(newIndex);
}
if (options.lowerCase)
return cipherText.toLocaleLowerCase();
return cipherText;
}
function encrypt(plainText, options) {
plainText = plainText.toUpperCase();
options = options || {};
if (typeof options.key != 'undefined') {
return _encrypt(plainText, options);
}
let cipherTextArr = [];
for (let key = 1; key < alphabets.length; key++) {
options.key = key;
cipherTextArr.push(_encrypt(plainText, options));
}
return cipherTextArr;
}
function decrypt(cipherText, options) {
cipherText = cipherText.toUpperCase();
options = options || {};
let plainTextArr = [];
for (let key = 1; key < alphabets.length; key++) {
let plainText = "";
for (let c of cipherText) {
let index = alphabets.indexOf(c);
if (index == -1) {
plainText += c;
continue;
}
let newIndex = (index - key) % alphabets.length;
plainText += alphabets.charAt(newIndex >= 0 ? newIndex : alphabets.length + newIndex);
}
if (options.lowerCase)
plainTextArr.push(plainText.toLocaleLowerCase());
else
plainTextArr.push(plainText);
}
return plainTextArr;
}
function printArr(arr) {
let ret = "";
for (let i = 0; i < arr.length; i++) {
ret += (i + 1) + ") " + arr[i] + "<br>";
}
return ret;
}
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: 1}).should.equal('BCD');
});
it('can encrypt a to z', function () {
main.encrypt('abc', {key: 1}).should.equal('BCD');
});
it('handle space', function () {
main.encrypt('AB C', {key: 1}).should.equal('BC D');
});
it('handle space and a random key', function () {
main.encrypt('AB C', {key: 5}).should.equal('FG H');
});
it('handle space and a random key, return lower case', function () {
main.encrypt('AB C', {key: 5, lowerCase: true}).should.equal('fg h');
});
it('generate table', function () {
var expected = ['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI', 'IJ', 'JK', 'KL', 'LM', 'MN', 'NO', 'OP', 'PQ', 'QR', 'RS', 'ST', 'TU', 'UV', 'VW', 'WX', 'XY', 'YZ', 'ZA'];
for (let i = 0; i < 26; ++i) {
main.encrypt('AB', {key: i}).should.equal(expected[i]);
}
});
it('encrypt text', function () {
main.encrypt('MEET ME AFTER THE TOGA PARTY', {key: 3}).should.equal('PHHW PH DIWHU WKH WRJD SDUWB');
});
it('produce all keys', function () {
main.encrypt('MEET ME AFTER THE TOGA PARTY').should.have.lengthOf(25);
main.decrypt('MEET ME AFTER THE TOGA PARTY').should.contain('PHHW PH DIWHU WKH WRJD SDUWB');
main.decrypt("CAESAR'S WIFE MUST BE ABOVE SUSPICION").should.contain("BZDRZQ'R VHED LTRS AD ZANUD RTROHBHNM");
});
});
describe('Decryption', function () {
it('returned array should equal 25', function () {
main.decrypt('PHHW').should.have.lengthOf(25);
});
it('returned array should contain "meet" ', function () {
main.decrypt('PHHW', {lowerCase: true}).should.contain('meet');
});
it('returned array should contain "MEET" ', function () {
main.decrypt('PHHW').should.contain('MEET');
});
it('decrypt text', function () {
main.decrypt('PHHW PH DIWHU WKH WRJD SDUWB').should.contain('MEET ME AFTER THE TOGA PARTY');
});
it('decrypt figure 2.3 text and compare with it\'s table', function () {
let expected = ['OGGV OG CHVGT VJG VQIC RCTVA',
'NFFU NF BGUFS UIF UPHB QBSUZ',
'MEET ME AFTER THE TOGA PARTY',
'LDDS LD ZESDQ SGD SNFZ OZQSX',
'KCCR KC YDRCP RFC RMEY NYPRW',
'JBBQ JB XCQBO QEB QLDX MXOQV',
'IAAP IA WBPAN PDA PKCW LWNPU',
'HZZO HZ VAOZM OCZ OJBV KVMOT',
'GYYN GY UZNYL NBY NIAU JULNS',
'FXXM FX TYMXK MAX MHZT ITKMR',
'EWWL EW SXLWJ LZW LGYS HSJLQ',
'DVVK DV RWKVI KYV KFXR GRIKP',
'CUUJ CU QVJUH JXU JEWQ FQHJO',
'BTTI BT PUITG IWT IDVP EPGIN',
'ASSH AS OTHSF HVS HCUO DOFHM',
'ZRRG ZR NSGRE GUR GBTN CNEGL',
'YQQF YQ MRFQD FTQ FASM BMDFK',
'XPPE XP LQEPC ESP EZRL ALCEJ',
'WOOD WO KPDOB DRO DYQK ZKBDI',
'VNNC VN JOCNA CQN CXPJ YJACH',
'UMMB UM INBMZ BPM BWOI XIZBG',
'TLLA TL HMALY AOL AVNH WHYAF',
'SKKZ SK GLZKX ZNK ZUMG VGXZE',
'RJJY RJ FKYJW YMJ YTLF UFWYD',
'QIIX QI EJXIV XLI XSKE TEVXC'];
main.decrypt('PHHW PH DIWHU WKH WRJD SDUWB').should.deep.equal(expected);
});
it('decrypt supplied ciphetexts', function () {
main.decrypt("BZDRZQ'R VHED LTRS AD ZANUD RTROHBHNM").should.contain("CAESAR'S WIFE MUST BE ABOVE SUSPICION");
main.decrypt("KENKMOC PYBDEXK TEFKD").should.contain("AUDACES FORTUNA JUVAT");
});
});
<!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="Plain Text" id="plain">
<button onclick="document.getElementById('output').innerHTML=printArr(encrypt(document.getElementById('plain').value))">
Encrypt
</button>
<button onclick="document.getElementById('output').innerHTML=printArr(decrypt(document.getElementById('plain').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