{"record":{"id":"e6c4b10a5af130fd","repo":"gchq/CyberChef","slug":"the-key-must-consist-only-of-letters-in-the-englis-e6c4b1","errorCode":null,"errorMessage":"The key must consist only of letters in the English alphabet","messagePattern":"The key must consist only of letters in the English alphabet","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/BifidCipherEncode.mjs","lineNumber":57,"sourceCode":"     * @param {Object[]} args\n     * @returns {string}\n     *\n     * @throws {OperationError} if key is invalid\n     */\n    run(input, args) {\n        const keywordStr = args[0].toUpperCase().replace(\"J\", \"I\"),\n            keyword = keywordStr.split(\"\").unique(),\n            alpha = \"ABCDEFGHIKLMNOPQRSTUVWXYZ\",\n            xCo = [],\n            yCo = [],\n            structure = [];\n\n        let output = \"\",\n            count = 0;\n\n\n        if (!/^[A-Z]+$/.test(keywordStr) && keyword.length > 0)\n            throw new OperationError(\"The key must consist only of letters in the English alphabet\");\n\n        const polybius = genPolybiusSquare(keywordStr);\n\n        input.replace(\"J\", \"I\").split(\"\").forEach(letter => {\n            const alpInd = alpha.split(\"\").indexOf(letter.toLocaleUpperCase()) >= 0;\n            let polInd;\n\n            if (alpInd) {\n                for (let i = 0; i < 5; i++) {\n                    polInd = polybius[i].indexOf(letter.toLocaleUpperCase());\n                    if (polInd >= 0) {\n                        xCo.push(polInd);\n                        yCo.push(i);\n                    }\n                }\n\n                if (alpha.split(\"\").indexOf(letter) >= 0) {\n                    structure.push(true);","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/BifidCipherEncode.mjs#L39-L75","documentation":"The Bifid cipher builds a 5x5 Polybius square from a keyword (with J merged into I). After uppercasing and the J->I replacement, the keyword string must consist only of A-Z letters. This OperationError fires when non-alphabetic characters survive that preprocessing and at least one unique letter remains, because non-letter glyphs cannot be placed in the Polybius grid.","triggerScenarios":"Calling BifidCipherEncode.run with an args[0] key that contains digits, punctuation, whitespace, or accented/non-Latin characters while also containing at least one letter (so keyword.length > 0 and /^[A-Z]+$/ fails).","commonSituations":"User pastes a passphrase containing spaces, numbers, or symbols; an IME or autocorrect inserts non-ASCII characters; a config/UI forwards an unvalidated key string.","solutions":["Sanitize the key before invoking the operation: strip everything except A-Z/a-z (key.replace(/[^A-Za-z]/g, '')).","Leave the key empty if you want the default alphabet square (the check only fires when unique-letter count > 0).","Validate the key in your UI/config layer with /^[A-Za-z]+$/ before passing it as args[0]."],"exampleFix":"// before\nconst args = ['my secret 123'];\n// after\nconst args = ['mysecret'];","handlingStrategy":"validation","validationCode":"const key = (args[0] || '').toUpperCase().replace(/J/g, 'I');\nif (key.length > 0 && !/^[A-Z]+$/.test(key)) {\n  throw new Error('Bifid key must be A-Z only');\n}","typeGuard":"function isBifidKey(k) {\n  const s = String(k).toUpperCase().replace(/J/g, 'I');\n  return s.length === 0 || /^[A-Z]+$/.test(s);\n}","tryCatchPattern":"try { bifidEncode.run(input, [key]); }\ncatch (e) { if (/letters in the English alphabet/.test(e.message)) { /* sanitize key */ } else throw e; }","preventionTips":["Validate the key with /^[A-Za-z]+$/ in the UI before adding the operation to the recipe.","Strip non-letter characters from user input with replace(/[^A-Za-z]/g, '').","Treat an empty key as intentional rather than an error."],"tags":["cipher","validation","alphabet","bifid"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}