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;
}
}