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

Losing bytes at the end when using chunked decryption in Android

First I created a little encryption/decryption program in Java/Android. Nothing special about this just really basic, so no buffers or anything. Used an Iv and Salt there I write the salt and the iv at the beginning of the file(24 bytes) . This…
Julian
  • 3
  • 2
0
votes
1 answer

java - RSA BadPaddingException when trying to decrypt an int

I know similar questions have been asked before, but I can't find an answer specific to my problem. I am trying to use RSA encryption to take an image with encrypted pixels and decrypt it using the same private key. I keep on running into a problem…
KilRil
  • 29
  • 1
  • 6
0
votes
0 answers

Exception during RSA decrypt: javax.crypto.BadPaddingException: Data must start with zero

I'm trying to decrypt a password in RSA format, but it's returning exception Caused by: javax.crypto.BadPaddingException: Data must start with zero at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:308) at…
0
votes
1 answer

Given final block not properly padded(BadPaddingException)

I am using the ftpclient for java in which i want to encrypt a file and decrypt it back again. The encryption was done successfully using the below code: String s= EnumerationsQms.ReturnStatus.SUCCESS.getreturnstatus(); int read; FTPConfig objFTP =…
Ash Win
  • 115
  • 1
  • 10
0
votes
2 answers

Padding Exception: Given final block not properly padded

I am trying to encrypt and decrypt my file in which all my passwords are stored using AES. The algorithm works fine on encryption part. But while decryption it throws the error Message:Given final block not properly…
user2820823
  • 86
  • 1
  • 1
  • 9
0
votes
0 answers

javax.crypto.BadPaddingException Decryption error when trying to use HttpURLConnection on https URL

I'm trying to use webservice from java application. The webservice site is served through https. I write this code, but javax.crypto.BadPaddingException Decryption error is appearing. try{ URL url = new URL("https://www.google.ne.jp"); …
0
votes
1 answer

Encrypt a file appending IVSBytes in unique file - Execption: Given final block not properly padded

I'm trying to encrypt a file using AES with a SHA-256 Key. When I generate the IVs I preppend it at the beggining of the encrypted file, and append the rest. When I recover the IV's and compare it (the IV's after and then of the proces) they are…
selan
  • 956
  • 3
  • 11
  • 27
0
votes
1 answer

Java AES decryption causing BadPaddingException

I'm trying to create AES encryption/decryption methods, but I cant seem to get my original input without using AES/ECB/NoPadding. Now I am trying to use AES/CBC/PKCS7Padding. I have confirmed that reading and writing the byte to/from the file works…
Zach
  • 447
  • 6
  • 16
0
votes
1 answer

AES Encryption error: javax.crypto.BadPaddingException

I'm getting the following error with this code: javax.crypto.BadPaddingException: Given final block not properly padded. I pointed out where the error is occurring in the program. package aes; import javax.crypto.*; import java.security.*; import…
Spance
  • 555
  • 3
  • 11
  • 29
0
votes
2 answers

RSA Decryption fails due to input string

I have the following problem. I have 2 functions in my code which are intended to encrypt / decrypt simple string. SO: I have to pass a string "someString" to the function: public static String doEncryption(String input) { try { if…
Georgi Ivanov
  • 29
  • 1
  • 4
0
votes
1 answer

BadPaddingException when decrypting with PDFBox

In the past, I have tried to decrypt PDFs using PDFBox using the following code: if (doc.isEncrypted()){ doc.openProtection(new StandardDecryptionMaterial(password)); doc.setAllSecurityToBeRemoved(true); } (Note that I don't use…
Thunderforge
  • 19,637
  • 18
  • 83
  • 130
0
votes
2 answers

BadPaddingException: Data must start with zero - Why?

I need to write a java code which encrypt/decrypt messages using a private/public key given by a customer. Here is what I did: I got from the customer the private key which is in PKCS#1 format (It starts with -----BEGIN RSA PRIVATE KEY-----) I…
Guy Hudara
  • 247
  • 4
  • 13
0
votes
1 answer

Crazy exception at AES Decryption - BadPaddingException

I'm having some trouble here while trying to decode some encrypted text. CheckpswdBasedKey is always returning false, because of the BadPaddingException at c.doFInal I'm using AES, basicaly the encryption: public static String…
Aero
  • 3
  • 1
0
votes
1 answer

Why do I get BadPaddingException when trying to encrypt a string?

I create an Encryption class to encryt/decrypt binary data in my Android project, by this link. package com.my.package; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import…
Dragon warrior
  • 1,644
  • 2
  • 24
  • 37
-1
votes
1 answer

BadPaddingException: Given final block not properly padded due to different java version

I java 8 installed on client side where I am encrypting my data file using the below technique Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, key); outputStream = new CipherOutputStream(new…
Chunkyy Garg
  • 107
  • 1
  • 5
1 2 3 4
5