{"record":{"id":"fb08704fa6adbe54","repo":"gchq/CyberChef","slug":"invalid-key-length-key-length-bytes-chacha-u","errorCode":null,"errorMessage":"Invalid key length: ${key.length} bytes.\n\nChaCha uses a key of 16 or 32 bytes (128 or 256 bits).","messagePattern":"Invalid key length: (.+?) bytes\\.\n\nChaCha uses a key of 16 or 32 bytes \\(128 or 256 bits\\)\\.","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/ChaCha.mjs","lineNumber":158,"sourceCode":"                \"value\": [\"Raw\", \"Hex\"]\n            }\n        ];\n    }\n\n    /**\n     * @param {string} input\n     * @param {Object[]} args\n     * @returns {string}\n     */\n    run(input, args) {\n        const key = Utils.convertToByteArray(args[0].string, args[0].option),\n            nonceType = args[1].option,\n            rounds = parseInt(args[3], 10),\n            inputType = args[4],\n            outputType = args[5];\n\n        if (key.length !== 16 && key.length !== 32) {\n            throw new OperationError(`Invalid key length: ${key.length} bytes.\n\nChaCha uses a key of 16 or 32 bytes (128 or 256 bits).`);\n        }\n\n        let counter, nonce, counterLength;\n        if (nonceType === \"Integer\") {\n            nonce = Utils.intToByteArray(parseInt(args[1].string, 10), 12, \"little\");\n            counterLength = 4;\n        } else {\n            nonce = Utils.convertToByteArray(args[1].string, args[1].option);\n            if (!(nonce.length === 12 || nonce.length === 8)) {\n                throw new OperationError(`Invalid nonce length: ${nonce.length} bytes.\n\nChaCha uses a nonce of 8 or 12 bytes (64 or 96 bits).`);\n            }\n            counterLength = 16 - nonce.length;\n        }\n        counter = Utils.intToByteArray(args[2], counterLength, \"little\");","sourceCodeStart":140,"sourceCodeEnd":176,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/ChaCha.mjs#L140-L176","documentation":"Thrown by the ChaCha operation when the resolved key byte array is neither 16 nor 32 bytes long. ChaCha is defined only for 128-bit (16-byte) and 256-bit (32-byte) keys, so any other length is rejected before keystream generation.","triggerScenarios":"ChaCha.run builds key via Utils.convertToByteArray(args[0].string, args[0].option); if the resulting byte length is not exactly 16 or 32, it throws. This happens with Hex strings of wrong nibble count, Base64 decoding to odd lengths, UTF-8 passphrase input, or a truncated/overlong key.","commonSituations":"User pastes a 32-hex-char string (16 bytes) intending a 256-bit key but expects 64 hex chars; supplies a human passphrase as the key; picks 'Latin1' on multi-byte data producing unexpected length; copies a key with a leading/trailing space altering length.","solutions":["Generate exactly 16 or 32 raw bytes (32 or 64 hex characters, or ~22/44 Base64 chars).","Pick the input option matching your key encoding (Hex for hex, Base64 for base64).","Strip whitespace/newlines from pasted keys.","For a 256-bit key use 64 hex chars; for 128-bit use 32 hex chars."],"exampleFix":"// before — 30 hex chars → 15 bytes, throws\n// key (hex): \"00112233445566778899aabbccddeeffa0\"\n// after — 32 hex chars → 16 bytes\n// key (hex): \"00112233445566778899aabbccddeeff\"","handlingStrategy":"validation","validationCode":"const key = Utils.convertToByteArray(args[0].string, args[0].option);\nif (key.length !== 16 && key.length !== 32) {\n  // reject before ChaCha.run\n}","typeGuard":"function isValidChaChaKey(key) { return key.length === 16 || key.length === 32; }","tryCatchPattern":"null","preventionTips":["Generate keys of exactly 16 or 32 raw bytes.","Match the key input option to its encoding (Hex = 32/64 chars).","Strip whitespace/newlines from pasted keys.","Never pass a raw passphrase as the key."],"tags":["chacha","cryptography","key-length","input-validation"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}