Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

BadPaddingException: Given final block not properly padded

843811Feb 1 2006 — edited Feb 5 2006
the following code works fine when i supply the correct password, but what if the user puts in the wrong password? an exception is thrown. i would assume that if the wrong password is supplied, the data would be garbled.

btw...excuse the terrible exception handling...i'm just trying to get something up and running
import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;

public class PBEncryptor {
	
	private static final byte[] SALT = {
		(byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c, 
		(byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99
	};
	private static final int ITERATIONS = 8;
	
	/* Encrypts a String by creating a key using the given password
	 */
	public byte[] encryptString(String pass, String text) {
		try {
			//convert password to array of char
			char[] password = pass.toCharArray();
		
			//initialize SecretKeyFactory and specify type of encryption
			SecretKeyFactory skf = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
			
			//create the key
			SecretKey key = skf.generateSecret(new PBEKeySpec(password));
		
			//create the cipher
			Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
			cipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, ITERATIONS));
		
			//encrypt the data
			byte[] rawData = text.getBytes();
			byte[] encryptedData = cipher.doFinal(rawData);
			
			return encryptedData;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	/* Decrypts a String by creating a key using the given password
	 */
	public String decryptString(String pass, byte[] data) {
		try {
			//convert password to array of char
			char[] password = pass.toCharArray();
			
			//initialize SecretKeyFactory and specify type of encryption
			SecretKeyFactory skf = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
				
			//create the key
			SecretKey key = skf.generateSecret(new PBEKeySpec(password));
			
			//create the cipher
			Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
			cipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, ITERATIONS));
			
			//decrypt the data
			byte[] decryptedData = cipher.doFinal(data); //THIS IS LINE 60
			
			//convert the decrypted bytes to a String
			String decryptedString = new String(decryptedData);
			
			return decryptedString;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
using this code as a test works...
PBEncryptor pbe = new PBEncryptor();
		byte[] b = pbe.encryptString("edfts", "idjalskidifloaksjdlskalskdjdi");
		String s = pbe.decryptString("edfts", b);
		System.out.println(s);
using this code as a test, i run into problems...
PBEncryptor pbe = new PBEncryptor();
		byte[] b = pbe.encryptString("edfts10", "idjalskidifloaksjdlskalskdjdi");
		String s = pbe.decryptString("blah", b);
		System.out.println(s);
the stack trace...
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.SunJCE_ae.b(DashoA12275)
at com.sun.crypto.provider.PBEWithMD5AndDESCipher.engineDoFinal(DashoA12
275)
at javax.crypto.Cipher.doFinal(DashoA12275)
at PBEncryptor.decryptString(PBEncryptor.java:60)
at PBEncryptor.main(PBEncryptor.java:76)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 5 2006
Added on Feb 1 2006
9 comments
1,878 views