{"record":{"id":"ef3173be8be3e45c","repo":"gchq/CyberChef","slug":"key-has-to-be-bigger-than-2","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/RailFenceCipherDecode.mjs","lineNumber":52,"sourceCode":"                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 cipher = input;\n\n        if (key < 2) {\n            throw new OperationError(\"Key has to be bigger than 2\");\n        } else if (key > cipher.length) {\n            throw new OperationError(\"Key should be smaller than the cipher'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 plaintext = new Array(cipher.length);\n\n        let j = 0;\n        let x, y;\n\n        for (y = 0; y < key; y++) {\n            for (x = 0; x < cipher.length; x++) {\n                if ((y + x + offset) % cycle === 0 || (y - x - offset) % cycle === 0) {\n                    plaintext[x] = cipher[j++];","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/RailFenceCipherDecode.mjs#L34-L70","documentation":"Thrown by RailFenceCipherDecode when the 'key' argument (number of rails) is less than 2. The rail fence cipher needs at least two rails to form a zigzag. Note the message says 'bigger than 2' but the guard is `key < 2`, so the true minimum accepted value is 2.","triggerScenarios":"Entering a key of 0 or 1; passing a negative number; the key field defaulting to 0/1 in a malformed recipe.","commonSituations":"Typo in the key field; loading a recipe/config that omitted or zeroed the key; misreading the message and trying key=2 believing it is rejected.","solutions":["Set the key to an integer >= 2.","Confirm the key is supplied as a number, not a string that coerces to NaN.","Remember the misleading message: the real floor is 2, not 3."],"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 { decode(input, { key }); } catch (e) { if (/bigger than 2/.test(e.message)) key = Math.max(2, key); else throw e; }","preventionTips":["Coerce key to an integer and clamp to >= 2.","Remember the message wording is off; the real minimum is 2."],"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"}