title: "javax.crypto.ShortBufferException: Output buffer is not sufficient." when trying to encode a string with AES
body: <p>When the application is run on my localhost, it works fine, but if I try to run it on remote Digital Ocean server, <a href="http://freetexthost.com/owbdk3us0m" rel="nofollow noreferrer">this exception</a> is thrown.</p>
  
  <p><a href="http://freetexthost.com/qifayzwe6s" rel="nofollow noreferrer">Here</a> you can find the whole code. This is the function that causes the exception:</p>
  
  <pre><code>public static String encode(String src, String key) {
      // This is the function that has the exception. I put it all inside try-catch because an exception here would mean the program cannot work at all
  
      try {
          SecretKeySpec k = new SecretKeySpec(key.substring(0, 16).getBytes(), "AES");
          IvParameterSpec iv = new IvParameterSpec(key.substring(0, 16).getBytes());
  
          CryptoCipher encipher;
          encipher = Utils.getCipherInstance("AES/CBC/PKCS5Padding", getProps());
  
          byte[] input = src.getBytes();
          byte[] output = new byte[(input.length / 16 + 1) * 16];
  
          encipher.init(Cipher.ENCRYPT_MODE, k, iv);
  
          int updateBytes = encipher.update(input, 0, input.length, output, 0);
          int finalBytes = encipher.doFinal(input, 0, 0, output, updateBytes);
          encipher.close();
  
          return hexString(Arrays.copyOf(output, updateBytes + finalBytes));
      } catch (Exception ex) {
          throw new RuntimeException(ex);
      }   
  }
  </code></pre>
  
  <p>On localhost I use:</p>
  
  <pre><code>openjdk version "1.8.0_121"
  OpenJDK Runtime Environment (build 1.8.0_121-8u121-b13-0ubuntu1.16.04.2-b13)
  OpenSSL 1.0.2k  26 Jan 2017
  Server version: Apache Tomcat/8.5.11
  Server built:   Jan 10 2017 21:02:52 UTC
  Server number:  8.5.11.0
  OS Name:        Linux
  OS Version:     4.4.0-67-generic
  Architecture:   amd64
  JVM Version:    1.8.0_121-8u121-b13-0ubuntu1.16.04.2-b13
  JVM Vendor:     Oracle Corporation
  </code></pre>
  
  <p>The server has this:</p>
  
  <pre><code>openjdk version "1.8.0_121"
  OpenJDK Runtime Environment (build 1.8.0_121-8u121-b13-1~bpo8+1-b13)
  OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)
  OpenSSL 1.0.1t  3 May 2016
  Using CATALINA_BASE:   /usr/share/tomcat8
  Using CATALINA_HOME:   /usr/share/tomcat8
  Using CATALINA_TMPDIR: /usr/share/tomcat8/temp
  Using JRE_HOME:        /usr/lib/jvm/default-java
  Using CLASSPATH:       /usr/share/tomcat8/bin/bootstrap.jar:/usr/share/tomcat8/bin/tomcat-juli.jar
  Server version: Apache Tomcat/8.0.14 (Debian)
  Server built:   Feb 18 2017 05:56:46
  Server number:  8.0.14.0
  OS Name:        Linux
  OS Version:     3.16.0-4-amd64
  Architecture:   amd64
  JVM Version:    1.8.0_121-8u121-b13-1~bpo8+1-b13
  JVM Vendor:     Oracle Corporation
  </code></pre>
  
  <p>Have anyone had that problem, or maybe any ideas what to check?</p>