{"record":{"id":"cc2b3fc4ebf2212e","repo":"gchq/CyberChef","slug":"invalid-pkcs-5-padding-cc2b3f","errorCode":null,"errorMessage":"Invalid PKCS#5 padding.","messagePattern":"Invalid PKCS#5 padding\\.","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/lib/RC6.mjs","lineNumber":415,"sourceCode":" * @returns {number[]} - Unpadded message\n */\nfunction removePadding(message, padding, blockSize) {\n    if (message.length === 0) return message;\n\n    switch (padding) {\n        case \"NO\":\n        case \"ZERO\":\n        case \"RANDOM\":\n            // These padding types cannot be reliably removed\n            return message;\n\n        case \"PKCS5\": {\n            const padByte = message[message.length - 1];\n            if (padByte > 0 && padByte <= blockSize) {\n                // Verify padding\n                for (let i = 0; i < padByte; i++) {\n                    if (message[message.length - 1 - i] !== padByte) {\n                        throw new OperationError(\"Invalid PKCS#5 padding.\");\n                    }\n                }\n                return message.slice(0, message.length - padByte);\n            }\n            throw new OperationError(\"Invalid PKCS#5 padding.\");\n        }\n\n        case \"BIT\": {\n            // Find 0x80 byte working backwards, skipping zeros\n            for (let i = message.length - 1; i >= 0; i--) {\n                if (message[i] === 0x80) {\n                    return message.slice(0, i);\n                } else if (message[i] !== 0) {\n                    throw new OperationError(\"Invalid BIT padding.\");\n                }\n            }\n            throw new OperationError(\"Invalid BIT padding.\");\n        }","sourceCodeStart":397,"sourceCodeEnd":433,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/lib/RC6.mjs#L397-L433","documentation":"Thrown by RC6.removePadding for PKCS5 when the trailing pad byte count is in range (1..blockSize) but at least one of the final N bytes does not equal N. PKCS#5 padding requires the last N bytes all to equal N; a mismatch means the ciphertext was wrong, the padding type was wrong, or the data is not PKCS5-padded.","triggerScenarios":"Calling decryptRC6(...) with padding 'PKCS5' on ciphertext whose decrypted final block does not end in valid PKCS#5 bytes — wrong key, wrong IV, double-decryption, or the data was padded differently.","commonSituations":"Wrong key/IV producing random-looking plaintext; data was padded with ZERO/BIT but decrypted as PKCS5; ciphertext truncated/extended by a byte; CBC chain broken.","solutions":["Verify the key and IV match those used for encryption.","Confirm the data was actually PKCS5-padded at encryption time.","If unsure of padding, decrypt with 'NO' and inspect the tail bytes."],"exampleFix":"// before: decrypt with PKCS5 though sender used BIT\n// after: match the sender's padding scheme\nconst pt = decryptRC6(ct, key, iv, 'CBC', 'BIT');","handlingStrategy":"try-catch","validationCode":"function isValidPkcs5Tail(message, blockSize) {\n  const n = message[message.length - 1];\n  if (n < 1 || n > blockSize) return false;\n  for (let i = 0; i < n; i++) if (message[message.length - 1 - i] !== n) return false;\n  return true;\n}","typeGuard":"function hasValidPkcs5Padding(message, blockSize) {\n  const n = message[message.length - 1];\n  return n >= 1 && n <= blockSize && message.slice(-n).every(b => b === n);\n}","tryCatchPattern":"try {\n  return decryptRC6(ct, key, iv, mode, 'PKCS5', rounds, w);\n} catch (e) {\n  if (e instanceof OperationError && /Invalid PKCS#5 padding/.test(e.message)) {\n    // verify key/IV, or decrypt with 'NO' to inspect the tail bytes\n    return decryptRC6(ct, key, iv, mode, 'NO', rounds, w);\n  }\n  throw e;\n}","preventionTips":["Confirm key/IV match those used to encrypt.","Ensure the data was PKCS5-padded at the source.","When in doubt, decrypt with 'NO' and inspect the final block."],"tags":["crypto","rc6","padding","pkcs5","decrypt","operation-error"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}