javax.crypto.BadPaddingException: Given final block not properly padded
Folks:
I am getting the error (subject line) - when I try to decrypt on Solaris 10 OS. It works fine on Windows 2003 and Linux and also on Solaris 9.0. Any clues about this.
The stack trace is:
javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.SunJCE_h.b(DashoA12275)
at com.sun.crypto.provider.SunJCE_h.b(DashoA12275)
at com.sun.crypto.provider.BlowfishCipher.engineDoFinal(DashoA12275)
at javax.crypto.Cipher.doFinal(DashoA12275)
at com.powerup.configmgr.util.commandline.credentials.CryptoHelper.decrypt(CryptoHelper.java:123)
The line number 123 is the line below correspoinding to doFinal method call.
publicstaticsynchronizedbyte[] decrypt(byte[] input)
throws Exception
{
byte[] ret =null;
//System.out.println("ENCRYPTED INPUT= "+input);
Cipher c = getCipher();
c.init(Cipher.DECRYPT_MODE,getSecretKey(),getSalt());
if(input !=null)
ret = c.doFinal(input);
else
ret =newbyte[0];
//System.out.println("DECRYPTED INPUT="+ret);
return ret;
}
[1740 byte] By [
veejnasa] at [2007-10-3 5:16:33]

Thanks for your input...
It is none of the the above. I debugged all day to find that - Cust1-RW get different set of encrypted bytes on Solaris 10 - that cannot be decrypted ever. The same string - encryption works good on Linux and Windows.
The character hyphen is throwing it off...
Here are the byte arrays:
Linux: Input Bytes:67, 117, 115, 116, 49, 45, 82, 87 --> Encrypted Byte Array: 47, 119, 105, -95, 119, 41, -22, -47, 109, -13, 60, 7, -103, -23, 124, 31
Solaris: Input Bytes: 67, 117, 115, 116, 49, 45, 82, 87 --> Encrypted Byte Array: 47, 119, 105, -95, 119, 41, -22, -47, 99,34, -104, 93, 66, 71, -91, 63
Thanks for your help in advance
- Sanjeev
Here is my class - that does the encrypt and decrypt - I have KEY and SALT - defined.
/*
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class CryptoHelper {
public final static int BASE64 = 0;
private final static byte[] KEY = { (byte)164, (byte)208, (byte)200, (byte)118,
(byte)112, (byte)181, (byte)82, (byte)74,
(byte)30, (byte)179, (byte)9, (byte)125,
(byte)95, (byte)97, (byte)51, (byte)226};
private final static byte[] SALT = {(byte)129, (byte)187, (byte)151, (byte)73,
(byte)225, (byte)239, (byte)49, (byte)24};
private final static String CIPHER= "Blowfish/CBC/PKCS5Padding";
private final static String SECRET_KEY= "Blowfish";
private static Cipher cipher;
private static IvParameterSpec salt;
private static SecretKeySpec secretKey;
private static Cipher getCipher() throws NoSuchAlgorithmException, NoSuchPaddingException{
if( cipher == null ){
cipher = Cipher.getInstance(CIPHER);
}
return cipher;
}
private static SecretKeySpec getSecretKey() throws NoSuchAlgorithmException{
if( secretKey == null ){
secretKey = new SecretKeySpec(KEY, SECRET_KEY);
}
return secretKey;
}
private static IvParameterSpec getSalt() throws NoSuchPaddingException{
if( salt == null ){
salt = new IvParameterSpec(SALT);
}
return salt;
}
public static synchronized void writeEncrypted(File file, String value)
throws Exception
{
Cipher c = getCipher();
c.init(Cipher.ENCRYPT_MODE,getSecretKey(),getSalt());
FileOutputStream fos = new FileOutputStream( file );
try{
CipherOutputStream cos = new CipherOutputStream(fos,c);
try{
ByteArrayInputStream bais = new ByteArrayInputStream( value.getBytes() );
byte[] buf = new byte[8];
int len = bais.read(buf);
while(len > 0){
cos.write(buf,0,len);
len = bais.read(buf);
}
}finally{
cos.close();
}
}finally{
fos.close();
}
}
public static synchronized byte[] encrypt(byte[] input)
throws Exception
{
Cipher c = getCipher();
c.init(Cipher.ENCRYPT_MODE,getSecretKey(),getSalt());
return c.doFinal(input);
}
public static synchronized byte[] decrypt(byte[] input)
throws Exception
{
Cipher c = getCipher();
c.init(Cipher.DECRYPT_MODE,getSecretKey(),getSalt());
return c.doFinal(input);
}
public static synchronized String readEncrypted(File file)
throws Exception
{
String returnVal = null;
Cipher c = getCipher();
c.init(Cipher.DECRYPT_MODE,getSecretKey(),getSalt());
FileInputStream fis = new FileInputStream(file);
try{
CipherInputStream cis = new CipherInputStream(fis,c);
try{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[8];
int len = cis.read(buf);
while(len > 0){
baos.write(buf,0,len);
len = cis.read(buf);
}
returnVal = baos.toString();
}finally{
cis.close();
}
}finally{
fis.close();
}
return returnVal;
}
}