title: Convert ColdFusion Encryption using AES/Hex to C#
body: <p>Related to the topic in this post:
  <a href="https://stackoverflow.com/questions/27848994/converting-coldfusion-encryption-code-to-c-sharp">Converting Coldfusion encryption code to C#</a></p>
  
  <p>How would you do the conversion for this:</p>
  
  <pre><code>&lt;!--- valueToEncrypt = "34245678", key = "TJhKuhjyx/87giutBNHh9t==" ---&gt;
  &lt;cfset output = Encrypt( valueToEncrypt, key, "AES", "Hex")&gt;
  </code></pre>
  
  <p>Here's what I tried in C#:</p>
  
  <pre><code>byte[] plainText = Encoding.UTF8.GetBytes(TextToEncrypt);
  byte[] key = Convert.FromBase64String(encryptionKey);
  RijndaelManaged algorithm = new RijndaelManaged();
  algorithm.Mode = CipherMode.ECB;
  algorithm.Padding = PaddingMode.PKCS7;
  algorithm.BlockSize = 128;
  algorithm.KeySize = 128;
  algorithm.Key = key;
  string result;
  using (ICryptoTransform encryptor = algorithm.CreateEncryptor())
  {
      using (MemoryStream memoryStream = new MemoryStream())
      {
          using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
          {
              cryptoStream.Write(plainText, 0, plainText.Length);
              cryptoStream.FlushFinalBlock();
              result = Convert.ToBase64String(memoryStream.ToArray());
          }
      }
  }
  
  return result;
  </code></pre>
  
  <p><strong>ColdFusion Result:</strong></p>
  
  <pre><code>04197FAA3C9C030660A6377E44F77C4E
  </code></pre>
  
  <p><strong>C# Result:</strong></p>
  
  <pre><code>BBl/qjycAwZgpjd+RPd8Tg==
  </code></pre>