title: Encrypt in node and decrypt in java
body: <p>I have an encrypt-code in Java. I'm trying to port the encrypt part to node. Basically, node will do the encryption using the crypto module, and then Java will do the decryption.</p>
  
  <p>Here's how I do encryption in Java:</p>
  
  <pre><code>protected static String encrypt(String plaintext) {
      final byte[] KEY = {
              0x6d, 0x79, 0x56, 0x65, 0x72, 0x79, 0x54, 0x6f, 0x70,
              0x53, 0x65, 0x63, 0x72, 0x65, 0x74, 0x4b
      };
  
      try {
          Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
          final SecretKeySpec secretKey = new SecretKeySpec(KEY, "AES");
          cipher.init(Cipher.ENCRYPT_MODE, secretKey);
          final String encryptedString = Base64.encodeToString(
              cipher.doFinal(plaintext.getBytes()), Base64.DEFAULT);
  
          return encryptedString;
      } catch (Exception e) {
          return null;
      }
  }
  </code></pre>
  
  <p>Here's how I do encryption in node:</p>
  
  <pre><code>var crypto = require('crypto'),
      key = new Buffer('6d7956657279546f705365637265744b', 'hex'),
      cipher = crypto.createCipher('aes-128-ecb', key),
      chunks = [];
  
  cipher.setAutoPadding(true);
  chunks.push(cipher.update(
      new Buffer(JSON.stringify({someKey: "someValue"}), 'utf8'),
      null, 'base64'));
  chunks.push(cipher.final('base64'));
  
  var encryptedString = chunks.join('');
  </code></pre>
  
  <p>In Java, I get the string <code>T4RlJo5ENV8h1uvmOHzz1KjyXzBoBuqVLSTHsPppljA=</code>. This gets decrypted correctly. However, in node, I get <code>al084hEpTK7gOYGQRSGxF+WWKvNYhT4SC7MukrzHieM=</code> which is obviously different and thus it won't get decrypted correctly.</p>
  
  <p>I tried to look for people who has the same problem as me, and <a href="https://github.com/joyent/node/issues/4744" rel="noreferrer">this</a> github issue is the closest I can find. As suggested in that issue, I tried running openssl like so:</p>
  
  <pre><code>$ echo -e '{"someKey": "someValue"}' | openssl enc -a -e -aes-128-ecb -K "6d7956657279546f705365637265744b"
  T4RlJo5ENV8h1uvmOHzz1MY2bhoFRHZ+ClxsV24l2BU=
  </code></pre>
  
  <p>The result I got was close enough to the one produced by java, but still different:</p>
  
  <pre><code>T4RlJo5ENV8h1uvmOHzz1MY2bhoFRHZ+ClxsV24l2BU=  // openssl
  T4RlJo5ENV8h1uvmOHzz1KjyXzBoBuqVLSTHsPppljA=  // java
  al084hEpTK7gOYGQRSGxF+WWKvNYhT4SC7MukrzHieM=  // node
  </code></pre>
  
  <p>Which brings me to the question, how do I make node output the same encrypted string as my java code? I can only change my code in node, but not in java.</p>