Question

I can't understand why this code fails raising exception with the input bnlbnl18 and not with other values:

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
/**
 * 
 * @param password
 * @return
 * @throws AuthException 
 */
public static String encryptPassword(String password) throws AuthException{
    byte[] textEncrypted = "".getBytes();
    try{

        DESKeySpec keySpec = new DESKeySpec(Constants.DESkey);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey myDesKey = keyFactory.generateSecret(keySpec);

        Cipher desCipher;

        // Create the cipher 
        //desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        desCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

        AlgorithmParameterSpec alogrithm_specs = new IvParameterSpec(Constants.DESkey);

        // Initialize the cipher for encryption
        desCipher.init(Cipher.ENCRYPT_MODE, myDesKey, alogrithm_specs);

        //sensitive information
        byte[] text = password.getBytes();

        // Encrypt the text
        textEncrypted = desCipher.doFinal(text);

    }catch(NoSuchAlgorithmException e){
        e.printStackTrace();
        logger.error("[AUTH] Auth Exception...there is something wrong at the encryption phase\n" + e.getMessage());
        throw new AuthException("Auth Exception...there is something wrong at the encryption phase");
    }catch(NoSuchPaddingException e){
        e.printStackTrace();
        logger.error("[AUTH] Auth Exception...there is something wrong at the encryption phase\n" + e.getMessage());
        throw new AuthException("Auth Exception...there is something wrong at the encryption phase");
    }catch(InvalidKeyException e){
        e.printStackTrace();
        logger.error("[AUTH] Auth Exception...there is something wrong at the encryption phase\n" + e.getMessage());
        throw new AuthException("Auth Exception...there is something wrong at the encryption phase");
    }catch(IllegalBlockSizeException e){
        e.printStackTrace();
        logger.error("[AUTH] Auth Exception...there is something wrong at the encryption phase\n" + e.getMessage());
        throw new AuthException("Auth Exception...there is something wrong at the encryption phase");
    }catch(BadPaddingException e){
        e.printStackTrace();
        logger.error("[AUTH] Auth Exception...there is something wrong at the encryption phase\n" + e.getMessage());
        throw new AuthException("Auth Exception...there is something wrong at the encryption phase");
    } catch (InvalidKeySpecException e) {
        e.printStackTrace();
        logger.error("[AUTH] Auth Exception...there is something wrong at the encryption phase\n" + e.getMessage());
        throw new AuthException("Auth Exception...there is something wrong at the encryption phase");
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
        logger.error("[AUTH] Auth Exception...there is something wrong at the encryption phase\n" + e.getMessage());
        throw new AuthException("Auth Exception...there is something wrong at the encryption phase");
    } 

    return new String(textEncrypted);

}   
public static String decryptPassword(String passwordToDecrypt) throws AuthException{

    DESKeySpec keySpec;
    byte[] textDecrypted = "".getBytes();
    try {
        keySpec = new DESKeySpec(Constants.DESkey);

        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey myDesKey = keyFactory.generateSecret(keySpec);

        Cipher desCipher;

        AlgorithmParameterSpec alogrithm_specs = new IvParameterSpec(Constants.DESkey);

        // Create the cipher 
        //desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        desCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

        // Initialize the same cipher for decryption
        desCipher.init(Cipher.DECRYPT_MODE, myDesKey, alogrithm_specs);

        // Decrypt the text
        byte[] passwordToDecryptByte = passwordToDecrypt.getBytes();



       textDecrypted = desCipher.doFinal(passwordToDecryptByte);

    } catch (InvalidKeyException e) {
        e.printStackTrace();
        logger.error("[AUTH] Auth Exception...there is something wrong at the encryption phase\n" + e.getMessage());
        throw new AuthException("Auth Exception...there is something wrong at the decryption phase");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        logger.error("[AUTH] Auth Exception...there is something wrong at the encryption phase\n" + e.getMessage());
        throw new AuthException("Auth Exception...there is something wrong at the decryption phase");
    } catch (InvalidKeySpecException e) {
        e.printStackTrace();
        logger.error("[AUTH] Auth Exception...there is something wrong at the encryption phase\n" + e.getMessage());
        throw new AuthException("Auth Exception...there is something wrong at the decryption phase");
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
        logger.error("[AUTH] Auth Exception...there is something wrong at the encryption phase\n" + e.getMessage());
        throw new AuthException("Auth Exception...there is something wrong at the decryption phase");
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
        logger.error("[AUTH] Auth Exception...there is something wrong at the encryption phase\n" + e.getMessage());
        throw new AuthException("Auth Exception...there is something wrong at the decryption phase");
    } catch (BadPaddingException e) {
        e.printStackTrace();
        logger.error("Auth Exception...there is something wrong at the encryption phase\n" + e.getMessage());
        throw new AuthException("Auth Exception...there is something wrong at the decryption phase");
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
        logger.error("[AUTH] Auth Exception...there is something wrong at the encryption phase\n" + e.getMessage());
        throw new AuthException("Auth Exception...there is something wrong at the decryption phase");
    }

    return new String(textDecrypted);

}


