title: How to port encrypt (AES192) method from nodejs to java correctly?
body: <p>I'm trying to port this nodejs code to java</p>
  <pre><code>const crypto = require(&quot;crypto&quot;);
  
  const encrypt = (data, key) =&gt; {
      const cipher = crypto.createCipher('aes192', key)
  
      let crypted = cipher.update(data, 'utf8', 'hex')
      crypted += cipher.final('hex')
  
      return crypted;
  }
  </code></pre>
  <p>I've tried to use this solution:</p>
  <pre><code>import org.springframework.security.crypto.codec.Hex;
  import javax.crypto.Cipher;
  import javax.crypto.spec.SecretKeySpec;
  
  public String encrypt(String data, String key) {
      try {
          var cipher = Cipher.getInstance(&quot;AES&quot;);
          cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes(), &quot;AES&quot;));
  
          var cipherText = cipher.update(data.getBytes());
          cipherText = ArrayUtils.addAll(cipherText, cipher.doFinal());
  
          return new String(Hex.encode(cipherText));
      } catch (Exception e) {
          e.printStackTrace();
      }
      return null;
  }
  </code></pre>
  <p>Cons:</p>
  <ol>
  <li><p><code>encrypt</code> method for Java returns a different value than the method for nodejs (for the same data and key).</p>
  </li>
  <li><p>In nodejs, I can put a short key (5 characters long), at the same time in java I'm catching an exception e.g. &quot;java.security.InvalidKeyException: Invalid AES key length: 5 bytes&quot;</p>
  </li>
  </ol>
  <p>Could you suggest the right solution or point on a mistake in the existing one? Thank you in advance!</p>
  <p>Note: I'm unable to change encrypt/decrypt method in nodejs, so I need to port this to java correctly.</p>