{"record":{"id":"079776e248606f04","repo":"gchq/CyberChef","slug":"invalid-iv-length-iv-length-bytes-twofish-use","errorCode":null,"errorMessage":"Invalid IV length: ${iv.length} bytes\n\nTwofish uses an IV length of 16 bytes (128 bits).\nMake sure you have specified the type correctly (e.g. Hex vs UTF8).","messagePattern":"Invalid IV length: (.+?) bytes\n\nTwofish uses an IV length of 16 bytes \\(128 bits\\)\\.\nMake sure you have specified the type correctly \\(e\\.g\\. Hex vs UTF8\\)\\.","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/TwofishDecrypt.mjs","lineNumber":82,"sourceCode":"    }\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            [,, mode, inputType, outputType, padding] = args;\n\n        if (key.length !== 16 && key.length !== 24 && key.length !== 32)\n            throw new OperationError(`Invalid key length: ${key.length} bytes\n\nTwofish uses a key length of 16 bytes (128 bits), 24 bytes (192 bits), or 32 bytes (256 bits).`);\n\n        if (iv.length !== 16 && mode !== \"ECB\")\n            throw new OperationError(`Invalid IV length: ${iv.length} bytes\n\nTwofish uses an IV length of 16 bytes (128 bits).\nMake sure you have specified the type correctly (e.g. Hex vs UTF8).`);\n\n        input = Utils.convertToByteArray(input, inputType);\n        const output = decryptTwofish(input, key, iv, mode, padding);\n        return outputType === \"Hex\" ? toHex(output, \"\") : Utils.byteArrayToUtf8(output);\n    }\n\n}\n\nexport default TwofishDecrypt;\n","sourceCodeStart":64,"sourceCodeEnd":95,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/TwofishDecrypt.mjs#L64-L95","documentation":"Thrown by 'Twofish Decrypt' when the IV is not 16 bytes and the mode is not ECB. Twofish has a 128-bit block, so the IV must be 16 bytes. The error message also warns about Hex-vs-UTF8 type confusion.","triggerScenarios":"Non-ECB mode with an IV whose decoded length is not 16: an 8-byte DES IV, an empty IV, or a 32-char hex IV read as UTF8 (32 bytes).","commonSituations":"Reusing an IV from a 64-bit-block cipher, leaving the IV blank for CBC/CFB/OFB/CTR, or a format-option mismatch.","solutions":["Provide a 16-byte IV (32 hex chars with Hex option).","Ensure the IV format option matches the IV string.","Switch to ECB if no IV is intended."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"const ivBytes = Utils.convertToByteArray(iv.string, iv.option);\nif (mode !== \"ECB\" && ivBytes.length !== 16) {\n  throw new Error(`Twofish IV must be 16 bytes, got ${ivBytes.length}`);\n}","typeGuard":"function isValidTwofishIv(bytes, mode) { return mode === \"ECB\" || bytes.length === 16; }","tryCatchPattern":null,"preventionTips":["Match IV format option to IV encoding.","Do not reuse 64-bit-block IVs for Twofish."],"tags":["crypto","twofish","validation","iv-length","argument-error"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}