title: How can I get the same result from DecryptStringFromBytes_Aes that I have in .NET C # but in Node js?
body: <p>I need to get in Node js the same decrypted value that I get from my website in .NET C #</p>
  <p>My code in the .NET C # class called Crypto is:</p>
  <pre><code>private static readonly string key = &quot;Z8,omB0pxZwñ3h9s&quot;;
  
      public string Encrypt(string data)
      {
          string encData = null;
          byte[][] keys = GetHashKeys(key);
  
          try
          {
              encData = EncryptStringToBytes_Aes(data, keys[0], keys[1]);
              return encData;
          }
          catch (CryptographicException) {
              return null;
          }
          catch (ArgumentNullException) {
              return null;
          }
      }
  
      public string Decrypt(string data)
      {
          string decData = null;
          byte[][] keys = GetHashKeys(key);
  
          try
          {
              decData = DecryptStringFromBytes_Aes(data, keys[0], keys[1]);
          }
          catch (CryptographicException) { }
          catch (ArgumentNullException) { }
  
          return decData;
      }
  
      private byte[][] GetHashKeys(string key)
      {
          byte[][] result = new byte[2][];
          Encoding enc = Encoding.UTF8;
  
          SHA256 sha2 = new SHA256CryptoServiceProvider();
  
          byte[] rawKey = enc.GetBytes(key);
          byte[] rawIV = enc.GetBytes(key);
  
          byte[] hashKey = sha2.ComputeHash(rawKey);
          byte[] hashIV = sha2.ComputeHash(rawIV);
  
          Array.Resize(ref hashIV, 16);
  
          result[0] = hashKey;
          result[1] = hashIV;
  
          return result;
      }
  
      //source: https://msdn.microsoft.com/de-de/library/system.security.cryptography.aes(v=vs.110).aspx
      private static string EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV)
      {
          if (plainText == null || plainText.Length &lt;= 0)
              throw new ArgumentNullException(&quot;plainText&quot;);
          if (Key == null || Key.Length &lt;= 0)
              throw new ArgumentNullException(&quot;Key&quot;);
          if (IV == null || IV.Length &lt;= 0)
              throw new ArgumentNullException(&quot;IV&quot;);
  
          byte[] encrypted;
  
          using (AesManaged aesAlg = new AesManaged())
          {
              aesAlg.Key = Key;
              aesAlg.IV = IV;
  
              ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
  
              using (MemoryStream msEncrypt = new MemoryStream())
              {
                  using (CryptoStream csEncrypt =
                          new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                  {
                      using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                      {
                          swEncrypt.Write(plainText);
                      }
                      encrypted = msEncrypt.ToArray();
                  }
              }
          }
          return Convert.ToBase64String(encrypted);
      }
  
      //source: https://msdn.microsoft.com/de-de/library/system.security.cryptography.aes(v=vs.110).aspx
      private static string DecryptStringFromBytes_Aes(string cipherTextString, byte[] Key, byte[] IV)
      {
          byte[] cipherText = Convert.FromBase64String(cipherTextString);
  
          if (cipherText == null || cipherText.Length &lt;= 0)
              throw new ArgumentNullException(&quot;cipherText&quot;);
          if (Key == null || Key.Length &lt;= 0)
              throw new ArgumentNullException(&quot;Key&quot;);
          if (IV == null || IV.Length &lt;= 0)
              throw new ArgumentNullException(&quot;IV&quot;);
  
          string plaintext = null;
  
          using (Aes aesAlg = Aes.Create())
          {
              aesAlg.Key = Key;
              aesAlg.IV = IV;
  
              ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
  
              using (MemoryStream msDecrypt = new MemoryStream(cipherText))
              {
                  using (CryptoStream csDecrypt =
                          new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                  {
                      using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                      {
                          plaintext = srDecrypt.ReadToEnd();
                      }
                  }
              }
          }
          return plaintext;
      }
  </code></pre>
  <p>And what I have in Node js for now is this:</p>
  <pre><code>var sys = require ('sys'),
  url = require('url'),
  http = require('http'),
  qs = require('querystring');
  crypto = require('crypto-js');
  var express = require('express');
  var app = express();
  var sql = require('mssql');
  const getConn = require('./configVariables');
  
  var config={
      server : getConn.getServer(),
      database : getConn.getDatabase(),
      user : getConn.getUser(),
      password : getConn.getPass(),
      port:getConn.getPort(),
      options: {
          encrypt: false
      }
  };
  
  http.createServer(function (req, res) {
      try{
          res.writeHead(200, {&quot;Content-Type&quot;: &quot;text/html;charset=UTF-8&quot;});
  
          var key = &quot;Z8,omB0pxZwñ3h9s&quot;;
          var secret = &quot;ZZZZZZ&quot;;
          e1 = crypto.AES.encrypt(secret, key, {mode: crypto.mode.CBC, padding: crypto.pad.Pkcs7});
          console.log(&quot;key: &quot; + crypto.enc.Base64.stringify(e1.key));
          console.log(&quot;iv: &quot; + crypto.enc.Base64.stringify(e1.iv));
          console.log(&quot;salt: &quot; + crypto.enc.Base64.stringify(e1.salt));
          console.log(&quot;ciphertext: &quot; + crypto.enc.Base64.stringify(e1.ciphertext));
          p = crypto.AES.decrypt(e1, key, {mode: crypto.mode.CBC, padding: crypto.pad.Pkcs7});
          console.log(&quot;decrypted: &quot; + crypto.enc.Utf8.stringify(p));        
          var isResponse = JSON.stringify({StatusCode:&quot;200&quot;, info:&quot;true&quot;, values:p})
          res.end(isResponse);
      }catch(err){
          var errorResponse = JSON.stringify({StatusCode:&quot;400&quot;, Descripcion:&quot;ERROR: &quot; + err})
          res.end(errorResponse);
      }
  }).listen(process.env.PORT);
  </code></pre>
  <p>If I encrypt in .NET the variable ZZZZZZ gives me the result I need.
  But in Node js while the result may be a bit similar, it is not the same as I get in .NET and I really need to have the exact same result for both encryption and decryption.</p>
  <p>Please I need help. I do not manage at all in what is encryption</p>