gchq/CyberChef · error · OperationError

Invalid key length: ${key.length} bytes. ChaCha uses a key

Error message

Invalid key length: ${key.length} bytes.

ChaCha uses a key of 16 or 32 bytes (128 or 256 bits).

What it means

Thrown by the ChaCha operation when the resolved key byte array is neither 16 nor 32 bytes long. ChaCha is defined only for 128-bit (16-byte) and 256-bit (32-byte) keys, so any other length is rejected before keystream generation.

Source

Thrown at src/core/operations/ChaCha.mjs:158

                "value": ["Raw", "Hex"]
            }
        ];
    }

    /**
     * @param {string} input
     * @param {Object[]} args
     * @returns {string}
     */
    run(input, args) {
        const key = Utils.convertToByteArray(args[0].string, args[0].option),
            nonceType = args[1].option,
            rounds = parseInt(args[3], 10),
            inputType = args[4],
            outputType = args[5];

        if (key.length !== 16 && key.length !== 32) {
            throw new OperationError(`Invalid key length: ${key.length} bytes.

ChaCha uses a key of 16 or 32 bytes (128 or 256 bits).`);
        }

        let counter, nonce, counterLength;
        if (nonceType === "Integer") {
            nonce = Utils.intToByteArray(parseInt(args[1].string, 10), 12, "little");
            counterLength = 4;
        } else {
            nonce = Utils.convertToByteArray(args[1].string, args[1].option);
            if (!(nonce.length === 12 || nonce.length === 8)) {
                throw new OperationError(`Invalid nonce length: ${nonce.length} bytes.

ChaCha uses a nonce of 8 or 12 bytes (64 or 96 bits).`);
            }
            counterLength = 16 - nonce.length;
        }
        counter = Utils.intToByteArray(args[2], counterLength, "little");

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Generate exactly 16 or 32 raw bytes (32 or 64 hex characters, or ~22/44 Base64 chars).
  2. Pick the input option matching your key encoding (Hex for hex, Base64 for base64).
  3. Strip whitespace/newlines from pasted keys.
  4. For a 256-bit key use 64 hex chars; for 128-bit use 32 hex chars.

Example fix

// before — 30 hex chars → 15 bytes, throws
// key (hex): "00112233445566778899aabbccddeeffa0"
// after — 32 hex chars → 16 bytes
// key (hex): "00112233445566778899aabbccddeeff"
Defensive patterns

Strategy: validation

Validate before calling

const key = Utils.convertToByteArray(args[0].string, args[0].option);
if (key.length !== 16 && key.length !== 32) {
  // reject before ChaCha.run
}

Type guard

function isValidChaChaKey(key) { return key.length === 16 || key.length === 32; }

Try / catch

null

Prevention

When it happens

Trigger: ChaCha.run builds key via Utils.convertToByteArray(args[0].string, args[0].option); if the resulting byte length is not exactly 16 or 32, it throws. This happens with Hex strings of wrong nibble count, Base64 decoding to odd lengths, UTF-8 passphrase input, or a truncated/overlong key.

Common situations: User pastes a 32-hex-char string (16 bytes) intending a 256-bit key but expects 64 hex chars; supplies a human passphrase as the key; picks 'Latin1' on multi-byte data producing unexpected length; copies a key with a leading/trailing space altering length.

Related errors


AI-assisted analysis of gchq/CyberChef@4290ea7539 (2026-08-13). Data as JSON: /api/errors/fb08704fa6adbe54. Report an issue: GitHub.