{"record":{"id":"75c47ddc501323b9","repo":"gchq/CyberChef","slug":"invalid-iv-length-iv-length-bytes-twofish-use-75c47d","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/TwofishEncrypt.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 = encryptTwofish(input, key, iv, mode, padding);\n        return outputType === \"Hex\" ? toHex(output, \"\") : Utils.byteArrayToUtf8(output);\n    }\n\n}\n\nexport default TwofishEncrypt;\n","sourceCodeStart":64,"sourceCodeEnd":95,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/TwofishEncrypt.mjs#L64-L95","documentation":"Thrown by 'Twofish Encrypt' when the IV is not 16 bytes and mode is not ECB. Twofish's 128-bit block dictates a 16-byte IV; the message flags the common Hex-vs-UTF8 mistake.","triggerScenarios":"Non-ECB mode with a non-16-byte IV: e.g. an 8-byte IV, empty IV, or a 32-char hex IV read as UTF8.","commonSituations":"Format option mismatch, reusing a DES/3DES IV, or omitting the IV for a chaining mode.","solutions":["Provide a 16-byte IV.","Verify the IV format option.","Use ECB if you intend no IV."],"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":["Set the IV format option correctly.","Use ECB only when omitting the IV intentionally."],"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"}