Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

javax.crypto.BadPaddingException: Given final block not properly padded
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 © 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!

Report this thread to moderator Post Follow-up to this message
Old Post
yishushui@hotmail.com
03-20-04 01:55 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Java Security archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 11:48 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.