Results 1 to 1 of 1
Thread: BadPaddingException
- 12-23-2007, 04:00 PM #1
Member
- Join Date
- Dec 2007
- Posts
- 1
- Rep Power
- 0
BadPaddingException
I've been getting this BadPaddingException;
Here are some parts of the code;Java 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*..)
encrypt() method;
decrypt() method;Java 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; }
Method, getDefaultCipher;Java 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; }
Exception was raised when the file size if bigger than BUFFERSIZE in decrypting back.Java 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; }
Can anyone suggest me?Last edited by swmk; 12-24-2007 at 05:35 PM.


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks