title: AES Encryption with C# Decryption with crypto-js
body: <p>I'm triying to Encrypt string with C# and decrypt it using Angular crypto-js library but it's giving me different output.
  I tried different c# aes encryption implementations but crypto-js library can't decrypt the encrypted data in c#. Thank you for any help.</p>
  <p>Here is my code</p>
  <p>Program.cs</p>
  <pre><code> static void Main()
      {
          var r = EncryptString(&quot;exampleString&quot;, &quot;examplePassword&quot;);
          Console.Write(r);
      }
  
   public static string EncryptString(string plainText, string passPhrase)
      {
          if (string.IsNullOrEmpty(plainText))
          {
              return &quot;&quot;;
          }
          // generate salt
          byte[] key, iv;
          var salt = new byte[8];
          var rng = new RNGCryptoServiceProvider();
          rng.GetNonZeroBytes(salt);
          DeriveKeyAndIv(passPhrase, salt, out key, out iv);
          // encrypt bytes
          var encryptedBytes = EncryptStringToBytesAes(plainText, key, iv);
          // add salt as first 8 bytes
          var encryptedBytesWithSalt = new byte[salt.Length + encryptedBytes.Length + 8];
          Buffer.BlockCopy(Encoding.ASCII.GetBytes(&quot;Salted__&quot;), 0, encryptedBytesWithSalt, 0, 8);
          Buffer.BlockCopy(salt, 0, encryptedBytesWithSalt, 8, salt.Length);
          Buffer.BlockCopy(encryptedBytes, 0, encryptedBytesWithSalt, salt.Length + 8, encryptedBytes.Length);
          // base64 encode
          return Convert.ToBase64String(encryptedBytesWithSalt);
      }
      private static void DeriveKeyAndIv(string passPhrase, byte[] salt, out byte[] key, out byte[] iv)
      {
          // generate key and iv
          var concatenatedHashes = new List&lt;byte&gt;(48);
          var password = Encoding.UTF8.GetBytes(passPhrase);
          var currentHash = new byte[0];
          var md5 = MD5.Create();
          bool enoughBytesForKey = false;
          // See http://www.openssl.org/docs/crypto/EVP_BytesToKey.html#KEY_DERIVATION_ALGORITHM
          while (!enoughBytesForKey)
          {
              var preHashLength = currentHash.Length + password.Length + salt.Length;
              var preHash = new byte[preHashLength];
              Buffer.BlockCopy(currentHash, 0, preHash, 0, currentHash.Length);
              Buffer.BlockCopy(password, 0, preHash, currentHash.Length, password.Length);
              Buffer.BlockCopy(salt, 0, preHash, currentHash.Length + password.Length, salt.Length);
              currentHash = md5.ComputeHash(preHash);
              concatenatedHashes.AddRange(currentHash);
              if (concatenatedHashes.Count &gt;= 48)
                  enoughBytesForKey = true;
          }
          key = new byte[32];
          iv = new byte[16];
          concatenatedHashes.CopyTo(0, key, 0, 32);
          concatenatedHashes.CopyTo(32, iv, 0, 16);
          md5.Clear();
      }
  
      static byte[] EncryptStringToBytesAes(string plainText, byte[] key, byte[] iv)
      {
          // Check arguments.
          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;);
          // Declare the stream used to encrypt to an in memory
          // array of bytes.
          MemoryStream msEncrypt;
          // Declare the RijndaelManaged object
          // used to encrypt the data.
          RijndaelManaged aesAlg = null;
          try
          {
              // Create a RijndaelManaged object
              // with the specified key and IV.
              aesAlg = new RijndaelManaged { Mode = CipherMode.CBC, KeySize = 256, BlockSize = 128, Key = key, IV = iv };
              // Create an encryptor to perform the stream transform.
              ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
              // Create the streams used for encryption.
              msEncrypt = new MemoryStream();
              using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
              {
                  using (var swEncrypt = new StreamWriter(csEncrypt))
                  {
                      //Write all data to the stream.
                      swEncrypt.Write(plainText);
                      swEncrypt.Flush();
                      swEncrypt.Close();
                  }
              }
          }
          finally
          {
              // Clear the RijndaelManaged object.
              aesAlg?.Clear();
          }
          // Return the encrypted bytes from the memory stream.
          return msEncrypt.ToArray();
      }
  </code></pre>
  <p>Simply decrypting it using crypto-js</p>
  <pre><code> let CryptoJS = require('crypto-js');
  let r = CryptoJS.AES.decrypt('exampleString', 'examplePassword').toString();
  </code></pre>