I've been getting this BadPaddingException;
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;
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;
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;
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?