Results 1 to 2 of 2
  1. #1
    sachinsunny is offline Member
    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

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

    Java Code:
    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(ENCRYPT_MODE, 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(DECRYPT_MODE, key);
    
            byte[] plainBytes = cipher.doFinal(encryptedBytes);
    
            return plainBytes.toString();
        }
    
        /**
         * This method sets the EncryptionResourceBundle
         */
        private static void setEncryptionResourceBundle() {
            encryptionResourceBundle = ResourceBundle
                    .getBundle(ENCRYPTION_CONFIG_BUNDLE);
        }
    
        /**
         * This method returns the encryption Algorithm
         * 
         * @return String
         */
        private static String getAlgorithm() {
            return encryptionResourceBundle.getString(ENCRYPTION_ALGORITHM_KEY);
        }
    
        /**
         * @return String
         */
        private static String getCipherAlgorithm() {
            return encryptionResourceBundle
                    .getString(ENCRYPTION_CIPHER_ALGORITHM_KEY);
        }
    }
    Java Code:
    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();
            }
        }
    Java Code:
    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)

  2. #2
    sabre150 is offline Member
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    75
    Rep Power
    0

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

    The prime offending line is the return statement in
    Java Code:
        
       byte[] encryptedBytes = cipher.doFinal(plainBytes);
     
            return encryptedBytes.toString();
    since the toString() method on a byte array does no way reflect the content of the the byte array.

    Also, since in general the transformation bytes->String->bytes does not guarantee to get back the original bytes a String is not a valid container for binary data and ciphertext is binary data. If you absolutely must to have a String version of the ciphertext then you must encode it. You have many choices but Base64 or Hex encoding are fairly standard and Googling for 'apache commons codec' will find good well tested libraries.

    One final point, when converting your cleartext String to bytes you should not rely on the default character encoding since this makes it machine an OS dependent. I always explicitly specify utf-8 encoding which will work anywhere.

Similar Threads

  1. Combining multiple ints to 2 bytes.
    By gonzalioz in forum Advanced Java
    Replies: 3
    Last Post: 05-12-2011, 04:12 PM
  2. Replies: 5
    Last Post: 03-17-2011, 05:19 PM
  3. Vertical padding problem
    By phil128 in forum AWT / Swing
    Replies: 1
    Last Post: 01-30-2011, 10:13 PM
  4. Padding Problem
    By Srikar in forum Advanced Java
    Replies: 1
    Last Post: 08-20-2010, 09:39 PM
  5. OAEP Padding Problem by encryption
    By Buhmann in forum Advanced Java
    Replies: 1
    Last Post: 09-07-2009, 02:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •