Tidied up Bifid operations

feature-extract-files
n1474335 2017-06-28 19:54:34 +01:00
parent fe3aeabd0a
commit 323928ff86
2 changed files with 54 additions and 32 deletions

View File

@ -1521,7 +1521,7 @@ const OperationConfig = {
outputType: "string", outputType: "string",
args: [ args: [
{ {
name: "Alphabet Key", name: "Keyword",
type: "string", type: "string",
value: "" value: ""
} }
@ -1536,7 +1536,7 @@ const OperationConfig = {
outputType: "string", outputType: "string",
args: [ args: [
{ {
name: "Alphabet Key", name: "Keyword",
type: "string", type: "string",
value: "" value: ""
} }

View File

@ -593,25 +593,24 @@ const Cipher = {
return Cipher.runAffineEnc(input, [25, 25]); return Cipher.runAffineEnc(input, [25, 25]);
}, },
/** /**
* Generates a polybius square for the given keyword * Generates a polybius square for the given keyword
* *
* @private * @private
* @author Matt C [matt@artemisbot.uk] * @author Matt C [matt@artemisbot.uk]
* @param {string} keyword * @param {string} keyword - Must be upper case
* @returns {string} * @returns {string}
*/ */
_genPolybiusSquare: function (keyword) { _genPolybiusSquare: function (keyword) {
const alpha = "ABCDEFGHIKLMNOPQRSTUVWXYZ"; const alpha = "ABCDEFGHIKLMNOPQRSTUVWXYZ";
let polybius = [], const polArray = `${keyword}${alpha}`.split("").unique();
polString = ""; let polybius = [];
keyword.split("").unique().forEach(letter => {
polString += letter;
});
polString = `${polString}${alpha}`.split("").unique().join("");
for (let i = 0; i < 5; i++) { for (let i = 0; i < 5; i++) {
polybius[i] = polString.substr(i*5, 5).split(""); polybius[i] = polArray.slice(i*5, i*5 + 5);
} }
return polybius; return polybius;
}, },
@ -624,20 +623,28 @@ const Cipher = {
* @returns {string} * @returns {string}
*/ */
runBifidEnc: function (input, args) { runBifidEnc: function (input, args) {
const keyword = args[0].toUpperCase().replace("J", "I"), const keywordStr = args[0].toUpperCase().replace("J", "I"),
keyword = keywordStr.split("").unique(),
alpha = "ABCDEFGHIKLMNOPQRSTUVWXYZ"; alpha = "ABCDEFGHIKLMNOPQRSTUVWXYZ";
let output = "", let output = "",
xCo = [], xCo = [],
yCo = [], yCo = [],
structure = [], structure = [],
count = 0, count = 0;
trans;
if (keyword.split("").unique().length > 25) return "The alphabet keyword must be less than 25 characters."; if (keyword.length > 25)
if (!/^[a-zA-Z]+$/.test(keyword) && keyword.split("").unique().length > 0) return "The key must consist only of letters"; return "The alphabet keyword must be less than 25 characters.";
const polybius = Cipher._genPolybiusSquare(keyword);
input.replace("J", "I").split("").forEach((letter) => { if (!/^[a-zA-Z]+$/.test(keywordStr) && keyword.length > 0)
return "The key must consist only of letters";
const polybius = Cipher._genPolybiusSquare(keywordStr);
input.replace("J", "I").split("").forEach(letter => {
let alpInd = alpha.split("").indexOf(letter.toLocaleUpperCase()) >= 0, let alpInd = alpha.split("").indexOf(letter.toLocaleUpperCase()) >= 0,
polInd; polInd;
if (alpInd) { if (alpInd) {
for (let i = 0; i < 5; i++) { for (let i = 0; i < 5; i++) {
polInd = polybius[i].indexOf(letter.toLocaleUpperCase()); polInd = polybius[i].indexOf(letter.toLocaleUpperCase());
@ -646,6 +653,7 @@ const Cipher = {
yCo.push(i); yCo.push(i);
} }
} }
if (alpha.split("").indexOf(letter) >= 0) { if (alpha.split("").indexOf(letter) >= 0) {
structure.push(true); structure.push(true);
} else if (alpInd) { } else if (alpInd) {
@ -655,20 +663,22 @@ const Cipher = {
structure.push(letter); structure.push(letter);
} }
}); });
trans = `${yCo.join("")}${xCo.join("")}`;
const trans = `${yCo.join("")}${xCo.join("")}`;
structure.forEach(pos => { structure.forEach(pos => {
if (typeof pos === "boolean") { if (typeof pos === "boolean") {
let coords = trans.substr(2*count, 2).split(""); let coords = trans.substr(2*count, 2).split("");
if (pos) {
output += polybius[coords[0]][coords[1]]; output += pos ?
} else { polybius[coords[0]][coords[1]] :
output += polybius[coords[0]][coords[1]].toLocaleLowerCase(); polybius[coords[0]][coords[1]].toLocaleLowerCase();
}
count++; count++;
} else { } else {
output += pos; output += pos;
} }
}); });
return output; return output;
}, },
@ -681,18 +691,27 @@ const Cipher = {
* @returns {string} * @returns {string}
*/ */
runBifidDec: function (input, args) { runBifidDec: function (input, args) {
const keyword = args[0].toUpperCase().replace("J", "I"), const keywordStr = args[0].toUpperCase().replace("J", "I"),
keyword = keywordStr.split("").unique(),
alpha = "ABCDEFGHIKLMNOPQRSTUVWXYZ"; alpha = "ABCDEFGHIKLMNOPQRSTUVWXYZ";
let output = "", let output = "",
structure = [], structure = [],
count = 0, count = 0,
trans = ""; trans = "";
if (keyword.split("").unique().length > 25) return "The alphabet keyword must be less than 25 characters.";
if (!/^[a-zA-Z]+$/.test(keyword) && keyword.split("").unique().length > 0) return "The key must consist only of letters"; if (keyword.length > 25)
const polybius = Cipher._genPolybiusSquare(keyword); return "The alphabet keyword must be less than 25 characters.";
if (!/^[a-zA-Z]+$/.test(keywordStr) && keyword.length > 0)
return "The key must consist only of letters";
const polybius = Cipher._genPolybiusSquare(keywordStr);
input.replace("J", "I").split("").forEach((letter) => { input.replace("J", "I").split("").forEach((letter) => {
let alpInd = alpha.split("").indexOf(letter.toLocaleUpperCase()) >= 0, let alpInd = alpha.split("").indexOf(letter.toLocaleUpperCase()) >= 0,
polInd; polInd;
if (alpInd) { if (alpInd) {
for (let i = 0; i < 5; i++) { for (let i = 0; i < 5; i++) {
polInd = polybius[i].indexOf(letter.toLocaleUpperCase()); polInd = polybius[i].indexOf(letter.toLocaleUpperCase());
@ -700,6 +719,7 @@ const Cipher = {
trans += `${i}${polInd}`; trans += `${i}${polInd}`;
} }
} }
if (alpha.split("").indexOf(letter) >= 0) { if (alpha.split("").indexOf(letter) >= 0) {
structure.push(true); structure.push(true);
} else if (alpInd) { } else if (alpInd) {
@ -709,22 +729,24 @@ const Cipher = {
structure.push(letter); structure.push(letter);
} }
}); });
structure.forEach(pos => { structure.forEach(pos => {
if (typeof pos === "boolean") { if (typeof pos === "boolean") {
let coords = [trans[count], trans[count+trans.length/2]]; let coords = [trans[count], trans[count+trans.length/2]];
if (pos) {
output += polybius[coords[0]][coords[1]]; output += pos ?
} else { polybius[coords[0]][coords[1]] :
output += polybius[coords[0]][coords[1]].toLocaleLowerCase(); polybius[coords[0]][coords[1]].toLocaleLowerCase();
}
count++; count++;
} else { } else {
output += pos; output += pos;
} }
}); });
return output; return output;
}, },
/** /**
* @constant * @constant
* @default * @default