{"record":{"id":"ebb47a428d0c9c72","repo":"spring-projects/spring-security","slug":"unable-to-invoke-cipher-due-to-bad-padding","errorCode":null,"errorMessage":"Unable to invoke Cipher due to bad padding","messagePattern":"Unable to invoke Cipher due to bad padding","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"crypto/src/main/java/org/springframework/security/crypto/encrypt/CipherUtils.java","lineNumber":144,"sourceCode":"\t\t}\n\t\tcatch (InvalidAlgorithmParameterException ex) {\n\t\t\tthrow new IllegalStateException(\"Unable to initialize due to invalid decryption parameter spec\", ex);\n\t\t}\n\t}\n\n\t/**\n\t * Invokes the Cipher to perform encryption or decryption (depending on the\n\t * initialized mode).\n\t */\n\tstatic byte[] doFinal(Cipher cipher, byte[] input) {\n\t\ttry {\n\t\t\treturn cipher.doFinal(input);\n\t\t}\n\t\tcatch (IllegalBlockSizeException ex) {\n\t\t\tthrow new IllegalStateException(\"Unable to invoke Cipher due to illegal block size\", ex);\n\t\t}\n\t\tcatch (BadPaddingException ex) {\n\t\t\tthrow new IllegalStateException(\"Unable to invoke Cipher due to bad padding\", ex);\n\t\t}\n\t}\n\n}\n","sourceCodeStart":126,"sourceCodeEnd":149,"githubUrl":"https://github.com/spring-projects/spring-security/blob/96852e8860138a482cb13d1479573f24ff6443c6/crypto/src/main/java/org/springframework/security/crypto/encrypt/CipherUtils.java#L126-L149","documentation":"CipherUtils wraps JCE Cipher operations and rethrows checked exceptions as unchecked IllegalStateException. This error means Cipher.doFinal() threw BadPaddingException: the decrypted data's padding bytes did not match the padding scheme (e.g. PKCS5Padding) specified for the transform. It almost always indicates wrong key, corrupted/truncated ciphertext, or mismatched padding configuration between encryptor and decryptor.","triggerScenarios":"Calling TextEncryptor/CipherUtils decrypt with a ciphertext produced by a different key, a different padding mode, or data that was truncated/re-encoded (e.g. Base64 mangled), so the final block fails the padding check.","commonSituations":"Rotated or mismatched encryption keys across environments; ciphertext round-tripped through a medium that altered bytes (URL encoding, charset conversion); decrypting data encrypted with a different provider or JCE policy (e.g. pre-JDK8 256-bit limits); switching transform from ECB/PKCS5 to CBC/NoPadding.","solutions":["Verify the decryptor uses the exact same password/key and transform as the encryptor","Check the ciphertext string was not altered: compare byte length / Base64 integrity before decrypting","Confirm the JCE provider supports the configured padding and unlimited-strength policy is available","Wrap decryption in try-catch for IllegalStateException and treat as corrupt input rather than retrying"],"exampleFix":"// before\nString plain = encryptor.decrypt(corruptedCipherText);\n// after\ntry {\n    String plain = encryptor.decrypt(cipherText);\n} catch (IllegalStateException ex) {\n    if (ex.getCause() instanceof BadPaddingException) {\n        throw new IOException(\"Ciphertext is corrupted or key mismatch\", ex);\n    }\n    throw ex;\n}","handlingStrategy":"try-catch","validationCode":"// verify ciphertext round-trips before relying on it\ntry {\n    decrypt(encrypt(sample));\n} catch (IllegalStateException e) {\n    throw new IllegalArgumentException(\"Cipher transform/key mismatch\", e);\n}","typeGuard":null,"tryCatchPattern":"try { String plain = encryptor.decrypt(cipherText); } catch (IllegalStateException ex) { /* treat as corrupt input / key mismatch, do not retry */ }","preventionTips":["Always test encrypt->decrypt round trip after changing keys or transforms","Never re-encode ciphertext through charset conversions or URL encoding","Keep encryption keys versioned and identical across encrypting/decrypting services","Log ciphertext length to detect truncation before decryption"],"tags":["crypto","cryptography","jce","decryption"],"backgroundTag":"decryption-padding-error","analyzedSha":"96852e8860138a482cb13d1479573f24ff6443c6","analyzedAt":"2026-09-10T23:25:23.477Z","contentChangedAt":"2026-09-10T23:25:23.477Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}