public static void main(String[] args) throws AuthException, UnsupportedEncodingException{
    String password = URLEncoder.encode(encryptPassword("bnlbnl18"), "UTF-8");

    System.out.println("\"" + URLDecoder.decode(password, "UTF-8") + "\"" + decryptPassword(URLDecoder.decode(password,"UTF-8")));

}

And this is the exception

javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:811)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:676)
at com.sun.crypto.provider.DESCipher.engineDoFinal(DESCipher.java:314)
at javax.crypto.Cipher.doFinal(Cipher.java:2087)
    ...

I think I miss something. It's strange as behaviour. Do you have any ideas? Is it wrong the "DES" solution?

Thanks in advance.

Was it helpful?

Solution 2

@JB Nizet : following your instructions, here the code

/**
 * 
 * @param password
 * @return
 * @throws AuthException 
 * @throws UnsupportedEncodingException 
 */
public static String encryptPassword(String password) throws AuthException, UnsupportedEncodingException{
    byte[] textEncrypted = "".getBytes();
    try{

        DESKeySpec keySpec = new DESKeySpec(Constants.DESkey);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey myDesKey = keyFactory.generateSecret(keySpec);

        Cipher desCipher;

        // Create the cipher 
        desCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

        AlgorithmParameterSpec alogrithm_specs = new IvParameterSpec(Constants.DESkey);

        // Initialize the cipher for encryption
        desCipher.init(Cipher.ENCRYPT_MODE, myDesKey, alogrithm_specs);

        //sensitive information
        byte[] text = password.getBytes("UTF-8");

        // Encrypt the text
        textEncrypted = desCipher.doFinal(text);

    }catch(NoSuchAlgorithmException e){
        e.printStackTrace();
        logger.error("[AUTH] Auth Exception...there is something wrong at the encryption phase\n" + e.getMessage());
        throw new AuthException("Auth Exception...there is something wrong at the encryption phase");
    }catch(NoSuchPaddingException e){
        e.printStackTrace();
        logger.error("[AUTH] Auth Exception...there is something wrong at the encryption phase\n" + e.getMessage());
        throw new AuthException("Auth Exception...there is something wrong at the encryption phase");
    }catch(InvalidKeyException e){
        e.printStackTrace();
        logger.error("[AUTH] Auth Exception...there is something wrong at the encryption phase\n" + e.getMessage());
        throw new AuthException("Auth Exception...there is something wrong at the encryption phase");
    }catch(IllegalBlockSizeException e){
        e.printStackTrace();
        logger.error("[AUTH] Auth Exception...there is something wrong at the encryption phase\n" + e.getMessage());
        throw new AuthException("Auth Exception...there is something wrong at the encryption phase");
    }catch(BadPaddingException e){
        e.printStackTrace();
        logger.error("[AUTH] Auth Exception...there is something wrong at the encryption phase\n" + e.getMessage());
        throw new AuthException("Auth Exception...there is something wrong at the encryption phase");
    } catch (InvalidKeySpecException e) {
        e.printStackTrace();
        logger.error("[AUTH] Auth Exception...there is something wrong at the encryption phase\n" + e.getMessage());
        throw new AuthException("Auth Exception...there is something wrong at the encryption phase");
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
        logger.error("[AUTH] Auth Exception...there is something wrong at the encryption phase\n" + e.getMessage());
        throw new AuthException("Auth Exception...there is something wrong at the encryption phase");
    } 

    return BaseEncoding.base64().encode(textEncrypted);

}   

