title: Using CryptoJS to encrypt in Javascript and decrypt in Java
body: <p>I am trying to encrypt something in JavaScript using the google's <a href="https://code.google.com/p/crypto-js/#AES" rel="noreferrer">https://code.google.com/p/crypto-js/#AES</a> exactly as it is if the example. The problem is that where I am trying to decrypt it in Java the result is way different. The only difference that I can see is the padding (CryptoJs uses Pkcs7 whereas java uses Pkcs5) after reading a bit I realized that Pkcs7 and Pkcs5 are basically the same.</p>
  
  <p>Here is a sample of the code that i do the encryption in Javascript:</p>
  
  <pre class="lang-js prettyprint-override"><code>var crypto = require('./aes.js');
  var login = 'ABCD';
  var key = crypto.CryptoJS.enc.Hex.parse('0123456789012345');
  var ive  = crypto.CryptoJS.enc.Hex.parse('0123456789012345');
  
  var encrypted = crypto.CryptoJS.AES.encrypt(login, key, {iv: ive});
  console.log('encrypted msg = ' + encrypted)
  </code></pre>
  
  <p>This is the code that I am using in Java to decrypt it:</p>
  
  <pre class="lang-java prettyprint-override"><code>public String decrypt(byte[] cipherText) throws Exception {
  
      String psk = "0123456789012345";
      String iv = "0123456789012345";
      try {
          String encryptionKey = psk;
          final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "SunJCE");
          final SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes(UTF8), "AES");
          cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv.getBytes(UTF8)));
          return new String(cipher.doFinal(cipherText), UTF8);
      } catch (BadPaddingException | IllegalBlockSizeException | UnsupportedEncodingException | InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | NoSuchProviderException ex) {
          LOG.log(Level.SEVERE, ex.getMessage(), ex);
          throw new Exception(ex.getMessage());
      }
  }
  </code></pre>
  
  <p>Do you have any idea why it fails that bad? Is it a different algorithm or do I fail somewhere else? </p>