title: Trying to get AES encryption of a string in node.js to match encrypted value in .net
body: <p>I'm trying to encrypt a value in node.js that I can decrypt in .net. I've been given the code that they use on the .net side of things for encrypting a value and i'm trying to achieve the same encrypted value in my node.js script.</p>
  
  <p>I'm definitely not an encryption buff so please help me figure out where i'm going wrong. My node.js encrypted value is not matching that of the .net encrypted value, and my node.js encrypted value is actually not returning the same value every time I run the script either.</p>
  
  <p>Here's the .net encryption logic:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Security.Cryptography;
  using System.IO;
  
  namespace ConsoleApplication1
  {
      class Program
      {
          static void Main(string[] args)
          {
              Console.WriteLine("start:");
              string key = "mysecretkey";
              string secret = "encryptThisMessage";
  
              string crypto = EncryptString(secret, key);
              Console.WriteLine(crypto);
  
              string returnValue = DecryptString(crypto, key);
              Console.WriteLine(returnValue);
              Console.ReadKey();
  
          }
  
          /// &lt;summary&gt;
          /// Encrpyts the sourceString, returns this result as an Aes encrpyted, BASE64 encoded string
          /// &lt;/summary&gt;
          /// &lt;param name="plainSourceStringToEncrypt"&gt;a plain, Framework string (ASCII, null terminated)&lt;/param&gt;
          /// &lt;param name="passPhrase"&gt;The pass phrase.&lt;/param&gt;
          /// &lt;returns&gt;
          /// returns an Aes encrypted, BASE64 encoded string
          /// &lt;/returns&gt;
          public static string EncryptString(string plainSourceStringToEncrypt, string passPhrase)
          {
              //Set up the encryption objects
              using (AesCryptoServiceProvider acsp = GetProvider(Encoding.Default.GetBytes(passPhrase)))
              {
                  byte[] sourceBytes = Encoding.ASCII.GetBytes(plainSourceStringToEncrypt);
                  ICryptoTransform ictE = acsp.CreateEncryptor();
  
                  //Set up stream to contain the encryption
                  MemoryStream msS = new MemoryStream();
  
                  //Perform the encrpytion, storing output into the stream
                  CryptoStream csS = new CryptoStream(msS, ictE, CryptoStreamMode.Write);
                  csS.Write(sourceBytes, 0, sourceBytes.Length);
                  csS.FlushFinalBlock();
  
                  //sourceBytes are now encrypted as an array of secure bytes
                  byte[] encryptedBytes = msS.ToArray(); //.ToArray() is important, don't mess with the buffer
  
                  //return the encrypted bytes as a BASE64 encoded string
                  return Convert.ToBase64String(encryptedBytes);
              }
          }
  
  
          /// &lt;summary&gt;
          /// Decrypts a BASE64 encoded string of encrypted data, returns a plain string
          /// &lt;/summary&gt;
          /// &lt;param name="base64StringToDecrypt"&gt;an Aes encrypted AND base64 encoded string&lt;/param&gt;
          /// &lt;param name="passphrase"&gt;The passphrase.&lt;/param&gt;
          /// &lt;returns&gt;returns a plain string&lt;/returns&gt;
          public static string DecryptString(string base64StringToDecrypt, string passphrase)
          {
              //Set up the encryption objects
              using (AesCryptoServiceProvider acsp = GetProvider(Encoding.Default.GetBytes(passphrase)))
              {
                  byte[] RawBytes = Convert.FromBase64String(base64StringToDecrypt);
                  ICryptoTransform ictD = acsp.CreateDecryptor();
  
                  //RawBytes now contains original byte array, still in Encrypted state
  
                  //Decrypt into stream
                  MemoryStream msD = new MemoryStream(RawBytes, 0, RawBytes.Length);
                  CryptoStream csD = new CryptoStream(msD, ictD, CryptoStreamMode.Read);
                  //csD now contains original byte array, fully decrypted
  
                  //return the content of msD as a regular string
                  return (new StreamReader(csD)).ReadToEnd();
              }
          }
  
          private static AesCryptoServiceProvider GetProvider(byte[] key)
          {
              AesCryptoServiceProvider result = new AesCryptoServiceProvider();
              result.BlockSize = 128;
              result.KeySize = 128;
              result.Mode = CipherMode.CBC;
              result.Padding = PaddingMode.PKCS7;
  
              result.GenerateIV();
              result.IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  
              byte[] RealKey = GetKey(key, result);
              result.Key = RealKey;
              // result.IV = RealKey;
              return result;
          }
  
          private static byte[] GetKey(byte[] suggestedKey, SymmetricAlgorithm p)
          {
              byte[] kRaw = suggestedKey;
              List&lt;byte&gt; kList = new List&lt;byte&gt;();
  
              for (int i = 0; i &lt; p.LegalKeySizes[0].MinSize; i += 8)
              {
                  kList.Add(kRaw[(i / 8) % kRaw.Length]);
              }
              byte[] k = kList.ToArray();
              return k;
          }
      }
  }
  </code></pre>
  
  <p>My node.js script:</p>
  
  <pre><code>var crypto = require('crypto-js');
  var key = "mysecretkey";
  var secret = "encryptThisMessage";
  
  e1 = crypto.AES.encrypt(secret, key, {mode: crypto.mode.CBC, padding: crypto.pad.Pkcs7});
  e2 = crypto.AES.encrypt(secret, key, {mode: crypto.mode.CBC, padding: crypto.pad.Pkcs7});
  
  console.log('e1');
  console.log(crypto.enc.Hex.stringify(e1));
  console.log(e1.toString());
  console.log(e1.salt.toString());
  console.log(e1.iv.toString());
  console.log(e1.ciphertext.toString());
  console.log(e1.ciphertext.toString(crypto.enc.Base64));
  console.log('e2');
  console.log(e2.toString());
  console.log(e2.salt.toString());
  console.log(e2.iv.toString());
  console.log(e2.ciphertext.toString(crypto.enc.Base64));
  </code></pre>
  
  <p>When running the encryption piece in the c# code, the value looks like this: (slightly modified for security purposes) <code>dp+8cjr/ajEw5oePdiG+4g==</code>. <strong>How can I change my node.js code to output this matched encrypted value?</strong></p>
  
  <p>Output of node.js script:</p>
  
  <p><img src="https://i.stack.imgur.com/c5ePR.jpg" alt="enter image description here"></p>