Questions tagged [badpaddingexception]

javax.crypto.BadPaddingException is raised if, during a decryption operation, the cryptographic padding is found to be corrupt. This usually indicates that the decryption key or decryption method is incorrect, but may also be caused by message corruption or deliberate tampering.

Some cryptographic operations require padding in order to increase security. Padding is non-random and often specifically structured to fulfill a particular cryptographic goal.

javax.crypto.BadPaddingException is raised if, during a decryption operation, the padding is found to be corrupt. This usually indicates that the decryption key or decryption method is incorrect, but may also be caused by message corruption or deliberate tampering.

Diagnosing and Correcting the Exception

There are a lot of things that might cause a Bad Padding exception. Obvious things to check are that for both encryption and decryption you are using:

  1. the same key, that is byte-for-byte the same.

  2. the same encryption mode (CBC, CTR or GCM commonly).

  3. the same IV/Nonce, again byte-for-byte the same.

  4. the same padding (PKCS5 or PKCS7 are common).

The byte-for-byte checking is important. A text key in UTF-8 does not give the same bytes as a text key in UTF-16, even if the text looks identical.

Do not rely on system defaults, especially when encrypting on one system and decrypting on another. If the system defaults are different, then your decryption will fail. Always explicitly set key, mode, IV and padding. There will be documented ways to do so in any reasonable crypto library.

If that doesn't solve it then you will need to do a bit more digging. Set the decryption method temporarily to NoPadding or whatever equivalent your library uses. That will let the decryption method ignore padding errors, and give you some output to examine. Have a look at the output and compare it to the original input; you may have to look at hex dumps here to be sure what is happening.

Among the possibilities are:

  1. the output is complete garbage: your key is wrong, or the IV/Nonce is wrong for a stream cypher or GCM mode or CTR mode.

  2. the first block is garbage with the rest matching the plaintext: you have the wrong IV in CBC mode.

  3. the output mostly matches, with some extra stuff added at the end: the extra stuff is the padding. Set your decryption method to expect that type of padding.

If none of these happen, then ask a question, clearly describing the symptoms.

When you have found a solution, you must set your decryption method back to expect the correct padding. Leaving it set to NoPadding is not secure since any old garbage can be added to the decrypted plaintext.

75 questions
0
votes
0 answers

javax.crypto.BadPaddingException: Decryption Error - Not able to decrypt multiple blocks from file

Hoping somebody can point me in the right direction here. I'm not particularly familiar with encryption (in Java or otherwise), but I said that I'd help somebody out with a task they have that specifies using RSA encrypt/decrypt. To familiarise…
marcuthh
  • 592
  • 3
  • 16
  • 42
0
votes
0 answers

BadPaddingException when decrypting JSON data with encrypted values

I have a third party API where I have to send data in JSON in encrypted form. The data in JSON looks like this: { "encryptedKey": "rltP+oNBMx26wSpmvKZ91iL=", "encryptedData": "u5o+ON08CNGLwvt8OUmHXFPAzfk3uPILANA=" } Note: The data in json…
gpsingh
  • 11
  • 3
0
votes
0 answers

Given final block not properly padded in AES Decryption

I want to do encryption in Javascript and decrypt it in Java but it kept getting me bad padding exception. I have no idea what to do. Here's the code key: string; iv:string keySize:256 iterations:1000; constructor() { } encrypt(keys,…
archaenjel
  • 25
  • 2
  • 7
0
votes
1 answer

BadPaddingException in RSA Encrryption

I got a BadPaddingException in my RSA Encryption Program. I dont know why it occurs though. import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import…
0
votes
1 answer

Spring Boot cipher BadPaddingException: Decryption error

I am following an article here, where it described how to use a cipher to encrypt spring boot application properties. So based on the instruction, I downloaded and placed the JCE Java Cryptography Extension (JCE) files. Then I created a keystore,…
Adnan Mian
  • 415
  • 1
  • 5
  • 17
0
votes
0 answers

BadPaddingException when trying to decrypt Fingerprint

I'm trying to encrypt multiple byte arrays from a file via socket. Encrypt procedure works fine, but decrypt procedure throws BadPaddingException: invalid argument. After I read, I understood that the key for decrypt procedure is different from the…
0
votes
1 answer

AES encryption-decryption issue: padding is invalid and cannot be removed

I am facing the AES padding issue. i am using the codes suggested in (generate a 128-bit string in C#) by Alcides Soares FIlho. Please note that my encryption side code is ... private string Encrypt(string clearText) { …
0
votes
1 answer

Java: Not Catching deliberate BadPaddingException

My app prompts the user for the password that was used to encrypt a control file. If the wrong password is entered, the app responds by creating a new control file. Therefore I need to catch a BadPaddingException so I can trigger the appropriate…
D. Harrop
  • 33
  • 4
0
votes
0 answers

RSA decypt failed, javax.crypto.BadPaddingException: Decryption error

I used bouncycastle to decrypt RSA encrypted string, and the data is encoded in Base64, but when I javac Decrypt.java and java Decrypt, it shows an error said "avax.crypto.BadPaddingException: Decryption error" , this is the code: import…
bug
  • 21
  • 6
0
votes
1 answer

Java AES-256-CBC not working as expected

Created a new class to test something with AES in CBC and CTR mode. So with this code, CTR is working fine, but CBC returns empty arrays. Not sure why this happens, hope somebody can explain that. import org.junit.Before; import…
TheOkeland
  • 37
  • 8
0
votes
1 answer

RSA Bad Padding Exception

I am trying to RSA encrypt data on Android and send it to server(spring). getting BadPaddingException : Approach: server send public key in a string, which i convert to PublicKey Object and send data from App after encryption as a string. server…
Makwana
  • 70
  • 10
0
votes
2 answers

Invalid Key Exception

I am retrieving a text password as input from a file and applying AES encryption over that and later on, decrypting that. When I did it for the first time, every 4 out of 5 times it was running correctly (encryption decryption successful) but 1…
0
votes
1 answer

Can't overcome the javax.crypto.BadPaddingException

I'm writing a steganography system which encrypt a text within an image - i decided that i want to encrypt the text as well before i embed it in the picture. The Steganography algorithm works with String input/output. Due to my whim, i've been…
0
votes
1 answer

Padding exception in java 7:Caused by: javax.crypto.BadPaddingException: Given final block not properly padded

Hi I am using tripedes key to read from input stream and write to output stream. Getting this execption in java7/8:Caused by: javax.crypto.BadPaddingException: Given final block not properly padded byte[] passwd = Base64Util.decode(pwd); bais = new…
mahan07
  • 887
  • 4
  • 14
  • 32
0
votes
0 answers

Java sign pdf keystore.load throws javax.crypto.BadPaddingException

I used the following code that I found on this page to sign PDFs with a *.p12 file. public static final boolean signPdf() throws IOException, DocumentException, Exception { // Vous devez preciser ici le chemin d'acces a votre clef…
mambo
  • 1