|
@@ -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 = [],
|
|
|
|
- polString = "";
|
|
|
|
- keyword.split("").unique().forEach(letter => {
|
|
|
|
- polString += letter;
|
|
|
|
- });
|
|
|
|
- polString = `${polString}${alpha}`.split("").unique().join("");
|
|
|
|
|
|
+ const polArray = `${keyword}${alpha}`.split("").unique();
|
|
|
|
+ let polybius = [];
|
|
|
|
+
|
|
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,
|
|
|
|
- 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";
|
|
|
|
- const polybius = Cipher._genPolybiusSquare(keyword);
|
|
|
|
- input.replace("J", "I").split("").forEach((letter) => {
|
|
|
|
|
|
+ count = 0;
|
|
|
|
+
|
|
|
|
+ if (keyword.length > 25)
|
|
|
|
+ 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 => {
|
|
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]];
|
|
|
|
- } else {
|
|
|
|
- output += polybius[coords[0]][coords[1]].toLocaleLowerCase();
|
|
|
|
- }
|
|
|
|
|
|
+
|
|
|
|
+ output += pos ?
|
|
|
|
+ polybius[coords[0]][coords[1]] :
|
|
|
|
+ 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";
|
|
|
|
- const polybius = Cipher._genPolybiusSquare(keyword);
|
|
|
|
|
|
+
|
|
|
|
+ if (keyword.length > 25)
|
|
|
|
+ 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]];
|
|
|
|
- } else {
|
|
|
|
- output += polybius[coords[0]][coords[1]].toLocaleLowerCase();
|
|
|
|
- }
|
|
|
|
|
|
+
|
|
|
|
+ output += pos ?
|
|
|
|
+ polybius[coords[0]][coords[1]] :
|
|
|
|
+ polybius[coords[0]][coords[1]].toLocaleLowerCase();
|
|
count++;
|
|
count++;
|
|
} else {
|
|
} else {
|
|
output += pos;
|
|
output += pos;
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
+
|
|
return output;
|
|
return output;
|
|
},
|
|
},
|
|
|
|
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* @constant
|
|
* @constant
|
|
* @default
|
|
* @default
|