title: AES encryption in java vs cryptojs
body: <p>I Have an AES encryption and decrypt both in java and nodejs (cryptojs).</p>
  
  <p>but when I tried to encrypt and decrypt in both have different result.</p>
  
  <p>This is my java code : </p>
  
  <pre><code>import java.*;
  import javax.crypto.spec.IvParameterSpec;
  import javax.crypto.spec.SecretKeySpec;
  import javax.crypto.Cipher;
  import org.apache.commons.codec.binary.Base64;
  
  public class enc {
  
      public static void main(String[] args){
          String key = "1234567890123456"; // 128 bit key: must 16 character
          String initVector = "1234567890123456"; // 16 bytes IV : must 16 character
  
          System.out.println("Key : "+key);
          System.out.println("Init Vector : "+initVector);
  
          String encrypted = encryptAES(key, initVector, "Hello World");
          System.out.println("encrypted : "+encrypted);
  
      }
  
      public static String encryptAES(String key, String initVector, String value) {
  
          try {
  
              IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
  
              SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
  
              Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
  
              cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
  
              byte[] encrypted = cipher.doFinal(value.getBytes());
  
              return Base64.encodeBase64String(encrypted);
  
          } catch (Exception ex) {
  
              ex.printStackTrace();
  
          }
  
          return null;
  
      }
  
      public static String decryptAES(String key, String initVector, String encrypted) {
  
          try {
  
              IvParameterSpec iv = new IvParameterSpec(
  
                      initVector.getBytes("UTF-8"));
  
              SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
  
              Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
  
              cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
  
              byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));
  
              return new String(original);
  
          } catch (Exception ex) {
  
              ex.printStackTrace();
  
          }
  
          return null;
  
      }
  }
  </code></pre>
  
  <p>and java result : </p>
  
  <pre><code>Key : 1234567890123456
  Init Vector : 1234567890123456
  encrypted : ZyODokM33Io1ZKIA8h7owA==
  decrypted : Hello World
  </code></pre>
  
  <p>My Nodejs using crypto js :</p>
  
  <pre><code>var CryptoJS = require("crypto-js");
  var atob = require('atob');
  var btoa = require('btoa');
  
  var message = "Hello World";
  
  var key = "1234567890123456"; 
  var iv  = "1234567890123456"; 
  
  console.log("Key : "+key);
  console.log("Init Vector : "+iv);
  
  key = CryptoJS.enc.Base64.parse(key);
  iv = CryptoJS.enc.Base64.parse(iv);
  
  var chiperData = CryptoJS.AES.encrypt(message, key, { iv: iv });
  console.log('encrypted : ',chiperData.toString());
  
  var data = CryptoJS.AES.decrypt(chiperData, key, { iv: iv });
  console.log('decrypted : ',hex2a(data.toString()));
  
  // Convert hex string to ASCII.
  // Thanks to https://stackoverflow.com/questions/11889329/word-array-to-string
  function hex2a(hex) {
      var str = '';
      for (var i = 0; i &lt; hex.length; i += 2)
          str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
      return str;
  }
  </code></pre>
  
  <p>and the result :</p>
  
  <pre><code>Key : 1234567890123456
  Init Vector : 1234567890123456
  encrypted :  tHtu5jUs0ZotjjJIHgy0eQ==
  decrypted :  Hello World
  </code></pre>
  
  <p>as you can see encrypted in java and cryptojs is different. <code>ZyODokM33Io1ZKIA8h7owA==</code> and <code>tHtu5jUs0ZotjjJIHgy0eQ==</code></p>
  
  <p>Where is my code wrong? and how to solve this case?</p>
  
  <p>so they have same encruption so can decrypt each other encrypted string</p>
  
  <p>thanks</p>