title: DES Encryption in Node.js
body: <p>I've been trying to port this Java Program to Node.js. I've tried a few ways and still unable to get this done so far. Could be due to my limited knowledge in Cryptography.</p>
  
  <p>The Node.js Program I've written is this.</p>
  
  <pre><code>var crypto = require('crypto');
  
  var iv = crypto.randomBytes(8);
  
  var encrypt = function(data, key) {
      var decodeKey = crypto.createHash('sha512').update(key, 'utf-8').digest().slice(0, 24);
      var cipher = crypto.createCipheriv('des-ede-cbc', decodeKey, iv);
      return cipher.update(data, 'utf8', 'hex') + cipher.final('hex');
  };
  
  var data = 'abc101'
  var key = '1785851816255252-8887772';
  
  var cipher = encrypt(data, key);
  console.log(cipher);
  </code></pre>
  
  <p>I keep receiving the following error:</p>
  
  <pre><code>Error: Invalid key length
  </code></pre>
  
  <p>The original program I am trying to port is this:</p>
  
  <pre><code>import java.security.MessageDigest;
  import java.security.spec.KeySpec;
  import java.util.Arrays;
  
  import javax.crypto.Cipher;
  import javax.crypto.SecretKey;
  import javax.crypto.SecretKeyFactory;
  import javax.crypto.spec.DESedeKeySpec;
  import javax.crypto.spec.IvParameterSpec;
  import javax.crypto.spec.SecretKeySpec;
  
  import org.apache.commons.codec.binary.Base64;
  
  public class PasswordEncryptor {
  
      public static void main(String[] args) {
  
          String passwd = "abc101";
          String msgId = "1785851816255252-8887772";
  
          String result = "";
  
          try {
              if (passwd == null &amp;&amp; passwd.equals("") &amp;&amp; msgId == null &amp;&amp; msgId.equals("")) {
                  System.out.println("Wrong Inputs");
              } else {
                  result = PasswordEncryptor.encryptPassword(msgId, passwd);
              }
              System.out.println(result);
          } catch (Exception e) {
              System.out.println("Exception in PasswordEncryption  ::: " + e);
          }
  
      }
  
      public static String encryptPassword(String msgId, String password) {
          String finalValue = "";
          try {
              MessageDigest mDigest = MessageDigest.getInstance("SHA-512");
              byte[] digestSeed = mDigest.digest(msgId.getBytes());
              byte[] seedEncArray = Arrays.copyOf(digestSeed, 24);
              Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
              SecretKeySpec skspec = new SecretKeySpec(seedEncArray, "DESede");
              IvParameterSpec iv = new IvParameterSpec(new byte[8]);
              cipher.init(Cipher.ENCRYPT_MODE, skspec, iv);
              byte[] finalByteArray = cipher.doFinal(password.getBytes());
              finalValue = new String(Base64.encodeBase64(finalByteArray));
          } catch (Exception e) {
              System.out.println("Exception in encryptPassword ::::" + e);
          }
          return finalValue;
      }
  }
  </code></pre>
  
  <p>Any help is appreciated. Thank you.</p>