This discussion is archived
4 Replies Latest reply: Dec 6, 2012 2:16 AM by 789595 RSS

Decryption Problem | Input length (with padding) not multiple of 8 bytes

789595 Newbie
Currently Being Moderated
Utility Class
public class EncryptionUtil {
 
    private static ResourceBundle encryptionResourceBundle;
 
    private static Cipher cipher;
 
    private static SecretKey key;
 
    static {
        setEncryptionResourceBundle();
        if (null == cipher) {
            KeyGenerator keygenerator;
            try {
                keygenerator = KeyGenerator.getInstance(getAlgorithm());
                key = keygenerator.generateKey();
                cipher = Cipher.getInstance(getCipherAlgorithm());
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (NoSuchPaddingException e) {
                e.printStackTrace();
            }
        }
    }
 
    /**
     * This method returns a encrypted String for the plain String provided.
     *
     * @param plainString
     *            - input String
     *
     * @return String
     *
     * @throws Exception
     */
    public static String encryptString(String plainString) throws Exception {
        byte[] plainBytes = null;
        if (plainString == null) {
            return null;
        }
        plainBytes = plainString.getBytes();
 
        cipher.init(ENCRYPTMODE, key);
 
        byte[] encryptedBytes = cipher.doFinal(plainBytes);
 
        return encryptedBytes.toString();
    }
 
    /**
     * This method returns a plain String for the encrypted String provided.
     *
     * @param encryptedString
     *            - input String
     * @return String
     * @throws Exception
     */
    public static String decryptString(String encryptedString) throws Exception {
        if (encryptedString == null) {
            return null;
        }
        byte[] encryptedBytes = encryptedString.getBytes();
 
        cipher.init(DECRYPTMODE, key);
 
        byte[] plainBytes = cipher.doFinal(encryptedBytes);
 
        return plainBytes.toString();
    }
 
    /**
     * This method sets the EncryptionResourceBundle
     */
    private static void setEncryptionResourceBundle() {
        encryptionResourceBundle = ResourceBundle
                .getBundle(ENCRYPTIONCONFIGBUNDLE);
    }
 
    /**
     * This method returns the encryption Algorithm
     *
     * @return String
     */
    private static String getAlgorithm() {
        return encryptionResourceBundle.getString(ENCRYPTIONALGORITHMKEY);
    }
 
    /**
     * @return String
     */
    private static String getCipherAlgorithm() {
        return encryptionResourceBundle
                .getString(ENCRYPTIONCIPHERALGORITHMKEY);
    }
} 

*
*
--------------------------------------------------------------------------------------------------------------------------------------------------------

Main Class :

*
*

public static void main(String args[]) throws UnsupportedEncodingException,
            SiteMapNotFoundException, IllegalParameterException,
            NoMatchingNodeFoundException {
        String txt = "Hello World Hello World Hello World Hello World Hello World Hello World Hello World ";
        try {
            txt = EncryptionUtil.encryptString(txt);
            System.out.println("Encrypted Text : "  txt  ":"  txt.length());
            Thread.sleep(3000);
            txt = EncryptionUtil.decryptString(txt);
            System.out.println("Decrypted Text : "  txt);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
Exception Trace :
javax.crypto.IllegalBlockSizeException: Input length (with padding) not multiple of 8 bytes
    at com.ibm.crypto.provider.DESCipher.a(Unknown Source)
    at com.ibm.crypto.provider.DESCipher.engineDoFinal(Unknown Source)
    at com.ibm.crypto.provider.DESCipher.engineDoFinal(Unknown Source)
    at javax.crypto.Cipher.doFinal(Unknown Source)
    at com.johndeere.myjd.encryption.EncryptionUtil.decryptString(EncryptionUtil.java:88)
    at sachin.main(sachin.java:17)

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points