title: Nodejs Encrypted string not matching java : AES-256-CBC
body: <p>Hi I have written nodejs encryption and java encryption by using same algorithm on both the side. But Java and NodeJS are returning different encrypted string. Please help me here.</p>
  
  <p>//Here is my Java Code        </p>
  
  <pre><code>import java.io.UnsupportedEncodingException;
  import java.security.Key;
  import java.security.MessageDigest;
  import java.security.NoSuchAlgorithmException;
  import java.security.spec.AlgorithmParameterSpec;
  
      import javax.crypto.Cipher;
      import javax.crypto.spec.IvParameterSpec;
      import javax.crypto.spec.SecretKeySpec;
      import java.util.Base64;
  
      public enum AESUtil {
          ;
          private static final String ENCRYPTION_KEY = "RwcmlVpg";
          private static final String ENCRYPTION_IV = "4e5Wa71fYoT7MFEX";
  
          public static String encrypt(String src) {
              try {
                  Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
                  cipher.init(Cipher.ENCRYPT_MODE, makeKey(), makeIv());
                  Base64.Encoder encoder = Base64.getEncoder();
                  return encoder.encodeToString(cipher.doFinal(src.getBytes()));
              } catch (Exception e) {
                  throw new RuntimeException(e);
              }
          }
  
          public static String decrypt(String src) {
              String decrypted = "";
              try {
                  Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
                  cipher.init(Cipher.DECRYPT_MODE, makeKey(), makeIv());
                  Base64.Decoder decoder = Base64.getDecoder();
                  decrypted = new String(cipher.doFinal(decoder.decode(src)));
              } catch (Exception e) {
                  throw new RuntimeException(e);
              }
              return decrypted;
          }
  
          static AlgorithmParameterSpec makeIv() {
              try {
                  return new IvParameterSpec(ENCRYPTION_IV.getBytes("UTF-8"));
              } catch (UnsupportedEncodingException e) {
                  e.printStackTrace();
              }
              return null;
          }
  
          static Key makeKey() {
              try {
                  MessageDigest md = MessageDigest.getInstance("SHA-256");
                  byte[] key = md.digest(ENCRYPTION_KEY.getBytes("UTF-8"));
                  return new SecretKeySpec(key, "AES");
              } catch (NoSuchAlgorithmException e) {
                  e.printStackTrace();
              } catch (UnsupportedEncodingException e) {
                  e.printStackTrace();
              }
  
              return null;
          }
      }
  </code></pre>
  
  <p>//Below is the code to test above code </p>
  
  <pre><code>    public class AESMain {
  
          /**
           * @param args
           */
          public static void main(String[] args) {
              String src = "Hello,CryptWorld";
              String encrypted = AESUtil.encrypt(src);
              String decrypted = AESUtil.decrypt(encrypted);
              System.out.println("src: " + src);
              System.out.println("encrypted: " + encrypted);
              System.out.println("decrypted: " + decrypted);
          }
  
      }
  </code></pre>
  
  <p>Response from the above code is </p>
  
  
  
  <pre class="lang-none prettyprint-override"><code>src: Hello,CryptWorld
  encrypted: rh7ro9NH1XZeLX95paLETDgYxRbnDoOIrxarO0Sy73s=
  decrypted: Hello,CryptWorld
  </code></pre>
  
  <p>//Node JS Code</p>
  
  <pre class="lang-js prettyprint-override"><code>var Encrypt, crypto;
  
  crypto = require("crypto");
  
  
  Encrypt = module.exports = (function() {
    var b64dec, b64enc, cipher, decrypt, encrypt, iv, key;
    key = crypto.createHash("sha256").update("RwcmlVpg").digest();
    iv = '4e5Wa71fYoT7MFEX';
    cipher = function(mode, data) {
      var encipher, encoded;
      encipher = crypto[mode]("aes-256-cbc", key, iv);
      encoded = encipher.update(data);
      encoded += encipher.final();
      return encoded;
    };
    encrypt = function(data) {
      return b64enc(cipher("createCipheriv", data));
    };
    decrypt = function(data) {
      return cipher("createDecipheriv", b64dec(data));
    };
    b64enc = function(data) {
      var b;
      b = new Buffer(data, "binary");
      return b.toString("base64");
    };
    b64dec = function(data) {
      var b;
      b = new Buffer(data, "base64");
      return b.toString("binary");
    };
    return {
      encrypt: encrypt,
      decrypt: decrypt
    };
  })();
  var expected = Encrypt.encrypt("Hello,CryptWorld");
  console.log("expected " + expected);
  </code></pre>
  
  <p>The Response from Node JS is
  </p>
  
  <pre class="lang-none prettyprint-override"><code>expected /R79/f1H/XZeLX95/f39TDgY/Rb9Dv39/Rb9O0T9/Xs=
  </code></pre>
  
  <p>The node js version is v6.10.1 and JDK version 1.8.0_77.</p>
  
  <p>I really don't know what I am missing.</p>