Last active
October 14, 2016 21:13
-
-
Save ebrandel/312a5a7b6be92b8ba476c3c5a71b800a to your computer and use it in GitHub Desktop.
Nihlist Cipher Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var letters = "ABCDEFGHIKLMNOPQRSTUVWXYZ"; | |
// encrypt and decrypt this plain text | |
var plainText = "No Its THE code breaker No more secrets"; | |
// remove spaces | |
var compressedText = plainText.replace(/\s+/g, "").toLocaleUpperCase(); | |
var decryptedText = ""; | |
// The key needs to be repeated until it's the same length as the text to encrypt | |
var key = "TRON"; | |
var extendedKey = extendKey(key, compressedText.length); | |
// Two options for a polybius square | |
// polybius square - 5x5 = 25 letters. combine I/J | |
// polybuis square - 6x6 = 26 letters + 0-9 digits | |
// Keyword for square cannot have repetitive letters | |
// or can, but must be filtered out, which this code does | |
var keyword = "BISHOP"; | |
var polybiusLetters = keyword + letters; | |
var polybiusLetters = polybiusLetters.split('').filter(function(item, i, ar){ return ar.indexOf(item) === i; }).join(''); | |
var polybiusSquare = [[]]; | |
var index = 0; | |
for (var i = 0; i<5; i++) { | |
polybiusSquare[i] = []; | |
for (var j = 0; j<5; j++) { | |
var letter = polybiusLetters[index]; | |
polybiusSquare[i][j] = letter; | |
index++; | |
} | |
} | |
var compTextArray = buildArray(compressedText, polybiusSquare); | |
var extKeyArray = buildArray(extendedKey, polybiusSquare); | |
var cipherArray = []; | |
// create the ciphertext by adding the plaintext and key together | |
for (var i = 0; i<compTextArray.length; i++) { | |
cipherArray.push(compTextArray[i] + extKeyArray[i]); | |
} | |
var decryptArray = [] | |
// we're simply reversing the above process to decrypt | |
for (var i = 0; i<cipherArray.length; i++) { | |
decryptArray.push(cipherArray[i] - extKeyArray[i]); | |
} | |
// rebuild the original message by looking up letters in the square | |
for (var i = 0; i < decryptArray.length; i++) { | |
decryptedText += lookupLetter(decryptArray[i], polybiusSquare); | |
} | |
// Let's see if it worked | |
console.log("compressedText", compressedText); | |
console.log("decryptedText ", decryptedText); | |
// Lookup letters in the square | |
function lookupLetter(chunk, polybiusSquare) { | |
var coords = String(chunk).split(""); | |
var row = Number(coords[0]); | |
var col = Number(coords[1]); | |
// the row and col values have been increased by 1 to match the 1-5 values | |
// of a written polybius square. decrease by 1 | |
row--; | |
col--; | |
return polybiusSquare[row][col]; | |
} | |
function buildArray(letters, polybiusSquare) { | |
var returnArray = []; | |
for (var i = 0; i<letters.length; i++) { | |
var char = letters[i]; | |
returnArray.push(findChunk(char, polybiusSquare)) | |
} | |
return returnArray; | |
} | |
// find a letter in the square | |
function findChunk(findMe, polybiusSquare) { | |
for (var i = 0; i<5; i++) { | |
for (var j = 0; j<5; j++) { | |
if (polybiusSquare[i][j] === findMe) { | |
// the Polybius square has rows and columns labeled 1,2,3,4,5 | |
// so increment the row and column for display | |
i++; | |
j++; | |
return Number("" + i + j); // this is hacky, but it works | |
} | |
} | |
} | |
} | |
// extend the key to a predetermined length | |
function extendKey(key, length) { | |
var longKey = key; | |
while (longKey.length < length) { | |
longKey += key; | |
} | |
return longKey.substring(0, length); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment