javax.crypto.BadPaddingException: Given final block not properly padded

Y

yishushui

Hi!
I have a class as below that wants to store an encrypted password into
database,
and then decrypts the encrypted password retriving from
database,print the
plaint password.


--enCryptPwd and enCryptPwd Methods' Code-----

package project_wh;
import java.text.*;
import java.lang.*;
import java.util.*;
import java.io.*;
import javax.crypto.*;
import sun.misc.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright &copy; 2003</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/

public class Self_Util {
Cipher ecipher;
Cipher dcipher;
SecretKey key;
public Self_Util() {
try {
ecipher = Cipher.getInstance("DES");
dcipher = Cipher.getInstance("DES");
key= KeyGenerator.getInstance("DES").generateKey();
ecipher.init(Cipher.ENCRYPT_MODE, key);
dcipher.init(Cipher.DECRYPT_MODE, key);

} catch (javax.crypto.NoSuchPaddingException e) {
} catch (java.security.NoSuchAlgorithmException e) {
} catch (java.security.InvalidKeyException e) {
}

}

public String enCryptPwd(String str) {
try {
// Encode the string into bytes using utf-8
byte[] utf8 = str.getBytes("UTF8");

// Encrypt
byte[] enc = ecipher.doFinal(utf8);

// Encode bytes to base64 to get a string
return new sun.misc.BASE64Encoder().encode(enc);
} catch (javax.crypto.BadPaddingException e) {
} catch (IllegalBlockSizeException e) {
} catch (UnsupportedEncodingException e) {
} catch (java.io.IOException e) {
}
return null;
}


public String deCryptPwd(String str)throws Exception{

// Decode base64 to get bytes
byte[] dec = new
sun.misc.BASE64Decoder().decodeBuffer(str);

// Decrypt
byte[] utf8 = dcipher.doFinal(dec);

// Decode using utf-8
return new String(utf8, "UTF8");
}
}



--the calling Code----

package project_wh;
import java.sql.*;
import java.util.*;
import javax.crypto.*;

public class test{
public static void main(String args[]) throws Exception {
InsertData is=new InsertData();
ResultSet rs=null;
String s2=null,s=null;
project_wh.Self_Util u=new project_wh.Self_Util();
String pwd="00000"; //presum the
plainted pass is "00000"
s=u.enCryptPwd(pwd); //encrypt the
plainted password

byte[] b_tmp=s.getBytes();
System.out.println("the length of the encrypted
password before storing->"+b_tmp.length);
System.out.println("the content of the encrypted
password before storing->"+s);

is.insertEmployee(2222,"test",s); //insert the
encrypted password into the database
rs=is.queryEmployee(2222); //retrive the encrpyted
password from the database
rs.first(); //point to the first row of result set
String s_pwd=rs.getString("Password"); //get the
encrypted password

byte[] b_tmp2=s_pwd.getBytes();
System.out.println("the length of the encrypted
password before decrpyting->"+b_tmp2.length);
System.out.println("the content of the encrypted
password before decrpyting->"+s_pwd);

s2=u.deCryptPwd(s_pwd); //decrypt the encrypted
password
System.out.println("the content of the encrypted
password after decrpyting->"+s2); //print the plainted password
}
}


when I first run the code ,I succeed with the following result:

the length of the encrypted password before storing->12
the content of the encrypted password before storing->H7uwFeag9WQ=
the length of the encrypted password before decrpyting->12
the content of the encrypted password before decrpyting->H7uwFeag9WQ=
the content of the encrypted password after decrpyting->00000


However,if the inserting block is commented as flow,only from
database retrving the encrypted
password and decrypting it ,I get some exception:

--this is modified calling code--
package project_wh;
import java.sql.*;
import java.util.*;
import javax.crypto.*;

public class test{
public static void main(String args[]) throws Exception {
InsertData is=new InsertData();
ResultSet rs=null;
String s2=null,s=null;
project_wh.Self_Util u=new project_wh.Self_Util();
/* String pwd="00000";
//presum the plainted pass is "00000"
s=u.enCryptPwd(pwd);
//encrypt the plainted password

byte[] b_tmp=s.getBytes();
System.out.println("the length of the encrypted
password before storing->"+b_tmp.length);
System.out.println("the content of the encrypted
password before storing->"+s);

is.insertEmployee(2222,"test",s);
//insert the encrypted password into the database
*/
rs=is.queryEmployee(2222);
//retrive the encrpyted password from the database
rs.first();
//point to the first row of result set
String s_pwd=rs.getString("Password");
//get the encrypted password

byte[] b_tmp2=s_pwd.getBytes();
System.out.println("the length of the encrypted
password before decrpyting->"+b_tmp2.length);
System.out.println("the content of the encrypted
password before decrpyting->"+s_pwd);

s2=u.deCryptPwd(s_pwd);
//decrypt the encrypted password
System.out.println("the content of the encrypted
password after decrpyting->"+s2);
//print the plainted password
}
}

this is the result I get after I run the code:

javax.crypto.BadPaddingException: Given final block not properly
padded
at com.sun.crypto.provider.DESCipher.engineDoFinal(DashoA6275)
at com.sun.crypto.provider.DESCipher.engineDoFinal(DashoA6275)
at javax.crypto.Cipher.doFinal(DashoA6275)
at project_wh.Self_Util.deCryptPwd(Self_Util.java:234)
the length of the encrypted password before decrpyting->12
the content of the encrypted password before decrpyting->H7uwFeag9WQ=
at project_wh.test.main(test.java:30)
Exception in thread "main"


Thanks in advance!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top