From e7515ffa63c74078ec09298f5fdcc7277e1f1d98 Mon Sep 17 00:00:00 2001 From: Flavio Diez Date: Mon, 27 Jan 2020 15:02:39 +0100 Subject: [PATCH] Add Rail Fence Cipher Encode --- src/core/config/Categories.json | 1 + src/core/operations/RailFenceCypherEncode.mjs | 96 +++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 src/core/operations/RailFenceCypherEncode.mjs diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index cc9268fc..0d68efe8 100755 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -95,6 +95,7 @@ "Affine Cipher Decode", "A1Z26 Cipher Encode", "A1Z26 Cipher Decode", + "Rail Fence Cipher Encode", "Rail Fence Cipher Decode", "Atbash Cipher", "Substitute", diff --git a/src/core/operations/RailFenceCypherEncode.mjs b/src/core/operations/RailFenceCypherEncode.mjs new file mode 100644 index 00000000..8da1830b --- /dev/null +++ b/src/core/operations/RailFenceCypherEncode.mjs @@ -0,0 +1,96 @@ +/** + * @author Flavio Diez [flaviofdiez+cyberchef@gmail.com] + * @copyright Crown Copyright 2020 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; + +/** + * Rail Fence Cypher Encode operation + */ +class RailFenceCypherEncode extends Operation { + + /** + * RailFenceCypherEncode constructor + */ + constructor() { + super(); + + this.name = "Rail Fence Cypher Encode"; + this.module = "Ciphers"; + this.description = "Encodes Strings using the Rail fence Cypher provided a key and an offset"; + this.infoURL = "https://en.wikipedia.org/wiki/Rail_fence_cipher"; + this.inputType = "string"; + this.outputType = "string"; + this.args = [ + { + name: "Key", + type: "number", + value: 2 + }, + { + name: "Offset", + type: "number", + value: 0 + } + ]; + } + + /** + * @param {string} input + * @param {Object[]} args + * @returns {string} + */ + run(input, args) { + const [key] = args; + // const [key, offset] = args; + + const plaintext = input; + + if (key < 2) { + return "Key has to be bigger than 2"; + } else if (key > plaintext.length) { + return "Key should be smaller than the plain text's length"; + } + + let cipher = ""; + + for (let block = 0; block < key; block++) { + for (let pos = block; pos < plaintext.length; pos += key) { + cipher += plaintext[pos]; + } + } + + return cipher; + } + + /** + * Highlight Rail Fence Cypher Encode + * + * @param {Object[]} pos + * @param {number} pos[].start + * @param {number} pos[].end + * @param {Object[]} args + * @returns {Object[]} pos + */ + highlight(pos, args) { + return pos; + } + + /** + * Highlight Rail Fence Cypher Encode in reverse + * + * @param {Object[]} pos + * @param {number} pos[].start + * @param {number} pos[].end + * @param {Object[]} args + * @returns {Object[]} pos + */ + highlightReverse(pos, args) { + return pos; + } + +} + +export default RailFenceCypherEncode;