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 {
            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) {
  • \$\begingroup\$ Side note: the method name is wrong. It should be doEncryption. \$\endgroup\$ – Roland Illig Sep 14 '17 at 20:24
  • \$\begingroup\$ Please provide the complete code to reproduce your claim. I just tried, and my encrypted byte array is 16 bytes long, exactly as expected. \$\endgroup\$ – Roland Illig Sep 14 '17 at 20:30

Your Answer


Browse other questions tagged or ask your own question.