title: Encrypt string in java, decrypt in node.js, error: bad decrypt
body: <p>I'm trying to encrypt a string in java, send it to my node.js server, and decrypt it.  However, when I try to do this I get keep getting errors when trying to decrypt.</p>
  
  <p>Java encrypt:</p>
  
  <pre><code>String privateKey = "someprivatekey";
  String data = "dataToEncrypt";
  
  DESKeySpec keySpec = new DESKeySpec(privateKey.getBytes("UTF-8"));
  SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
  SecretKey key = keyFactory.generateSecret(keySpec);
  
  byte[] dataToBytes = data.getBytes("UTF-8");      
  
  Cipher cipher = Cipher.getInstance("DES"); 
  cipher.init(Cipher.ENCRYPT_MODE, key);
  
  // send this string to server 
  String encryptedStr = Base64.encodeToString(cipher.doFinal(dataToBytes), 0);
  </code></pre>
  
  <p>node.js decrypt:</p>
  
  <pre><code>var privateKey = 'someprivatekey';
  var decipher = crypto.createDecipher('des', privateKey);
  var dec = decipher.update(textToDecipher, 'base64', 'utf8'); 
  dec += decipher.final('utf8'); 
  console.log('deciphered: ' + dec);
  </code></pre>
  
  <p>However, I get this error on the node.js side on the decipher.final() line:</p>
  
  <pre><code>TypeError: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
  </code></pre>
  
  <p>I've tried decoding the string and using a buffer on the node side, but it didn't seem to work either, I get the same bad decrypt error.  </p>
  
  <pre><code>var buffer = new Buffer(textToDecipher, 'base64');
  var decipher = crypto.createDecipher('des', privateKey);
  var dec = Buffer.concat([decipher.update(buffer), decipher.final()]);
  </code></pre>
  
  <p>Any ideas what I could be doing wrong?  </p>