{"record":{"id":"a61b2f849fb446f1","repo":"gchq/CyberChef","slug":"invalid-key-length-key-length-bytes-sm4-uses-a61b2f","errorCode":null,"errorMessage":"Invalid key length: ${key.length} bytes\n\nSM4 uses a key length of 16 bytes (128 bits).","messagePattern":"Invalid key length: (.+?) bytes\n\nSM4 uses a key length of 16 bytes \\(128 bits\\)\\.","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/SM4Encrypt.mjs","lineNumber":72,"sourceCode":"                \"name\": \"Output\",\n                \"type\": \"option\",\n                \"value\": [\"Hex\", \"Raw\"]\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            iv = Utils.convertToByteArray(args[1].string, args[1].option),\n            [,, mode, inputType, outputType] = args;\n\n        if (key.length !== 16)\n            throw new OperationError(`Invalid key length: ${key.length} bytes\n\nSM4 uses a key length of 16 bytes (128 bits).`);\n        if (iv.length !== 16 && !mode.startsWith(\"ECB\"))\n            throw new OperationError(`Invalid IV length: ${iv.length} bytes\n\nSM4 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 = encryptSM4(input, key, iv, mode.substring(0, 3), mode.endsWith(\"NoPadding\"));\n        return outputType === \"Hex\" ? toHex(output) : Utils.byteArrayToUtf8(output);\n    }\n\n}\n\nexport default SM4Encrypt;\n","sourceCodeStart":54,"sourceCodeEnd":89,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/SM4Encrypt.mjs#L54-L89","documentation":"Thrown by SM4 Encrypt when the key, decoded via Utils.convertToByteArray, is not exactly 16 bytes. SM4 requires a 128-bit key. The key toggle (Hex/UTF8/Latin1/Base64) determines how the key string is decoded before the length is checked.","triggerScenarios":"Same shape as the decrypt variant: the decoded key byte length is not 16. Most often a toggle/encoding mismatch where a 32-char hex key is interpreted as UTF8 (32 bytes) or the key is the wrong length entirely.","commonSituations":"Toggle set to UTF8 for a hex key; blank key (0 bytes); pasting a 256-bit key (32 bytes) intended for AES-256; trailing whitespace.","solutions":["Ensure the decoded key is exactly 16 bytes; for a hex key use the 'Hex' toggle.","Confirm the toggle matches the key encoding.","Strip trailing newlines/whitespace from the key field."],"exampleFix":"// before\nsm4Encrypt.run(pt, [{string:\"0123456789abcdef0123456789abcdef\", option:\"UTF8\"}, ...])\n// after\nsm4Encrypt.run(pt, [{string:\"0123456789abcdef0123456789abcdef\", option:\"Hex\"}, ...])","handlingStrategy":"validation","validationCode":"const key = Utils.convertToByteArray(keyArg.string, keyArg.option);\nif (key.length !== 16) {\n  throw new Error(`SM4 key must decode to 16 bytes, got ${key.length}. Check toggle.`);\n}","typeGuard":"function isSm4KeyArg(arg) {\n  return Utils.convertToByteArray(arg.string, arg.option).length === 16;\n}","tryCatchPattern":null,"preventionTips":["Use the 'Hex' toggle for hex keys so they decode to 16 bytes.","Ensure the key is exactly 128 bits before encoding."],"tags":["crypto","sm4","cipher","key","operation","validation"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}