{"record":{"id":"23111d37d2a6f11b","repo":"gchq/CyberChef","slug":"error-base64-alphabet-should-be-64-characters-lon","errorCode":null,"errorMessage":"Error: Base64 alphabet should be 64 characters long, or 65 with a padding character. Found ${alphabet.length}: ${alphabet}","messagePattern":"Error: Base64 alphabet should be 64 characters long, or 65 with a padding character\\. Found (.+?): (.+?)","errorType":"validation","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/lib/Base64.mjs","lineNumber":95,"sourceCode":" *\n * @example\n * // returns \"Hello\"\n * fromBase64(\"SGVsbG8=\");\n *\n * // returns [72, 101, 108, 108, 111]\n * fromBase64(\"SGVsbG8=\", null, \"byteArray\");\n */\nexport function fromBase64(data, alphabet=\"A-Za-z0-9+/=\", returnType=\"string\", removeNonAlphChars=true, strictMode=false) {\n    if (!data) {\n        return returnType === \"string\" ? \"\" : [];\n    }\n\n    alphabet = alphabet || \"A-Za-z0-9+/=\";\n    alphabet = Utils.expandAlphRange(alphabet).join(\"\");\n\n    // Confirm alphabet is a valid length\n    if (alphabet.length !== 64 && alphabet.length !== 65) { // Allow for padding\n        throw new OperationError(`Error: Base64 alphabet should be 64 characters long, or 65 with a padding character. Found ${alphabet.length}: ${alphabet}`);\n    }\n\n    // Remove non-alphabet characters\n    if (removeNonAlphChars) {\n        const re = new RegExp(\"[^\" + alphabet.replace(/[[\\]\\\\\\-^$]/g, \"\\\\$&\") + \"]\", \"g\");\n        data = data.replace(re, \"\");\n    }\n\n    if (strictMode) {\n        // Check for incorrect lengths (even without padding)\n        if (data.length % 4 === 1) {\n            throw new OperationError(`Error: Invalid Base64 input length (${data.length}). Cannot be 4n+1, even without padding chars.`);\n        }\n\n        if (alphabet.length === 65) { // Padding character included\n            const pad = alphabet.charAt(64);\n            const padPos = data.indexOf(pad);\n            if (padPos >= 0) {","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/lib/Base64.mjs#L77-L113","documentation":"Thrown by fromBase64() (Base64.mjs line 95) after expanding the alphabet, when its length is not 64 (or 65 with padding). Distinct from toBase64: fromBase64 first does alphabet = alphabet || 'A-Za-z0-9+/=' (line 90), so an empty/null/falsy alphabet is replaced by the default and does NOT throw - only a non-empty, wrong-length alphabet triggers this.","triggerScenarios":"Calling fromBase64(data, 'A-Z0-9') (36 chars); passing a URL-safe or custom alphabet that has the wrong count (e.g. 'A-Za-z0-9-_' without padding = 64 is fine, but a typo dropping a char gives 63); using a Base32/58 alphabet for Base64 decoding.","commonSituations":"User supplies a malformed custom alphabet in the 'From Base64' operation; URL-safe alphabet missing the padding char or one symbol; recipe carrying a custom alphabet that was edited/truncated.","solutions":["Pass null/undefined/'' to use the standard default alphabet.","Supply exactly 64 chars (no padding) or 65 chars (with padding as the 65th).","For URL-safe Base64 use 'A-Za-z0-9-_' (64, no padding) or add '=' for 65.","Use the matching operation for other bases."],"exampleFix":"// before\nfromBase64(data, 'A-Z0-9'); // 36 chars\n\n// after\nfromBase64(data); // default\nfromBase64(data, 'A-Za-z0-9-_'); // URL-safe, 64 chars","handlingStrategy":"validation","validationCode":"import Utils from './core/Utils.mjs';\nfunction validFromB64Alphabet(a) {\n  if (!a) return true; // empty/null uses default\n  const len = Utils.expandAlphRange(a).join('').length;\n  return len === 64 || len === 65;\n}","typeGuard":"function isFromBase64Alphabet(a): boolean { if (!a) return true; const n = Utils.expandAlphRange(a).join('').length; return n === 64 || n === 65; }","tryCatchPattern":"try { fromBase64(data, alphabet); } catch (e) {\n  if (e instanceof OperationError && /Base64 alphabet should be 64 characters/.test(e.message)) { alphabet = null; }\n}","preventionTips":["Pass null/undefined to use the standard default alphabet.","For URL-safe Base64 supply 'A-Za-z0-9-_' (64) or add '=' for padding (65).","Validate custom-alphabet length before decoding."],"tags":["base64","decoding","alphabet","validation","operationerror"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}