title: AES Encryption/Decryption in Node JS similar to Java
body: <p>I am trying to replicate the <strong>Java</strong> code for <strong>AES Encryption and Decryption</strong> in <strong>Node JS</strong>.</p>
  
  <p><strong>Java Code</strong></p>
  
  <pre><code>    SecretKeySpec skeySpec;
      String key = "a4e1112f45e84f785358bb86ba750f48";
  
      public void encryptString(String key) throws Exception {
          try {
              skeySpec = new SecretKeySpec(key.getBytes(), "AES");
              cipher = Cipher.getInstance("AES");
              cipher.init(1, skeySpec);
              byte encstr[] = cipher.doFinal(message.getBytes());
              String encData = new String(encstr, "UTF-8");
              System.out.println(encData);
          } catch (NoSuchAlgorithmException nsae) {
              throw new Exception("Invalid Java Version");
          } catch (NoSuchPaddingException nse) {
              throw new Exception("Invalid Key");
          }
      }
  </code></pre>
  
  <p><strong>Node JS</strong></p>
  
  <pre><code>    var encryptKey = function (text) {
          var cipher = crypto.createCipher('aes256', 'a4e1112f45e84f785358bb86ba750f48');
          var crypted = cipher.update(text,'utf8', 'hex')
          crypted += cipher.final('hex');
          console.log(crypted);
          return crypted;
      }
  </code></pre>
  
  <p>I am unable to get the exact <strong>cipher-text</strong> in <strong>Node JS</strong>, which i am getting in <strong>Java</strong>.</p>