/**
 * 
 * @param passwordDecrypted
 * @return
 * @throws AuthException 
 * @throws UnsupportedEncodingException 
 */

public static String decryptPassword(String passwordToDecrypt) throws AuthException, UnsupportedEncodingException{

    DESKeySpec keySpec;
    byte[] textDecrypted = "".getBytes();
    try {
        keySpec = new DESKeySpec(Constants.DESkey);

        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey myDesKey = keyFactory.generateSecret(keySpec);

        Cipher desCipher;

        AlgorithmParameterSpec alogrithm_specs = new IvParameterSpec(Constants.DESkey);

        // Create the cipher 
        //desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        desCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

        // Initialize the same cipher for decryption
        desCipher.init(Cipher.DECRYPT_MODE, myDesKey, alogrithm_specs);

        // Decrypt the text
        byte[] passwordToDecryptByte = BaseEncoding.base64().decode(passwordToDecrypt);

       textDecrypted = desCipher.doFinal(passwordToDecryptByte);

    } catch (InvalidKeyException e) {
        e.printStackTrace();
        logger.error("[AUTH] Auth Exception...there is something wrong at the encryption phase\n" + e.getMessage());
        throw new AuthException("Auth Exception...there is something wrong at the decryption phase");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        logger.error("[AUTH] Auth Exception...there is something wrong at the encryption phase\n" + e.getMessage());
        throw new AuthException("Auth Exception...there is something wrong at the decryption phase");
    } catch (InvalidKeySpecException e) {
        e.printStackTrace();
        logger.error("[AUTH] Auth Exception...there is something wrong at the encryption phase\n" + e.getMessage());
        throw new AuthException("Auth Exception...there is something wrong at the decryption phase");
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
        logger.error("[AUTH] Auth Exception...there is something wrong at the encryption phase\n" + e.getMessage());
        throw new AuthException("Auth Exception...there is something wrong at the decryption phase");
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
        logger.error("[AUTH] Auth Exception...there is something wrong at the encryption phase\n" + e.getMessage());
        throw new AuthException("Auth Exception...there is something wrong at the decryption phase");
    } catch (BadPaddingException e) {
        e.printStackTrace();
        logger.error("Auth Exception...there is something wrong at the encryption phase\n" + e.getMessage());
        throw new AuthException("Auth Exception...there is something wrong at the decryption phase");
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
        logger.error("[AUTH] Auth Exception...there is something wrong at the encryption phase\n" + e.getMessage());
        throw new AuthException("Auth Exception...there is something wrong at the decryption phase");
    }

    return new String(textDecrypted);


}


public static void main(String[] args) throws AuthException, UnsupportedEncodingException{
    String password = encryptPassword("testtest19");

    System.out.println(password + " --> " + "\"" + decryptPassword(password)+ "\"");

}

OTHER TIPS

You're transforming a byte array, containing any kind of byte, into a String, using

new String(bytes);

This constructor expects the bytes to represent characters transformed to bytes using your default platform encoding, and it has a very good chance of not being the case. Don't use String to represent binary data? Leave it as a byte array. Or encode the byte array using Hex or Base64 encoding.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top