title: How to decrypt text using node.js that has been encrypted with java spring security TextEncryptor
body: <p>We have a java spring backend that encrypts some columns using:</p>
  
  <pre><code>Encryptors.queryableText(secretKey, new String(Hex.encode(salt.getBytes(Charsets.UTF_8))));
  </code></pre>
  
  <p>This creates an object of type TextEncryptor in a specific configuration which then can encrypt with <code>textEncryptor.encrypt(msg)</code> and decrypt with <code>textEncryptor.decrypt(msg)</code>.
  This method of encryption provided by spring security is used to make encrypted columns queryable. The same text will always lead to the same encrypted String.</p>
  
  <p>Another node.js backends now needs to access some of the encrypted columns for generating aggregated reports.</p>
  
  <p>I found some article that seems to address that problem quite well:
  <a href="https://stackanswers.net/questions/spring-4-encryptors-vs-cryptojs" rel="nofollow noreferrer">https://stackanswers.net/questions/spring-4-encryptors-vs-cryptojs</a></p>
  
  <p>I changed that code to fit my needs. IV and salt had to be fix like specified by <code>Encryptors.queryableText()</code>:</p>
  
  <pre><code>/*! 
  * Author: flohall
  * date: 2019-11-05
  * file: module/textEncryptor.js
  */
  var CryptoJS = require("crypto-js");
  var config = require('../config.json');
  //keySize and iteration like specified in spring for AesBytesEncryptor
  const keySize = 256;
  const iterations = 1024;
  //see config.json
  const salt = CryptoJS.enc.Hex.parse(config.textEncryptor.hexEncodedSalt);
  const secretKey = config.textEncryptor.secretKey;
  const key = CryptoJS.PBKDF2(secretKey, salt, {
      keySize: keySize / 32,
      iterations: iterations
  });
  
  //same as NULL_IV_GENERATOR of AesBytesEncryptor - so encryption creates always same cipher text for same input
  const iv = {words: [0, 0, 0, 0, 0, 0, 0, 0], sigBytes: 0}
  const cfg = {
      iv: iv,
      padding: CryptoJS.pad.Pkcs7,
      mode: CryptoJS.mode.CBC
  }
  
  exports.encrypt = function (msg) {
  
      const encrypted = CryptoJS.AES.encrypt(msg, key, cfg);
      return encrypted.ciphertext.toString();
  }
  
  exports.decrypt = function (encryptedMessage) {
  
      var decrypted = CryptoJS.AES.decrypt(encryptedMessage, key, cfg)
      return decrypted.toString(CryptoJS.enc.Utf8);
  
  }
  </code></pre>
  
  <p>So far so good - important to know is that the config.textEncryptor.hexEncodedSalt given to the node.js function is the already hexencoded salt.
  I generated it using: </p>
  
  <pre><code>//salt is read from application.yml and not hexencoded yet
  System.out.println(new String(Hex.encode(salt.getBytes(Charsets.UTF_8))));
  </code></pre>
  
  <p>The node.js <code>textEncryptor.encrypt(msg)</code> method does generate the same encrypted message as <code>textEncryptor.encrypt(msg)</code> in java, but still <code>textEncryptor.decrypt(msg)</code> in node.js isn't able to decrypt in the same way as <code>textEncryptor.decrypt(msg)</code> in java does.</p>
  
  <p>This test code in node.js does not work:</p>
  
  <pre><code>var textEncryptor = require('./modules/textEncryptor.js');
  
  var encrypted = textEncryptor.encrypt("helloWorld")
  var decrypted = textEncryptor.decrypt(encrypted)
  console.log(encrypted);
  console.log(decrypted);
  </code></pre>
  
  <p>It prints out just the decrypted text followed by an empty line.<br>
  Any idea - what I am missing?</p>