title: Java to JS and JS to Java encryption using cryptojs
body: <p>I got on this post a couple of weeks ago and worked perfectly:
  <a href="https://stackoverflow.com/questions/23321049/compatible-aes-algorithm-for-java-and-javascript">Compatible AES algorithm for Java and Javascript</a></p>
  
  <p>Now, I need to do the reverse operation, but once in java, I am getting this exception:</p>
  
  <pre><code>javax.crypto.BadPaddingException: Given final block not properly padded
  at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:966)
  at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:824)
  at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:436)
  at javax.crypto.Cipher.doFinal(Cipher.java:2165)
  </code></pre>
  
  <p>This is my "reverse" operation I did in JavaScript:</p>
  
  <pre><code>var rkEncryptionKey = CryptoJS.enc.Base64.parse('u/Gu5posvwDsXUnV5Zaq4g==');
  var rkEncryptionIv = CryptoJS.enc.Base64.parse('5D9r9ZVzEYYgha93/aUK2w==');
  
  function encryptString(stringToEncrypt) {
      var utf8Stringified = CryptoJS.enc.Utf8.parse(stringToEncrypt);
      var encrypted = CryptoJS.AES.encrypt(utf8Stringified.toString(), rkEncryptionKey, {mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7, iv: rkEncryptionIv});
      return CryptoJS.enc.Base64.parse(encrypted.toString()).toString();
  }
  </code></pre>
  
  <p>I though this would be all?</p>
  
  <p><strong>[EDIT]</strong></p>
  
  <p>Encrypted string is the following: <code>{"company_name":"asdfasdfasd","customer_name":"asdfasdfasdfasdf","phone_number":"asdfasdfasdfasdf","email":"asdfasdfasdfasdfads"}</code></p>
  
  <p>When doing the encrypt / decrypt from java to java it works, when doing so from java to javascript, also works, but from javascript to java, not working.</p>
  
  <p><strong>Java Code</strong></p>
  
  <pre><code>public String toJson(final String encrypted) {
      try {
          SecretKey key = new SecretKeySpec(Base64.decodeBase64("u/Gu5posvwDsXUnV5Zaq4g=="), "AES");
          AlgorithmParameterSpec iv = new IvParameterSpec(Base64.decodeBase64("5D9r9ZVzEYYgha93/aUK2w=="));
          byte[] decodeBase64 = Base64.decodeBase64(encrypted);
  
          Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
          cipher.init(Cipher.DECRYPT_MODE, key, iv);
  
          return new String(cipher.doFinal(decodeBase64), "UTF-8");
      } catch (Exception e) {
          throw new RuntimeException("This should not happen in production.", e);
      }
  }
  </code></pre>