{"record":{"id":"d3559560e724a2f3","repo":"gchq/CyberChef","slug":"unable-to-detect-input-key-format","errorCode":null,"errorMessage":"Unable to detect input key format.","messagePattern":"Unable to detect input key format\\.","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/ParseSSHHostKey.mjs","lineNumber":127,"sourceCode":"    }\n\n\n    /**\n     * Detects if the key is base64 or hex encoded\n     *\n     * @param {string} inputKey\n     * @returns {string}\n     */\n    detectKeyFormat(inputKey) {\n        const hexPattern = new RegExp(/^(?:[\\dA-Fa-f]{2}[ ,;:]?)+$/);\n        const b64Pattern = new RegExp(/^\\s*(?:[A-Za-z\\d+/]{4})+(?:[A-Za-z\\d+/]{2}==|[A-Za-z\\d+/]{3}=)?\\s*$/);\n\n        if (hexPattern.test(inputKey)) {\n            return \"Hex\";\n        } else if (b64Pattern.test(inputKey)) {\n            return \"Base64\";\n        } else {\n            throw new OperationError(\"Unable to detect input key format.\");\n        }\n    }\n\n\n    /**\n     * Parses fields from the key\n     *\n     * @param {byteArray} key\n     */\n    parseKey(key) {\n        const fields = [];\n        while (key.length > 0) {\n            const lengthField = key.slice(0, 4);\n            let decodedLength = 0;\n            for (let i = 0; i < lengthField.length; i++) {\n                decodedLength += lengthField[i];\n                decodedLength = decodedLength << 8;\n            }","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/ParseSSHHostKey.mjs#L109-L145","documentation":"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.","triggerScenarios":"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).","commonSituations":"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).","solutions":["Manually select 'Hex' or 'Base64' as the input format instead of 'Auto'","Clean the key data: remove extra whitespace, line breaks, and non-ASCII characters","If the key has an OpenSSH new-format prefix not recognized by the parser, strip it manually and provide just the base64 key body"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Pre-check: test if the key matches hex or base64\nconst HEX_RE = /^(?:[\\dA-Fa-f]{2}[ ,;:]?)+$/;\nconst B64_RE = /^\\s*(?:[A-Za-z\\d+/]{4})+(?:[A-Za-z\\d+/]{2}==|[A-Za-z\\d+/]{3}=)?\\s*$/;\nconst keyBody = input.replace(/^(?:ssh|ecdsa-sha2)\\S+\\s+/, '').trim();\nif (!HEX_RE.test(keyBody) && !B64_RE.test(keyBody)) {\n  console.warn(\"Key format could not be auto-detected — specify Hex or Base64 manually\");\n}","typeGuard":"function isHexOrBase64(s) {\n  const HEX_RE = /^(?:[\\dA-Fa-f]{2}[ ,;:]?)+$/;\n  const B64_RE = /^\\s*(?:[A-Za-z\\d+/]{4})+(?:[A-Za-z\\d+/]{2}==|[A-Za-z\\d+/]{3}=)?\\s*$/;\n  return HEX_RE.test(s) || B64_RE.test(s);\n}","tryCatchPattern":"try {\n  const result = chef.parseSSHHostKey(input, [\"Auto\"]);\n} catch (e) {\n  if (/detect.*format/i.test(e.message)) {\n    // Retry with explicit format\n    const result = chef.parseSSHHostKey(input, [\"Base64\"]);\n  }\n}","preventionTips":["When auto-detection fails, manually select 'Hex' or 'Base64'","Clean the key data of embedded whitespace, line breaks, and formatting artifacts","Strip key type prefixes that the parser does not recognize before passing to the operation"],"tags":["ssh","key","encoding","auto-detection","validation"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}