{"record":{"id":"39c58c3c081a32c9","repo":"gchq/CyberChef","slug":"key-has-to-be-bigger-than-2-39c58c","errorCode":null,"errorMessage":"Key has to be bigger than 2","messagePattern":"Key has to be bigger than 2","errorType":"validation","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/RailFenceCipherEncode.mjs","lineNumber":51,"sourceCode":"            {\n                name: \"Offset\",\n                type: \"number\",\n                value: 0\n            }\n        ];\n    }\n\n    /**\n     * @param {string} input\n     * @param {Object[]} args\n     * @returns {string}\n     */\n    run(input, args) {\n        const [key, offset] = args;\n\n        const plaintext = input;\n        if (key < 2) {\n            throw new OperationError(\"Key has to be bigger than 2\");\n        } else if (key > plaintext.length) {\n            throw new OperationError(\"Key should be smaller than the plain text's length\");\n        }\n\n        if (offset < 0) {\n            throw new OperationError(\"Offset has to be a positive integer\");\n        }\n\n        const cycle = (key - 1) * 2;\n        const rows = new Array(key).fill(\"\");\n\n        for (let pos = 0; pos < plaintext.length; pos++) {\n            const rowIdx = key - 1 - Math.abs(cycle / 2 - (pos + offset) % cycle);\n\n            rows[rowIdx] += plaintext[pos];\n        }\n\n        return rows.join(\"\");","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/RailFenceCipherEncode.mjs#L33-L69","documentation":"Thrown by RailFenceCipherEncode when the 'key' (number of rails) is less than 2. The encoder mirrors the decoder's guard. The message reads 'bigger than 2' but the code `key < 2` means the actual minimum is 2.","triggerScenarios":"Key argument of 0 or 1; negative key; key field unset in a recipe.","commonSituations":"Typo in the key field; default key not set; misreading the message.","solutions":["Set the key to an integer >= 2.","Confirm the key is a number, not a NaN-coercing string.","Note the message wording: 2 is the true minimum."],"exampleFix":"// before\n//   key: 1\n// after\n//   key: 3","handlingStrategy":"validation","validationCode":"if (!Number.isInteger(key) || key < 2) throw new Error('Rail fence key must be an integer >= 2');","typeGuard":"const isRailKey = k => Number.isInteger(k) && k >= 2;","tryCatchPattern":"try { encode(input, { key }); } catch (e) { if (/bigger than 2/.test(e.message)) key = 2; else throw e; }","preventionTips":["Coerce key to integer >= 2.","Note the message wording: 2 is valid."],"tags":["cipher","rail-fence","key-validation","validation"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}