Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-23-2007, 05:00 PM
Member
 
Join Date: Dec 2007
Posts: 1
swmk is on a distinguished road
BadPaddingException
I've been getting this BadPaddingException;
Code:
javax.crypto.BadPaddingException: Given final block not properly padded at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..) at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..) at com.sun.crypto.provider.PKCS12PBECipherCore.b(DashoA13*..) at com.sun.crypto.provider.PKCS12PBECipherCore$PBEWithSHA1AndRC2_40.engineDoFinal(DashoA13*..) at javax.crypto.Cipher.doFinal(DashoA13*..)
Here are some parts of the code;

encrypt() method;
Code:
public Status encrypt(EncryptParams params){ Status retValue = Status.getDefault(); params.makeup(); File source = new File(params.getSource()); File destination = new File(params.getDestination()); try{ FileInputStream fis = new FileInputStream(source); FileOutputStream fos = new FileOutputStream(destination); BufferedOutputStream bos = new BufferedOutputStream(fos); Cipher cipher = getDefaultChipher(params.getPassword(), Cipher.ENCRYPT_MODE); if(cipher == null){ retValue.setStatus(StatusValue.ERROR.value()); retValue.setDescription("Unable to initialize encryption parameters."); return retValue; } long length = source.length(); final int BUFFERSIZE = (5 * 1024) * 1024; byte inbuffer [] = new byte[BUFFERSIZE]; byte buffer []; while(true){ if(BUFFERSIZE > fis.available()){ inbuffer = new byte[fis.available()]; } int nbytes = fis.read(inbuffer); if(nbytes == -1){ break; } buffer = cipher.doFinal(inbuffer); bos.write(buffer); if(fis.available()<1){break;} } fis.close(); bos.close(); }catch(IOException e){ retValue.setException(e); return retValue; }catch(Exception e){ e.printStackTrace(); retValue.setException(e); return retValue; } retValue = Status.getSuccess(); return retValue; }
decrypt() method;
Code:
public Status decrypt(EncryptParams params){ Status retValue = Status.getDefault(); params.makeup(); File source = new File(params.getSource()); File destination = new File(params.getDestination()); if(!source.exists()){ retValue.setStatus(StatusValue.ERROR.value()); retValue.setDescription("Source must exist."); return retValue; } if(destination.exists()){ retValue.setStatus(StatusValue.ERROR.value()); retValue.setDescription("Destination file must not exist."); return retValue; } try{ FileInputStream fis = new FileInputStream(source); FileOutputStream fos = new FileOutputStream(destination); BufferedOutputStream bos = new BufferedOutputStream(fos); Cipher cipher = getDefaultChipher(params.getPassword(), Cipher.DECRYPT_MODE); if(cipher == null){ retValue.setStatus(StatusValue.ERROR.value()); retValue.setDescription("Unable to initialize encryption parameters."); return retValue; } long length = source.length(); final int BUFFERSIZE = (5 * 1024) * 1024; byte inbuffer [] = new byte[BUFFERSIZE]; byte buffer []; while(true){ if(BUFFERSIZE > fis.available()){ inbuffer = new byte[fis.available()]; } int nbytes = fis.read(inbuffer); if(nbytes == -1){ break; } buffer = cipher.doFinal(inbuffer); bos.write(buffer); if(fis.available()<1){break;} } fis.close(); bos.close(); }catch(IOException e){ retValue.setException(e); return retValue; }catch(Exception e){ e.printStackTrace(); retValue.setException(e); return retValue; } retValue = Status.getSuccess(); return retValue; }
Method, getDefaultCipher;

Code:
private Cipher getDefaultChipher(String passphrase, int mode){ Cipher retValue = null; byte []salt = new byte [DEFAULT_SALT]; PBEKeySpec ks = new PBEKeySpec(passphrase.toCharArray()); try{ SecretKeyFactory skf = SecretKeyFactory.getInstance(DEFAULT_ALGORITHM); SecretKey key = skf.generateSecret(ks); PBEParameterSpec pspec = new PBEParameterSpec(salt, DEFAULT_ITERATION); Cipher cipher = Cipher.getInstance(DEFAULT_ALGORITHM); cipher.init(mode, key, pspec); retValue = cipher; }catch(Exception e){ e.printStackTrace(); return null; } return retValue; }
Exception was raised when the file size if bigger than BUFFERSIZE in decrypting back.

Can anyone suggest me?

Last edited by swmk : 12-24-2007 at 06:35 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 03:53 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org