{"record":{"id":"ca91d412cf1aa448","repo":"gchq/CyberChef","slug":"key-should-be-smaller-than-the-cipher-s-length","errorCode":null,"errorMessage":"Key should be smaller than the cipher's length","messagePattern":"Key should be smaller than the cipher's length","errorType":"validation","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/RailFenceCipherDecode.mjs","lineNumber":54,"sourceCode":"                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++];\n                }\n            }","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/RailFenceCipherDecode.mjs#L36-L72","documentation":"Thrown by RailFenceCipherDecode when the number of rails (key) exceeds the cipher-text length. With more rails than characters, the zigzag pattern cannot place every input character, so decoding is undefined.","triggerScenarios":"Short ciphertext (e.g. 5 chars) combined with a large key (e.g. 10); pasting a truncated ciphertext; key sized for a different (longer) message.","commonSituations":"Reusing a recipe with a long key on short test input; ciphertext truncated during copy-paste; mismatching key and ciphertext from different encryptions.","solutions":["Reduce the key so it is <= the ciphertext length.","Verify the full ciphertext was pasted (no truncation).","Use the same key that was used to encode the original message."],"exampleFix":"// before\n//   ciphertext \"HELLO\" (len 5), key 8 -> error\n// after\n//   ciphertext \"HELLO\", key 3","handlingStrategy":"validation","validationCode":"if (key > cipher.length) throw new Error('Key must be <= ciphertext length');","typeGuard":"const keyFits = (k, text) => Number.isInteger(k) && k >= 2 && k <= text.length;","tryCatchPattern":"try { decode(cipher, { key }); } catch (e) { if (/smaller than the cipher/.test(e.message)) key = cipher.length; else throw e; }","preventionTips":["Scale the key to the ciphertext length.","Verify ciphertext completeness before decoding."],"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"}