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
8
votes
0 answers

android 6.0 javax.crypto.BadPaddingException: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT

this code works well before android 6.0, but get an error on 6.0 if encrypted file size less than about 1k bytes. public static byte[] decode(byte[] decrypteSrcBuffer) throws Exception { Key deskey = null; DESedeKeySpec spec = new…
7
votes
2 answers

Getting "Cause: failed to decrypt safe contents entry: javax.crypto.BadPaddingException: Given final block not properly padded"

I am trying to test out LibGDX development, and I am getting an error trying to get even the most basic app to run on my android phone, because there is some problem with my keystore or in how it's being used, and I'm not sure exactly what that…
7
votes
2 answers

'BadPaddingException: pad block corrupted' while decrypting using AES/ECB

In Android/java app, byte[] data = ":ʺ$jhk¨ë‹òºÃ"; // fetched from php server.. Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, mKeyspec); return new String(cipher.doFinal(data)); The above code always throws…
Ron
  • 24,175
  • 8
  • 56
  • 97
6
votes
1 answer

Android cipher.doFinal got BadPaddingException when try to decrypt after reopen app

Question may be long but I will try to describe it in detail. Here is a demo has issue like mine. I have an android app and I want to add a function, which allow user to encrypt and save their passwords in SharedPreferences, and read and decrpty…
lovefish
  • 657
  • 6
  • 18
5
votes
2 answers

BadPaddingException when decrypting AES with the same key

This is the tester: public class CryptographySimpleTests extends ActivityTestCase { public void testsCryptographyClass_encryptAndDecrypt() { final String orgVal = "hi world! :D"; final String key = "key"; …
jmb95
  • 97
  • 1
  • 2
  • 9
4
votes
1 answer

RSA BadPaddingException in Java - encrypt in Android decrypt in JRE

i found some other questions but i did not find a explaination what causes this. I wrote a function in Java to encrypt data, store it into a file and decrypt it loading the file again. As it is a asymetric encryption, i have a public and a private…
René W.
  • 150
  • 1
  • 9
4
votes
1 answer

BadPaddingException with Jasypt library

I am not sure whether this is a bug or I am doing some mistake. In either case, I need help from somebody. We have incorporated Jasypt along with Spring 2.05 in our web application in which we are encrypting some passwords in the properties file.…
Kashif Nazar
  • 20,775
  • 5
  • 29
  • 46
2
votes
1 answer

SOAP encryption - javax.crypto.BadPaddingException: unknown block type

I am trying to sign and encrypt the SOAP message, but I am getting this error - The signature or decryption was invalid; nested exception is: javax.crypto.BadPaddingException: unknown block type Since there is no explicitly mentioned padding in the…
2
votes
0 answers

How to fix javax.crypto.BadPaddingException: Decryption error in Java

I am trying to make a Server/Client Echo program with encryption. By looking for a bit I found this post which had an answer with simple step by step guide on what needs to be done to encrypt communication between the server and client. I am now on…
Oscar K.
  • 71
  • 3
  • 9
2
votes
1 answer

BadPaddingException: mac check in GCM failed

I am trying to encrypt/decrypt using AES-GCM and JDK 1.8 CipherOutputStream, But getting BadPaddingException during decryption. I am using same IV and secret key during encryption and decryption, but not sure what is going wrong. Please see the code…
2
votes
1 answer

RSA OAEP, Golang Encrypt, Java Decrypt -BadPaddingException: Decryption error

I'm trying to decrypt a string which is encrypted in Golang using RSA-OAEP. but getting BadPaddingException: Decryption error. Having hard time to figure out what am I missing.. Here is the Golang encrypt method func encryptString() { rootPEM :=…
2
votes
1 answer

BadPadding: Encryption Error when I decrypt my encrypted string at java

I use SwiftyRSA to encrypt string with public key with PKCS1 padding. Unfortunately, I've found BadPadding: Encryption Error when I decrypt my encrypted string at Java. So far, I found Java use Mode to encrypt/decrypt string but there is no Mode in…
PPShein
  • 13,309
  • 42
  • 142
  • 227
2
votes
1 answer

ASUS zenfone5 t00j AES 256 decryption issue

I am working on mobile application and client side we are using JavaScript (kony) at server side its java. This is working fine for all other device except intel chipset devices (ASUS Zenfone). PFB the JS code for encryption function…
Kris
  • 891
  • 2
  • 18
  • 41
2
votes
1 answer

Bad Padding Exception while using AES encryption

I am working on AES encryption for my project. I decided to do File Encryption When i execute encryption and decryption in same function ,it runs without any flaw.But when i do it separately, decryption part experience some problem. I used the same…
Barath B
  • 23
  • 1
  • 5
2
votes
2 answers

BadPaddingException and some files stuck at 99%

I've tried to gather all possible information about Encryption/Decryption from here. Tinkered with it, some success and failures. But now I've applied the code and its hit and a miss too. Some files (exe or msi's) are working but they still gives…
1
2 3 4 5