File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Security and the fly likes BadPaddingException using AES Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Security
Bookmark "BadPaddingException using AES" Watch "BadPaddingException using AES" New topic

BadPaddingException using AES

Rupesh Bhima

Joined: Jul 27, 2009
Posts: 1
Hi All,

I am try to Decrypt the Data by giving this as input "P27EZQfL5tSvqjMYuQVh3Q== this is Exception i am getting

Exception in thread "main" javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.SunJCE_h.b(DashoA12275)
at com.sun.crypto.provider.SunJCE_h.b(DashoA12275)
at com.sun.crypto.provider.AESCipher.engineDoFinal(DashoA12275)
at javax.crypto.Cipher.doFinal(DashoA12275)

Please if any one know the solutions please help me find me Code



import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Testt {
static Cipher cipher = null;
static SecretKey aesKey1 = null;
static KeyGenerator keygen1 = null;
static {
try {
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
keygen1 = KeyGenerator.getInstance("AES");
aesKey1 = keygen1.generateKey();
} catch (Exception e) {

public static void main(String[] args) throws Exception {
//String str=encrypt("sadadsa");

String str=decrypt("P27EZQfL5tSvqjMYuQVh3Q==");

/*public static byte[] encrypt(String pValue) throws InvalidKeyException,
BadPaddingException, IllegalBlockSizeException {
cipher.init(Cipher.ENCRYPT_MODE, aesKey1);
byte[] inputBytes = pValue.getBytes();
return cipher.doFinal(inputBytes);

public static String decrypt(String pValue) throws InvalidKeyException,
BadPaddingException, IllegalBlockSizeException {
System.out.println(" pValue ---> " + pValue);
byte[] encryptionBytes = toBinArray(pValue);
System.out.println("---> " + encryptionBytes);
cipher.init(Cipher.DECRYPT_MODE, aesKey1);
byte[] recoveredBytes = cipher.doFinal(encryptionBytes);
String recovered = new String(recoveredBytes);
return recovered;

public static String encrypt(String message) throws IllegalBlockSizeException,
BadPaddingException, NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException,
UnsupportedEncodingException {
// Get a cipher object.
cipher.init(Cipher.ENCRYPT_MODE, aesKey1);

// Gets the raw bytes to encrypt, UTF8 is needed for
// having a standard character set
byte[] stringBytes = message.getBytes("UTF8");
// encrypt using the cypher
byte[] raw = cipher.doFinal(stringBytes);

// converts to base64 for easier display.
BASE64Encoder encoder = new BASE64Encoder();
String base64 = encoder.encode(raw);
return base64;
public static String decrypt(String encrypted) throws InvalidKeyException,
NoSuchAlgorithmException, NoSuchPaddingException,
IllegalBlockSizeException, BadPaddingException, IOException {

// Get a cipher object.
cipher.init(Cipher.DECRYPT_MODE, aesKey1);

//decode the BASE64 coded message
BASE64Decoder decoder = new BASE64Decoder();

byte[] raw = decoder.decodeBuffer(encrypted);

//decode the message
byte[] stringBytes = cipher.doFinal(raw);
//converts the decoded message to a String
String clear = new String(stringBytes, "UTF8");
return clear;
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Please edit your post to UseCodeTags. It's unnecessarily hard to read the code as it is, making it less likely that people will bother to do so.

} catch (Exception e) {

Don't do this. You should at least print a message that there was an exception; otherwise, how will you know about problems?
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

The last thing your encrypt() method does is to Base64 encode the ciphertext so the first thing you decrypt() method should do is to Base64 decode the Base64 encoded ciphertext.

Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
I agree. Here's the link:
subject: BadPaddingException using AES
It's not a secret anymore!