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
1
vote
0 answers

"javax.crypto.BadPaddingException: pad block corrupted" exception during decryption

I am working on an existing app, coming to it having not written it. I get this exception (javax.crypto.BadPaddingException: pad block corrupted) during decryption of a username and password in this method: private static byte[] decrypt(byte[] raw,…
BeniaminoBaggins
  • 11,202
  • 41
  • 152
  • 287
1
vote
2 answers

AES File decrypting “given final block not properly padded”

I want to encrypt and then decrypt file use AES. I have read many topics about error "Given final block not properly padded". But i don't find solution for me. Sorry about specify the language of my code, i don't know write language java Here is my…
hungdh0x5e
  • 55
  • 10
1
vote
0 answers

Java BadPaddingException "Given final block not properly padded" after several hours

Like many others over time I'm having issues with encryption in a system I'm maintaining. Process A generates some encrypted text which later Process B must decode. They share the same code for this purpose which is as below: public class…
1
vote
1 answer

Decryption Error bad padding

I am trying to send encrypted messages between two agents .I have a string that contains information that I convert to bytes encrypt it and then to string again to send the message. Messages are received however, at the receiving agent I get the…
Dina
  • 83
  • 1
  • 11
1
vote
1 answer

Why do I get a BadPaddingException from RSA decryption?

I used RSA algorithm to encrypt and decrypt. When I encrypt a string, it's working properly. When I decrypt, I get an error. Below, I post my code. public final String modulusString =".............."; public final String publicExponentString =…
Poongunran
  • 11
  • 1
  • 2
1
vote
2 answers

Java AES String decrypting "given final block not properly padded"

For all haters, I READ MANY topics like this one, and non of them was helpful. eg. here javax.crypto.BadPaddingException: Given final block not properly padded error while decryption or here Given final block not properly padded I want to encrypt…
pawpaw
  • 145
  • 1
  • 2
  • 11
1
vote
1 answer

javax.crypto.BadPaddingException: Message is larger than modulus

I am working on a project where I need to show some encryption decryption over the RMI network. I am using RSA system for this. On decryption, my code is giving me the following error : javax.crypto.BadPaddingException: Message is larger than…
user3606758
  • 11
  • 1
  • 1
  • 2
1
vote
1 answer

Java AES decryption with wrong key

I have written a simple Java AES encryption and decryption as below (for learning purpose): //Encryption Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); SecretKeySpec secretKey = new SecretKeySpec(key,…
Sam YC
  • 10,725
  • 19
  • 102
  • 158
1
vote
1 answer

BadPaddingException after porting Java app to OS X

I'm in the process of porting our Java application to OS X (10.8). One of our unit tests fails when doing encryption (it works on Windows). Both are running Java 7 Update 21 but the Windows version is using the 32 bit JDK and the Mac version is…
Yrlec
  • 3,401
  • 6
  • 39
  • 75
1
vote
3 answers

Android AES decryption and data from iOS:javax.crypto.BadPaddingException: pad block corrupted

I tried to decrypt a backup on Android which is sent from iOS, and the exception javax.crypto.BadPaddingException: pad block corrupted is showed at method doFinal. public String decrypt(byte[] cipherText, SecretKey key, byte [] initialVector)…
lht
  • 13
  • 5
1
vote
1 answer

javax.crypto.BadPaddingException: data hash wrong

I try to write a crypto package in java that wraps the javax.crypto package and makes it more comfortable to use. So I implemented a RSACore class that only wraps javax.crypto. Then I implemented two classes RSAEncoder and RSADecoder to hide tha…
Coding
  • 134
  • 3
  • 10
0
votes
1 answer

Decryption error while Decrypt the symmetric key

I'm trying to use RSA algorithm and PKCS1Padding for encryption the given password but when cipher.doFinal() is called the BadPaddingException is thrown. Here is the code : String rsaKeyAlgorithm = "RSA"; String rsaEncryptAlgorithm =…
Mohammadreza Khatami
  • 1,444
  • 2
  • 13
  • 27
0
votes
0 answers

Old code on new server: BadPaddingException: Given final block not properly padded

UPDATE: I made a standalone version (.class file with a main method) which works just fine on the new server. I can only think it has something to do with reading the passwords from the config file (I verified it's encoding, UTF-8, matches what the…
0
votes
0 answers

RSA decryption in Java is throwing BadPaddingException when encrypted in NodeJS (RSA/ECB/PKCS1PADDING)

RSA encryption with NodeJS and Decryption in Java throwing BadPaddingException. I issue is I cannot touch Java code as it is legacy code written by another team. The Java code is another server code which is throwing BadPaddingException: My NodeJS…
vivek
  • 1
  • 1
0
votes
0 answers

How to resolve BadPadding exception while RSA Signing?

I am trying to create a signed JWT , but when I try to sign he JWT I am getting the following exception, Caused by: javax.crypto.BadPaddingException: RSA private key operation failed at sun.security.rsa.RSACore.crtCrypt(RSACore.java:201) …
Q2Dev
  • 85
  • 1
  • 9