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
2
votes
1 answer

AES Encryption/Decryption : Given final block not properly padded

I am trying to write methods to encrypt or decrypt String (which mostly will be numeric). It works fine for some texts(e.g.- '1010000011','1010000012', '1010000013') but gives following error for others(e.g.- '1010000014',…
Amarjeet
  • 113
  • 2
  • 9
2
votes
1 answer

BadPaddingException: pad block corrupted in android decrypting block

I know this question has been asked several times but it doesn't seem to work with my code. I'm getting an exception when decrypting: "javax.crypto.BadPaddingException: pad block corrupted" My code is: private static byte[]…
Fernando
  • 751
  • 2
  • 13
  • 27
2
votes
0 answers

javax.crypto.BadPaddingException: Given final block not properly padded

I'm creating an AES key using the KeyGenerator class. Then I exposed a web service to do encryption and decryption using this AES key and return the cipher text/plain text in web service response. Encryption is working perfectly fine but when I call…
user3817206
  • 315
  • 4
  • 14
2
votes
2 answers

AES Decryption ERROR: android pad block corrupted

I've looked around stack to find an answer to my problem and yet nothing did work. What im trying to achive is to encrypte XML file that is downloaded in ASYNCtask and later on decrypt it. What I've already checked: -Generated key is the same when…
JakubW
  • 1,101
  • 1
  • 20
  • 37
2
votes
2 answers

Java AES decryption BadPaddingException

note: Java NOOB. Alright, I know this has been answered a few dozen times on here, but the solutions don't seem to work/apply directly to where I understand them. (Yes, I know I don't completely understand encryption/decryption, AES, etc. but that…
earthling
  • 5,084
  • 9
  • 46
  • 90
1
vote
1 answer

CipherInputStream gets BadPaddingException

Hope someone can see my issue. I'm trying to send encrypted data over the network using CipherOutputStream/CipherInputStream and keep on getting BadPaddingException. I use the same RSA keys for both sides and can manually encrypt/decrypt fine. …
trungdok
  • 53
  • 5
1
vote
0 answers

Is there a way to generate same encrypted value while encrypting same text using aes 256?

I have a requirement of of encrypt and decrypt the text but each time the encryption value of same text should be generated same. Suppose if I am encrypting a text "My Name is John". And while encrypting first time its value was generated…
1
vote
1 answer

Java BadPaddingException when verifying data signed on a JavaCard

I'm signing some data on a JavaCard with a cipher, then outputting it into a text file, reading it into a java program and trying to verify it. I keep getting a BadPaddingException. I'm signing it like so on my JavaCard applet: Cipher cipher =…
LozCodes
  • 109
  • 8
1
vote
1 answer

I am getting BadPaddingException while decrypting an 128 bit AES key which has been encoded by using RSA generated public key(.jks)

package demo123; import java.io.File; import java.io.FileInputStream; import java.nio.file.Files; import java.security.Key; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import…
bhavesh
  • 13
  • 4
1
vote
0 answers

RSA Decryption JAVA BadPadding

I am trying to integrate an API where I have to send the data in RSA encrypted and BASE64 encoded form. In response I get a BASE64 String which is supposed to be decoded and decrypted at my end. I am able to send the data in encrypted and encoded…
manish
  • 53
  • 7
1
vote
0 answers

Android javax.crypto.BadPaddingException pad block corrupted on android 7 and 8

I see some exceptions in production which only happens with some particular users (10 users for last month from 100 000+ active users per month) My encrypt and decrypt methods uses the same algorithms and everything and all other users don't…
Wackaloon
  • 2,285
  • 1
  • 16
  • 33
1
vote
2 answers

Cipher decryption / encryption changing results

I'm reverse engineering some code which is decrypting data, hoping I'll be able to encrypt it back and obtain the same data it started with, for reasons that would make this question too long and off-topic. public void Test() throws Exception { …
Saturnix
  • 10,130
  • 17
  • 64
  • 120
1
vote
2 answers

'javax.crypto.BadPaddingException' while using cipherInputStream

I'm writing a program to encrypt and decrypt data. for encrypting, I created a symmetric key using keyGenerator. I transferred the key to the cipher, and created a string version of the key: String keyString =…
Loly
  • 125
  • 2
  • 4
  • 11
1
vote
1 answer

BadPaddingException: Decryption error when trying to decrypt a byte array?

public static String decrypt(byte[] text, PrivateKey key) { byte[] decryptedText = null; try { final Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, key); decryptedText =…
Archie
  • 13
  • 1
  • 5
1
vote
2 answers

javax.crypto.BadPaddingException: Given final block not properly padded...tried using getbytes("UTF")

I have tried adding getbytes("UTF") or getbytes("UTF-8"), since it was suggested in a similar question. It said we need to try UTF while converting bytes to string and vice a versa. But still it is not working for my code...please help public class…
sasha00
  • 23
  • 1
  • 1
  • 5