BifidCipherEncode.mjs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 OperationError from "../errors/OperationError";
  8. import { genPolybiusSquare } from "../lib/Ciphers";
  9. /**
  10. * Bifid Cipher Encode operation
  11. */
  12. class BifidCipherEncode extends Operation {
  13. /**
  14. * BifidCipherEncode constructor
  15. */
  16. constructor() {
  17. super();
  18. this.name = "Bifid Cipher Encode";
  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 key is invalid
  37. */
  38. run(input, args) {
  39. const keywordStr = args[0].toUpperCase().replace("J", "I"),
  40. keyword = keywordStr.split("").unique(),
  41. alpha = "ABCDEFGHIKLMNOPQRSTUVWXYZ",
  42. xCo = [],
  43. yCo = [],
  44. structure = [];
  45. let output = "",
  46. count = 0;
  47. if (!/^[A-Z]+$/.test(keywordStr) && keyword.length > 0)
  48. throw new OperationError("The key must consist only of letters in the English alphabet");
  49. const polybius = genPolybiusSquare(keywordStr);
  50. input.replace("J", "I").split("").forEach(letter => {
  51. const alpInd = alpha.split("").indexOf(letter.toLocaleUpperCase()) >= 0;
  52. let polInd;
  53. if (alpInd) {
  54. for (let i = 0; i < 5; i++) {
  55. polInd = polybius[i].indexOf(letter.toLocaleUpperCase());
  56. if (polInd >= 0) {
  57. xCo.push(polInd);
  58. yCo.push(i);
  59. }
  60. }
  61. if (alpha.split("").indexOf(letter) >= 0) {
  62. structure.push(true);
  63. } else if (alpInd) {
  64. structure.push(false);
  65. }
  66. } else {
  67. structure.push(letter);
  68. }
  69. });
  70. const trans = `${yCo.join("")}${xCo.join("")}`;
  71. structure.forEach(pos => {
  72. if (typeof pos === "boolean") {
  73. const coords = trans.substr(2*count, 2).split("");
  74. output += pos ?
  75. polybius[coords[0]][coords[1]] :
  76. polybius[coords[0]][coords[1]].toLocaleLowerCase();
  77. count++;
  78. } else {
  79. output += pos;
  80. }
  81. });
  82. return output;
  83. }
  84. /**
  85. * Highlight Bifid Cipher Encode
  86. *
  87. * @param {Object[]} pos
  88. * @param {number} pos[].start
  89. * @param {number} pos[].end
  90. * @param {Object[]} args
  91. * @returns {Object[]} pos
  92. */
  93. highlight(pos, args) {
  94. return pos;
  95. }
  96. /**
  97. * Highlight Bifid Cipher Encode in reverse
  98. *
  99. * @param {Object[]} pos
  100. * @param {number} pos[].start
  101. * @param {number} pos[].end
  102. * @param {Object[]} args
  103. * @returns {Object[]} pos
  104. */
  105. highlightReverse(pos, args) {
  106. return pos;
  107. }
  108. }
  109. export default BifidCipherEncode;