Hello there,
BadPaddingException can arise from a few different issues. Here are some things you can try:
1. Incorrect padding: If the padding used to encrypt and decrypt some piece of information, then you can get a BadPaddingException. Please make sure that if you are using Cipher, both the encryption and decryption ones are using the same padding, i.e. PKCS5Padding.
2. Sometimes a InvalidKeyException can present itself as a BadPaddingException. This is when you are encrypting and decrypting with different keys and the Cipher objects are initialised with different keys.
3. Incorrect algorithm when instantiating the Cipher: you may use an invalid algorithm.
4. Another reason for this could be that if you are migration your keystore from JKS to PKCS12. JKS uses a keystone password and a key password, which can be different. When you migrate to PKCS12, we need to be a bit more careful. PKCS12 usually uses a keystone password for the while keystore. So, if there are mismatches between the passwords, you can face a BadPaddingException when decrypting.
You can change the key password of a JKS:
keytool -storepasswd -keystore yourKeystore.jks
You'll be prompted to enter the current keystore password. After entering the current password, press Enter.
Next, keytool will ask you to enter the new keystore password. Type the new password and press Enter.
You'll be asked to re-enter the new password for confirmation. Type it again and press Enter.
I hope this helps! Please feel free to follow up with any questions you may have.
Best,
Prarthona