gchq/CyberChef · error · OperationError
Invalid key length: ${key.length} bytes SM4 uses a key leng
Error message
Invalid key length: ${key.length} bytes
SM4 uses a key length of 16 bytes (128 bits). What it means
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.
Source
Thrown at src/core/operations/SM4Encrypt.mjs:72
"name": "Output",
"type": "option",
"value": ["Hex", "Raw"]
}
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const key = Utils.convertToByteArray(args[0].string, args[0].option),
iv = Utils.convertToByteArray(args[1].string, args[1].option),
[,, mode, inputType, outputType] = args;
if (key.length !== 16)
throw new OperationError(`Invalid key length: ${key.length} bytes
SM4 uses a key length of 16 bytes (128 bits).`);
if (iv.length !== 16 && !mode.startsWith("ECB"))
throw new OperationError(`Invalid IV length: ${iv.length} bytes
SM4 uses an IV length of 16 bytes (128 bits).
Make sure you have specified the type correctly (e.g. Hex vs UTF8).`);
input = Utils.convertToByteArray(input, inputType);
const output = encryptSM4(input, key, iv, mode.substring(0, 3), mode.endsWith("NoPadding"));
return outputType === "Hex" ? toHex(output) : Utils.byteArrayToUtf8(output);
}
}
export default SM4Encrypt;
View on GitHub (pinned to 4290ea7539)
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.
Example fix
// before
sm4Encrypt.run(pt, [{string:"0123456789abcdef0123456789abcdef", option:"UTF8"}, ...])
// after
sm4Encrypt.run(pt, [{string:"0123456789abcdef0123456789abcdef", option:"Hex"}, ...]) Defensive patterns
Strategy: validation
Validate before calling
const key = Utils.convertToByteArray(keyArg.string, keyArg.option);
if (key.length !== 16) {
throw new Error(`SM4 key must decode to 16 bytes, got ${key.length}. Check toggle.`);
} Type guard
function isSm4KeyArg(arg) {
return Utils.convertToByteArray(arg.string, arg.option).length === 16;
} Prevention
- Use the 'Hex' toggle for hex keys so they decode to 16 bytes.
- Ensure the key is exactly 128 bits before encoding.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Invalid key length: ${key.length} bytes SM4 uses a key leng
- Invalid IV length: ${iv.length} bytes SM4 uses an IV length
- Invalid IV length: ${iv.length} bytes SM4 uses an IV length
- Invalid key length: ${key.length} bytes. Salsa20 uses a key
- Input private key must be in hex; and should be 32 bytes
AI-assisted analysis of gchq/CyberChef@4290ea7539 (2026-08-13).
Data as JSON: /api/errors/a61b2f849fb446f1.
Report an issue: GitHub.