{"record":{"id":"c66a50f896e63923","repo":"gchq/CyberChef","slug":"invalid-key-length-key-length-bytes-expected","errorCode":null,"errorMessage":"Invalid key length: ${key.length} bytes (expected: 16)","messagePattern":"Invalid key length: (.+?) bytes \\(expected: 16\\)","errorType":"validation","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/Rabbit.mjs","lineNumber":75,"sourceCode":"        ];\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            iv = Utils.convertToByteArray(args[1].string, args[1].option),\n            endianness = args[2],\n            inputType = args[3],\n            outputType = args[4];\n\n        const littleEndian = endianness === \"Little\";\n\n        if (key.length !== 16) {\n            throw new OperationError(`Invalid key length: ${key.length} bytes (expected: 16)`);\n        }\n        if (iv.length !== 0 && iv.length !== 8) {\n            throw new OperationError(`Invalid IV length: ${iv.length} bytes (expected: 0 or 8)`);\n        }\n\n        // Inner State\n        const X = new Uint32Array(8), C = new Uint32Array(8);\n        let b = 0;\n\n        // Counter System\n        const A = [\n            0x4d34d34d, 0xd34d34d3, 0x34d34d34, 0x4d34d34d,\n            0xd34d34d3, 0x34d34d34, 0x4d34d34d, 0xd34d34d3\n        ];\n        const counterUpdate = function() {\n            for (let j = 0; j < 8; j++) {\n                const temp = C[j] + A[j] + b;\n                b = (temp / ((1 << 30) * 4)) >>> 0;","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/Rabbit.mjs#L57-L93","documentation":"Thrown by the Rabbit stream cipher operation when the supplied key is not exactly 16 bytes (128 bits), the only key size the Rabbit algorithm supports. The key is obtained via Utils.convertToByteArray from the user's key string/option, so its byte length depends on the chosen encoding (Latin1/UTF8/Hex/Base64). If the decoded byte array is not 16 bytes long, execution stops before any cipher state is initialized.","triggerScenarios":"Passing a key whose decoded byte length is not 16 — e.g. a 15-byte Latin1 string, a 31-char hex string, or a Base64 string that decodes to a non-128-bit value. Also triggered by selecting the wrong key input format (Hex when the key is actually UTF-8 text).","commonSituations":"Mistyping the key length; mismatching the 'Key format' dropdown to the actual key encoding; copying a truncated key; assuming Rabbit accepts variable-length keys like a passphrase-derived cipher.","solutions":["Provide exactly 16 bytes: use a 16-character Latin1 string, a 32-character hex string, or Base64 that decodes to 16 bytes.","Verify the 'Key format' option matches the encoding of your key string (Hex/Base64/Latin1/UTF8).","If your key is a passphrase, hash/derive it to 16 bytes (e.g. MD5) before feeding it in, or use a KDF upstream.","Count the key bytes: convert your key with the same encoding and confirm length === 16."],"exampleFix":"// before\n//   key string: \"mysecretkey\" (11 bytes) with format Latin1\n// after\n//   key string: \"mysecretkey12345\" (16 bytes) with format Latin1\n//   or hex: \"6d79736563726574 6b65793132333435\" (32 hex chars -> 16 bytes)","handlingStrategy":"validation","validationCode":"const keyBytes = Utils.convertToByteArray(keyStr, keyOption);\nif (keyBytes.length !== 16) {\n  throw new Error(`Key must be 16 bytes, got ${keyBytes.length}`);\n}","typeGuard":"function isValidRabbitKey(keyBytes) { return ArrayBuffer.isView(keyBytes) && keyBytes.length === 16; }","tryCatchPattern":"try { chef.rabbit(input, { key: keyStr, iv: ivStr }); } catch (e) { if (/Invalid key length/.test(e.message)) fixKeyLength(); else throw e; }","preventionTips":["Normalize the key to exactly 16 bytes (hash or KDF) before invoking Rabbit.","Always pair the key string with its correct format option.","Unit-test key length before running the recipe."],"tags":["crypto","rabbit","key-validation","validation"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}