From ce638de9b1c500ea53589133c1aebb2ad802e965 Mon Sep 17 00:00:00 2001 From: Flavio Diez Date: Mon, 27 Jan 2020 14:43:00 +0100 Subject: [PATCH] Corrected padding and added error cases --- src/core/operations/RailFenceCypherDecode.mjs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/core/operations/RailFenceCypherDecode.mjs b/src/core/operations/RailFenceCypherDecode.mjs index 1c9c67b5..20a8a4f2 100644 --- a/src/core/operations/RailFenceCypherDecode.mjs +++ b/src/core/operations/RailFenceCypherDecode.mjs @@ -48,10 +48,16 @@ class RailFenceCypherDecode extends Operation { let cipher = input; + if (key < 2) { + return "Key has to be bigger than 2"; + } else if (key > cipher.length) { + return "Key should be smaller than the cipher's length"; + } + const rest = cipher.length % key; if (rest !== 0) { - cipher = cipher + (" ".repeat(rest)); + cipher = cipher + (" ".repeat(key-rest)); } const blockLen = cipher.length / key;