BifidCipherDecode.mjs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * @author Matt C [matt@artemisbot.uk]
  3. * @copyright Crown Copyright 2018
  4. * @license Apache-2.0
  5. */
  6. import Operation from "../Operation";
  7. import { genPolybiusSquare } from "../lib/Ciphers";
  8. import OperationError from "../errors/OperationError";
  9. /**
  10. * Bifid Cipher Decode operation
  11. */
  12. class BifidCipherDecode extends Operation {
  13. /**
  14. * BifidCipherDecode constructor
  15. */
  16. constructor() {
  17. super();
  18. this.name = "Bifid Cipher Decode";
  19. this.module = "Ciphers";
  20. this.description = "The Bifid cipher is a cipher which uses a Polybius square in conjunction with transposition, which can be fairly difficult to decipher without knowing the alphabet keyword.";
  21. this.inputType = "string";
  22. this.outputType = "string";
  23. this.args = [
  24. {
  25. "name": "Keyword",
  26. "type": "string",
  27. "value": ""
  28. }
  29. ];
  30. }
  31. /**
  32. * @param {string} input
  33. * @param {Object[]} args
  34. * @returns {string}
  35. *
  36. * @throws {OperationError} if invalid key
  37. */
  38. run(input, args) {
  39. const keywordStr = args[0].toUpperCase().replace("J", "I"),
  40. keyword = keywordStr.split("").unique(),
  41. alpha = "ABCDEFGHIKLMNOPQRSTUVWXYZ",
  42. structure = [];
  43. let output = "",
  44. count = 0,
  45. trans = "";
  46. if (!/^[A-Z]+$/.test(keywordStr) && keyword.length > 0)
  47. throw new OperationError("The key must consist only of letters in the English alphabet");
  48. const polybius = genPolybiusSquare(keywordStr);
  49. input.replace("J", "I").split("").forEach((letter) => {
  50. const alpInd = alpha.split("").indexOf(letter.toLocaleUpperCase()) >= 0;
  51. let polInd;
  52. if (alpInd) {
  53. for (let i = 0; i < 5; i++) {
  54. polInd = polybius[i].indexOf(letter.toLocaleUpperCase());
  55. if (polInd >= 0) {
  56. trans += `${i}${polInd}`;
  57. }
  58. }
  59. if (alpha.split("").indexOf(letter) >= 0) {
  60. structure.push(true);
  61. } else if (alpInd) {
  62. structure.push(false);
  63. }
  64. } else {
  65. structure.push(letter);
  66. }
  67. });
  68. structure.forEach(pos => {
  69. if (typeof pos === "boolean") {
  70. const coords = [trans[count], trans[count+trans.length/2]];
  71. output += pos ?
  72. polybius[coords[0]][coords[1]] :
  73. polybius[coords[0]][coords[1]].toLocaleLowerCase();
  74. count++;
  75. } else {
  76. output += pos;
  77. }
  78. });
  79. return output;
  80. }
  81. /**
  82. * Highlight Bifid Cipher Decode
  83. *
  84. * @param {Object[]} pos
  85. * @param {number} pos[].start
  86. * @param {number} pos[].end
  87. * @param {Object[]} args
  88. * @returns {Object[]} pos
  89. */
  90. highlight(pos, args) {
  91. return pos;
  92. }
  93. /**
  94. * Highlight Bifid Cipher Decode in reverse
  95. *
  96. * @param {Object[]} pos
  97. * @param {number} pos[].start
  98. * @param {number} pos[].end
  99. * @param {Object[]} args
  100. * @returns {Object[]} pos
  101. */
  102. highlightReverse(pos, args) {
  103. return pos;
  104. }
  105. }
  106. export default BifidCipherDecode;