BifidCipherEncode.mjs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * @author n1474335 [n1474335@gmail.com]
  3. * @copyright Crown Copyright 2016
  4. * @license Apache-2.0
  5. */
  6. import Operation from "../Operation";
  7. import { genPolybiusSquare } from "../lib/Ciphers";
  8. /**
  9. * Bifid Cipher Encode operation
  10. */
  11. class BifidCipherEncode extends Operation {
  12. /**
  13. * BifidCipherEncode constructor
  14. */
  15. constructor() {
  16. super();
  17. this.name = "Bifid Cipher Encode";
  18. this.module = "Ciphers";
  19. 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.";
  20. this.inputType = "string";
  21. this.outputType = "string";
  22. this.args = [
  23. {
  24. "name": "Keyword",
  25. "type": "string",
  26. "value": ""
  27. }
  28. ];
  29. }
  30. /**
  31. * @param {string} input
  32. * @param {Object[]} args
  33. * @returns {string}
  34. */
  35. run(input, args) {
  36. const keywordStr = args[0].toUpperCase().replace("J", "I"),
  37. keyword = keywordStr.split("").unique(),
  38. alpha = "ABCDEFGHIKLMNOPQRSTUVWXYZ",
  39. xCo = [],
  40. yCo = [],
  41. structure = [];
  42. let output = "",
  43. count = 0;
  44. if (keyword.length > 25)
  45. return "The alphabet keyword must be less than 25 characters.";
  46. if (!/^[a-zA-Z]+$/.test(keywordStr) && keyword.length > 0)
  47. return "The key must consist only of letters";
  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. xCo.push(polInd);
  57. yCo.push(i);
  58. }
  59. }
  60. if (alpha.split("").indexOf(letter) >= 0) {
  61. structure.push(true);
  62. } else if (alpInd) {
  63. structure.push(false);
  64. }
  65. } else {
  66. structure.push(letter);
  67. }
  68. });
  69. const trans = `${yCo.join("")}${xCo.join("")}`;
  70. structure.forEach(pos => {
  71. if (typeof pos === "boolean") {
  72. const coords = trans.substr(2*count, 2).split("");
  73. output += pos ?
  74. polybius[coords[0]][coords[1]] :
  75. polybius[coords[0]][coords[1]].toLocaleLowerCase();
  76. count++;
  77. } else {
  78. output += pos;
  79. }
  80. });
  81. return output;
  82. }
  83. /**
  84. * Highlight Bifid Cipher Encode
  85. *
  86. * @param {Object[]} pos
  87. * @param {number} pos[].start
  88. * @param {number} pos[].end
  89. * @param {Object[]} args
  90. * @returns {Object[]} pos
  91. */
  92. highlight(pos, args) {
  93. return pos;
  94. }
  95. /**
  96. * Highlight Bifid Cipher Encode in reverse
  97. *
  98. * @param {Object[]} pos
  99. * @param {number} pos[].start
  100. * @param {number} pos[].end
  101. * @param {Object[]} args
  102. * @returns {Object[]} pos
  103. */
  104. highlightReverse(pos, args) {
  105. return pos;
  106. }
  107. }
  108. export default BifidCipherEncode;