aspose file tools*
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
Author

BadPaddingException using AES

Rupesh Bhima
Greenhorn

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)
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");
keygen1.init(128);
aesKey1 = keygen1.generateKey();
} catch (Exception e) {
}
}

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

String str=decrypt("P27EZQfL5tSvqjMYuQVh3Q==");
System.out.println(str);
}

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

public static String decrypt(String pValue) throws InvalidKeyException,
BadPaddingException, IllegalBlockSizeException {
System.out.println("Main.decrypt()");
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.
System.out.println("Testt.encrypt()");
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");
System.out.println(stringBytes);
// encrypt using the cypher
byte[] raw = cipher.doFinal(stringBytes);
System.out.println(raw);

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

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

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

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

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

Joined: Mar 22, 2005
Posts: 42293
    
  64
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?


Ping & DNS - my free Android networking tools app
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.
 
wood burning stoves
 
subject: BadPaddingException using AES