Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
    Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

javax.crypto.BadPaddingException when trying to run AES enryption and decryption multiple times

Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I am trying to encrypt and decrypt a message(String) using a given key string. For that, two seperate methods (one for encrypt and the other for decrypt) were called and when these methods are called from my applicaion. I am getting the below exception while decrypting. The encryption and decryption operations are working properly for the first time.Again if I call the methods with different message and key value it is throwing the below exception.

javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.CipherCore.doFinal(
at com.sun.crypto.provider.CipherCore.doFinal(
at com.sun.crypto.provider.AESCipher.engineDoFinal(
at javax.crypto.Cipher.doFinal(

I don't think that, there is a problem with data. Because using the same data (key and message) when I try to call the encrypt/decrypt methods it is working properly. Also if I call the methods from my application for the first time, it will work fine. Please help me to solve the issue......

Posts: 22584
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please UseOneThreadPerQuestion. Let's continue here.
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic