{"record":{"id":"1b35ea6a0675e494","repo":"gchq/CyberChef","slug":"invalid-key-length-key-length-bytes-the-follo","errorCode":null,"errorMessage":"Invalid key length: ${key.length} bytes\n\nThe following algorithms will be used based on the size of the key:\n  16 bytes = AES-128\n  24 bytes = AES-192\n  32 bytes = AES-256","messagePattern":"Invalid key length: (.+?) bytes\n\nThe following algorithms will be used based on the size of the key:\n  16 bytes = AES-128\n  24 bytes = AES-192\n  32 bytes = AES-256","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/AESDecrypt.mjs","lineNumber":152,"sourceCode":"     *\n     * @throws {OperationError} if cannot decrypt input or invalid key length\n     */\n    run(input, args) {\n        let iv;\n\n        const key = Utils.convertToByteString(args[0].string, args[0].option),\n            ivLength = args[2],\n            mode = args[3].split(\"/\")[0],\n            noPadding = args[3].endsWith(\"NoPadding\"),\n            inputType = args[4],\n            outputType = args[5],\n            gcmTag = Utils.convertToByteString(args[6].string, args[6].option),\n            aad = Utils.convertToByteString(args[7].string, args[7].option),\n            ivFromInput = args[8];\n\n\n        if ([16, 24, 32].indexOf(key.length) < 0) {\n            throw new OperationError(`Invalid key length: ${key.length} bytes\n\nThe following algorithms will be used based on the size of the key:\n  16 bytes = AES-128\n  24 bytes = AES-192\n  32 bytes = AES-256`);\n        }\n\n        input = Utils.convertToByteString(input, inputType);\n\n        if (ivFromInput !== \"Off\") {\n            if (input.length <= ivLength) {\n                throw new OperationError(`Input is too short to contain an IV of ${ivLength} bytes.`);\n            }\n\n            if (ivFromInput === \"From start\") {\n                iv = input.substr(0, ivLength);\n                input = input.substr(ivLength);\n            } else {","sourceCodeStart":134,"sourceCodeEnd":170,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/AESDecrypt.mjs#L134-L170","documentation":"AES only accepts key sizes of 16, 24, or 32 bytes (AES-128/192/256 respectively). After converting the key argument to a byte string, AESDecrypt checks key.length against [16, 24, 32] and throws this OperationError if it is absent. This is enforced before any decryption begins.","triggerScenarios":"The key converted via Utils.convertToByteString(args[0].string, args[0].option) yields a length not equal to 16, 24, or 32. Common causes: the key was entered as a 16-char ASCII passphrase but interpreted as Hex (giving 8 bytes), a 32-char hex string interpreted as UTF8 (giving 32 bytes instead of 16), or an empty/short key.","commonSituations":"Key encoding option (UTF8 / Hex / Base64) does not match the actual format of the pasted key; a human-memorable passphrase is used directly as an AES key instead of being run through a KDF; key was truncated during copy-paste; mismatch between the key format used at encryption time and at decryption time.","solutions":["Verify the Key Format option matches the bytes: select Hex for hex-encoded keys, Base64 for base64, UTF8 only for raw text.","Compute the exact byte length you expect (16/24/32) and confirm the input produces it.","If using a passphrase, derive the key with a KDF (PBKDF2/EVP_BytesToKey) so the output is exactly 16/24/32 bytes.","Pad with zeros or hash (SHA-256 → 32 bytes) to reach a valid length only if that matches how the ciphertext was produced."],"exampleFix":"// before: key \"000102030405060708090a0b0c0d0e0f\" with format UTF8 → 32 bytes (AES-256) when AES-128 intended\n// after: set Key Format to \"Hex\" so the same string → 16 bytes (AES-128)","handlingStrategy":"validation","validationCode":"function validateAesKey(keyBytes) {\n  if (![16, 24, 32].includes(keyBytes.length)) {\n    throw new Error(`Key must be 16/24/32 bytes, got ${keyBytes.length}`);\n  }\n}","typeGuard":"function isAesKey(bytes) {\n  return bytes instanceof Uint8Array && [16, 24, 32].includes(bytes.length);\n}","tryCatchPattern":"try { decryptAES(...); } catch (e) { if (/Invalid key length/.test(e.message)) {/* fix key format */} else throw e; }","preventionTips":["Always pair the key string with the correct format option (Hex/Base64/UTF8).","Derive passphrases through PBKDF2 to a fixed 16/24/32-byte length.","Assert the byte length before calling decrypt."],"tags":["aes","crypto","key-management","validation"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}