Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • 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 ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

BadPaddingException using AES

 
Greenhorn
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");
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;
}
}
 
Rancher
Posts: 43026
76
  • 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.
 
today's feeble attempt to support the empire
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic