• Post Reply 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 ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
  • Piet Souris
  • Himai Minh

BadPaddingException using AES

Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
at com.tech.Testt.decrypt(Testt.java:98)
at com.tech.Testt.main(Testt.java:36)

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

package com.tech;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

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;
Posts: 43027
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
please buy my thing and then I'll have more money:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
    Bookmark Topic Watch Topic
  • New Topic