gchq/CyberChef · error · OperationError

Unable to detect input key format.

Error message

Unable to detect input key format.

What it means

When input format is 'Auto', detectKeyFormat() tests the key against a hex regex and a base64 regex. If neither matches, the key data does not resemble either encoding and the format cannot be determined automatically.

Source

Thrown at src/core/operations/ParseSSHHostKey.mjs:127

    }


    /**
     * Detects if the key is base64 or hex encoded
     *
     * @param {string} inputKey
     * @returns {string}
     */
    detectKeyFormat(inputKey) {
        const hexPattern = new RegExp(/^(?:[\dA-Fa-f]{2}[ ,;:]?)+$/);
        const b64Pattern = new RegExp(/^\s*(?:[A-Za-z\d+/]{4})+(?:[A-Za-z\d+/]{2}==|[A-Za-z\d+/]{3}=)?\s*$/);

        if (hexPattern.test(inputKey)) {
            return "Hex";
        } else if (b64Pattern.test(inputKey)) {
            return "Base64";
        } else {
            throw new OperationError("Unable to detect input key format.");
        }
    }


    /**
     * Parses fields from the key
     *
     * @param {byteArray} key
     */
    parseKey(key) {
        const fields = [];
        while (key.length > 0) {
            const lengthField = key.slice(0, 4);
            let decodedLength = 0;
            for (let i = 0; i < lengthField.length; i++) {
                decodedLength += lengthField[i];
                decodedLength = decodedLength << 8;
            }

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Manually select 'Hex' or 'Base64' as the input format instead of 'Auto'
  2. Clean the key data: remove extra whitespace, line breaks, and non-ASCII characters
  3. If the key has an OpenSSH new-format prefix not recognized by the parser, strip it manually and provide just the base64 key body
Defensive patterns

Strategy: validation

Validate before calling

// Pre-check: test if the key matches hex or base64
const HEX_RE = /^(?:[\dA-Fa-f]{2}[ ,;:]?)+$/;
const B64_RE = /^\s*(?:[A-Za-z\d+/]{4})+(?:[A-Za-z\d+/]{2}==|[A-Za-z\d+/]{3}=)?\s*$/;
const keyBody = input.replace(/^(?:ssh|ecdsa-sha2)\S+\s+/, '').trim();
if (!HEX_RE.test(keyBody) && !B64_RE.test(keyBody)) {
  console.warn("Key format could not be auto-detected — specify Hex or Base64 manually");
}

Type guard

function isHexOrBase64(s) {
  const HEX_RE = /^(?:[\dA-Fa-f]{2}[ ,;:]?)+$/;
  const B64_RE = /^\s*(?:[A-Za-z\d+/]{4})+(?:[A-Za-z\d+/]{2}==|[A-Za-z\d+/]{3}=)?\s*$/;
  return HEX_RE.test(s) || B64_RE.test(s);
}

Try / catch

try {
  const result = chef.parseSSHHostKey(input, ["Auto"]);
} catch (e) {
  if (/detect.*format/i.test(e.message)) {
    // Retry with explicit format
    const result = chef.parseSSHHostKey(input, ["Base64"]);
  }
}

Prevention

When it happens

Trigger: The key string contains characters invalid in both hex and base64 (e.g., special characters, whitespace in the middle, or non-ASCII bytes). The key was corrupted or partially stripped. The key has a prefix like 'ssh-rsa ' that was not stripped (note: the code does strip 'ssh-/ecdsa-' prefixes, but other prefixes are not handled).

Common situations: User pastes a key with a type prefix that is not matched by the keyPattern regex (e.g., 'sk-ssh-ed25519@openssh.com'). Key has embedded whitespace or line breaks that break both regexes. Key was copy-pasted with formatting artifacts (smart quotes, non-breaking spaces).

Related errors


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