title: AES decryption gives extra zeros in the result
body: <p>I have the following code to encrypt-decrypt a string using a key and random IV. However during decrypt I get a lot of zeros at the end in my IDE.</p>
  <pre><code>public class Example {
  
      private static final String AES_MODE = &quot;AES/CBC/PKCS5Padding&quot;;
      private static final String CHARSET = &quot;UTF-8&quot;;
      private static final String HASH_ALGORITHM = &quot;SHA-256&quot;;
      private static final String KEY = &quot;SUPER_SECURE_KEY&quot;;
  
      private static SecretKeySpec getSecretKey() throws NoSuchAlgorithmException, UnsupportedEncodingException {
          final MessageDigest digest = MessageDigest.getInstance(HASH_ALGORITHM);
          byte[] bytes = KEY.getBytes(CHARSET);
          digest.update(bytes, 0, bytes.length);
          byte[] key = digest.digest();
  
          return new SecretKeySpec(key, &quot;AES&quot;);
      }
  
      public static String encrypt(String message) {
          if(message == null || message.isEmpty()) {
              return &quot;&quot;;
          }
  
          try {
              final SecretKeySpec key = getSecretKey();
              byte[] cipherText = encrypt(key, message.getBytes(CHARSET));
              return Base64.getEncoder().encodeToString(cipherText);
          } catch (Exception e) {
              System.out.print(e.toString());
              return &quot;&quot;;
          }
      }
  
      private static byte[] encrypt(final SecretKeySpec key, final byte[] message) throws GeneralSecurityException {
  
          final Cipher cipher = Cipher.getInstance(AES_MODE);
  
          byte[] iv = new byte[cipher.getBlockSize()];
          new SecureRandom().nextBytes(iv);
  
          IvParameterSpec ivSpec = new IvParameterSpec(iv);
          cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
  
          byte[] ciphertext = new byte[iv.length + cipher.getOutputSize(message.length)];
          System.arraycopy(iv, 0, ciphertext, 0, iv.length);
          cipher.doFinal(message, 0, message.length, ciphertext, iv.length);
          return ciphertext;
      }
  
      // ========================================================================================
  
      public static String decrypt(String base64EncodedCipherText) {
          if(base64EncodedCipherText == null || base64EncodedCipherText.isEmpty()) {
              return &quot;&quot;;
          }
  
          try {
              final SecretKeySpec key = getSecretKey();
  
              byte[] decodedCipherText = Base64.getDecoder().decode(base64EncodedCipherText);
              byte[] decryptedBytes = decrypt(key, decodedCipherText);
  
              return new String(decryptedBytes, CHARSET);
          } catch (Exception e) {
              System.out.print(e.toString());
              return &quot;&quot;;
          }
      }
  
      private static byte[] decrypt(final SecretKeySpec key, final byte[] decodedCipherText) throws GeneralSecurityException {
          final Cipher cipher = Cipher.getInstance(AES_MODE);
  
          IvParameterSpec ivSpec = new IvParameterSpec(decodedCipherText, 0, cipher.getBlockSize());
          cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
  
          int plainTextLength  = decodedCipherText.length - cipher.getBlockSize();
          byte[] plaintext = new byte[plainTextLength];
  
          cipher.doFinal(decodedCipherText, cipher.getBlockSize(), plainTextLength, plaintext, 0);
  
          return plaintext;
  
          // return cipher.doFinal(decodedCipherText);
      }
  
      // ========================================================================================
  
      public static void main(String[] args) {
  
          String message = &quot;Message to encrypt.&quot;;
  
          String encryptedText = encrypt(message);
          System.out.println(encryptedText);
  
          String decryptedText= decrypt(encryptedText);
          System.out.println(decryptedText);
  
      }
  
  }
  </code></pre>
  <p>The output I get in IntelliJ IDEA is:
  <a href="https://i.stack.imgur.com/ZqfnI.png" rel="nofollow noreferrer">here</a></p>
  <p>I think I am correctly separating the IV from the ciphertext, and decrypt the ciphertext with the key and the random IV. But still end up getting zeros in the end. Any pointers to what is wrong?</p>