Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Input length must be multiple of 8 when decrypting with padded cipher

807580Jun 21 2010 — edited Jun 22 2010
Hi,

I'm debugging issues with encrypted passwords on provisioned resources. Passwords are base64 encoded > triple DES > base 64 encoded again.
When reversing this encryption I am getting :
Input length must be multiple of 8 when decrypting with padded cipher

I believe I may need to pad the byte array after the first base64 decode. Any insight would be greatly appreciated.

Thanks,
Kym
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
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.DESedeKeySpec;

public class Test{

    /******************************************
    * Helpers for doing encryption
    ******************************************/
    /** Read a TripleDES secret key */
    public static SecretKey readKey(String s)
        throws IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException {

        // Convert the raw bytes to a secret key like this
        DESedeKeySpec keyspec = new DESedeKeySpec(s.getBytes());
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
        SecretKey key = keyfactory.generateSecret(keyspec);
        return key;
    }

    /** return an encrypted string, given the Key and plaintext String */
    public static String encrypt(SecretKey key, String in)
        throws NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, IOException, IllegalBlockSizeException, BadPaddingException {

        // Create and initialize the encryption engine
        Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key);

        byte[] cipherbyte = cipher.doFinal(in.getBytes());
        String str = Base64.encodeBytes(cipherbyte);

        //observe what the encrypted/base64 encoded string value is
        return str;
    }

    public static byte[] decrypt(SecretKey key, String in)
        throws NoSuchAlgorithmException, InvalidKeyException, IOException, IllegalBlockSizeException, NoSuchPaddingException, BadPaddingException {

        // Create and initialize the decryption engine
        Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, key);

		byte[] cipherbyte = cipher.doFinal(in.getBytes());
		return cipherbyte;
    }

    public static void main(String[] args) {
    	try  {

    	 	String inkey = "8KSDg9ej4klgAdfkjSDkh459";
			SecretKey key = readKey(inkey);

			String myTest = "com.waveset.object.GenericObject";
			String base64Encoded = Base64.encodeBytes(myTest.getBytes());
			String encryptedStr = encrypt(key, base64Encoded);
			System.out.println(encryptedStr);


			// String myEncTest = "d5zHqqbRi+pPdtKebic9YAU8BthT7uuq0NVnnzqeExD3pENni71v40sPe1a1GPzDZYY9dIsFsyE==";
			// String myEncTest = "d5zHqqbRi+pPdtKebic9YAU8BthT7uuq0NVnnzqeExD3pENni71v40tZHUbLwWNUYLD1n8s/9S8mc2Dyq/LFcA==";
			String myEncTest = "d5zHqqbRi+pPdtKebic9YAU8BthT7uuq0NVnnzqeExD3pENni71v44CnhaJLDDZsglBZnSMfwycmc2Dyq/LFcA==";
			String base64Decoded = new String(Base64.decode(myEncTest));
			System.out.println("base64Decoded: " + base64Decoded);
			System.out.println("Length: " + base64Decoded.length());			
	
			byte[] decryptedb = decrypt(key, base64Decoded);
			System.out.println("here");
			String oldstr = new String(decryptedb);
			String basestr = new String(Base64.decode(oldstr));
			System.out.println(basestr);

		} catch (Exception e) {
            		System.err.println("Exception: " + e.getMessage());
		}
    }
}
Edited by: KymMcInerney on Jun 21, 2010 6:29 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 20 2010
Added on Jun 21 2010
4 comments
6,866 views