Java Crypto library number of padding bytes using PKCS5Padding I have implemented AES encryption in Java using the following function. What puzzles me is the number of padding bytes added using the PKCS5Padding method. For example, when calling the following function with a text byte of length 12, the output is a 32 byte array. I would expect a 16 byte output of which 4 bytes are padding.
How can the initialization of the cipher be changed to do this?

public byte[] do_decryption(byte[] text) {

        String key_val = new String("1234567890123456");
        byte[] key = key_val.getBytes();

        try {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            System.out.println("Block size: " + cipher.getBlockSize());

            byte[] output = cipher.doFinal(text);
            return output;

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
"PKCS5Padding" in Java is really PKCS#7 compatible padding. It adds 1 to 16 bytes for a 16-byte-block cipher like AES. If you view those bytes as hexadecimals, e.g. in a debugger, then it may look like 32 characters instead.

ECB mode is almost never secure enough to be used. Use GCM or another authenticated mode (e.g. CCM or EAX).



public byte[] do_decryption(byte[] text) {


For Java we'd use CamelCase starting with a lower case letter (doDecryption or performDecryption).

text is not a good name for a byte array. plaintext for encryption or ciphertext for decryption could be acceptable though.

String key_val = new String("1234567890123456");
byte[] key = key_val.getBytes();


Obviously the key should not be in the source code.

Furthermore, you should almost never use getBytes without specifying the character encoding (e.g. use StandardCharsets.US_ASCII as parameter for the overloaded function.

Variable names except constants should not contain underscores.

cipher.init(Cipher.ENCRYPT_MODE, secretKey);


Already noted in the comments, either the method is badly named or this is plain wrong. Encryption != decryption.

e.printStackTrace();


Please handle exceptions properly.