gchq/CyberChef · error · OperationError

Invalid encoding

Error message

Invalid encoding

What it means

Thrown in EncodeText.run when CHR_ENC_CODE_PAGES[args[0]] is falsy. The Encoding arg is an `option` dropdown whose values are exactly Object.keys(CHR_ENC_CODE_PAGES), so through the normal UI this error is effectively unreachable. It becomes reachable when a recipe is built or imported programmatically (Node API or hand-edited JSON) and references an encoding name absent from the table (typo, removed alias, or a name from a different CyberChef version).

Source

Thrown at src/core/operations/EncodeText.mjs:53

        this.outputType = "ArrayBuffer";
        this.args = [
            {
                "name": "Encoding",
                "type": "option",
                "value": Object.keys(CHR_ENC_CODE_PAGES)
            }
        ];
    }

    /**
     * @param {string} input
     * @param {Object[]} args
     * @returns {ArrayBuffer}
     */
    run(input, args) {
        const format = CHR_ENC_CODE_PAGES[args[0]];
        if (!format) {
            throw new OperationError("Invalid encoding");
        }
        const encoded = cptable.utils.encode(format, input);
        return new Uint8Array(encoded).buffer;
    }

}


export default EncodeText;

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Use an encoding name exactly as it appears in the operation's dropdown (it enumerates CHR_ENC_CODE_PAGES keys).
  2. For programmatic use, validate the name against Object.keys(CHR_ENC_CODE_PAGES) before calling.
  3. When importing recipes, confirm each encoding still exists in the target version.
  4. Avoid trailing spaces / case drift in encoding names.

Example fix

// before
chef.encodeText(text, ['UTF8 ']);        // typo/whitespace -> throws
// after
chef.encodeText(text, ['UTF-8']);         // exact key from CHR_ENC_CODE_PAGES
Defensive patterns

Strategy: validation

Validate before calling

import { CHR_ENC_CODE_PAGES } from "../core/lib/ChrEnc.mjs";
const validEncodings = Object.keys(CHR_ENC_CODE_PAGES);
if (!validEncodings.includes(encoding)) throw new Error(`unknown encoding; pick one of: ${validEncodings.join(", ")}`);

Type guard

const isKnownEncoding = (name) => Object.prototype.hasOwnProperty.call(CHR_ENC_CODE_PAGES, name);

Prevention

When it happens

Trigger: args[0] is a string not present as a key in CHR_ENC_CODE_PAGES - a typo ('UTF-8 ' trailing space), a case mismatch, a name removed in this build, or an arbitrary string injected via recipe JSON / Node API bypassing the dropdown.

Common situations: Sharing a recipe across CyberChef versions where the encoding list changed; importing recipe JSON with a hand-typed encoding name; programmatic Node usage passing an unvalidated encoding string.

Related errors


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