Exception: "Given final block not properly padded" in Linux, but it works in Windows
        Posted  
        
            by 
                user1685364
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by user1685364
        
        
        
        Published on 2012-09-20T09:03:53Z
        Indexed on 
            2012/09/20
            9:37 UTC
        
        
        Read the original article
        Hit count: 444
        
java
|encryption
My application works in windows, but fails in Linux with Given final block not properly padded exception.
Configuration:
- JDK Version: 1.6
- Windows : version 7
- Linux : CentOS 5.8 64bit
My code is below:
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class SecurityKey {
    private static Key key = null;
    private static String encode = "UTF-8";
    private static String cipherKey = "DES/ECB/PKCS5Padding";
    static  {
        try {
            KeyGenerator generator = KeyGenerator.getInstance("DES");
            String seedStr = "test";
            generator.init(new SecureRandom(seedStr.getBytes()));
            key = generator.generateKey();
        } catch(Exception e) {
        }
    }
    // SecurityKey.decodeKey("password")
    public static String decodeKey(String str) throws Exception  {
        if(str == null)
            return str;
        Cipher cipher = null;
        byte[] raw = null;
        BASE64Decoder decoder = new BASE64Decoder();
        String result = null;
        cipher = Cipher.getInstance(cipherKey);
        cipher.init(Cipher.DECRYPT_MODE, key);
        raw = decoder.decodeBuffer(str);
        byte[] stringBytes = null;
        stringBytes = cipher.doFinal(raw); // Exception!!!!
        result = new String(stringBytes, encode);
        return result;
    }
}
At the line:
   ciper.doFilnal(raw);
the following exception is thrown:
   javax.crypto.BadPaddingException: Given final block not properly padded
How can I fix this issue?
© Stack Overflow or respective owner