{"record":{"id":"8679472d14c2f4c4","repo":"gchq/CyberChef","slug":"key-should-be-smaller-than-the-plain-text-s-length","errorCode":null,"errorMessage":"Key should be smaller than the plain text's length","messagePattern":"Key should be smaller than the plain text's length","errorType":"validation","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/RailFenceCipherEncode.mjs","lineNumber":53,"sourceCode":"                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(\"\");\n    }\n","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/RailFenceCipherEncode.mjs#L35-L71","documentation":"Thrown by RailFenceCipherEncode when the number of rails (key) exceeds the plaintext length. More rails than characters makes the zigzag degenerate, so encoding is rejected.","triggerScenarios":"Short plaintext (e.g. 4 chars) with a large key (e.g. 10); empty/short input combined with a default large key.","commonSituations":"Testing the encoder on a tiny string; key designed for a longer message; plaintext truncated.","solutions":["Lower the key to <= the plaintext length.","Provide a longer plaintext, or pad it.","Match the key to the actual input size."],"exampleFix":"// before\n//   plaintext \"HI\" (len 2), key 5 -> error\n// after\n//   plaintext \"HI\", key 2","handlingStrategy":"validation","validationCode":"if (key > plaintext.length) throw new Error('Key must be <= plaintext length');","typeGuard":"const keyFits = (k, text) => Number.isInteger(k) && k >= 2 && k <= text.length;","tryCatchPattern":"try { encode(text, { key }); } catch (e) { if (/smaller than the plain/.test(e.message)) key = text.length; else throw e; }","preventionTips":["Scale key to plaintext length.","Pad plaintext if a specific key is required."],"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"}