title: Mcrypt encryption C# equivalent of PHP (Rijndael 128 with 32bit hexKey)
body: <p>I am sorry if this was asked/answered elsewhere but I was unable to find it.</p>
  
  <p>I am currently trying to duplicate the result of PHP mcrypt in C#. PHP being the back-end server. I am using Rijndael 128 in ECB mode with a 256bit (32 char) hexKey. </p>
  
  <p>Now I know that ECB is less secure then CBC and that mcrypt is depreciated but I am not the one who developed the back-end and have no choice to do in this manner (trust me I asked). </p>
  
  <p>Test password: <strong>testPassword</strong></p>
  
  <p>hexKey: <strong>2619eeaed2cb05f60f298b7af8e565e4f155dd76821865994d4e660d7c041281</strong></p>
  
  <p>P.S. the hexKey is not the actualkey we are using ;)</p>
  
  <p>Here is my code in PHP</p>
  
  <pre><code>$key = $this-&gt;hexToStr([key]);
  $text = $password;
  $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_ECB);
  
  private function hexToStr($hex) {
  $string = '';
  for ($i = 0; $i &lt; strlen($hex) - 1; $i+=2) {
      $string .= chr(hexdec($hex[$i] . $hex[$i + 1]));
  }
  return $string;
  }
  </code></pre>
  
  <p>And my code in C#</p>
  
  <pre><code>    public string EncryptStringRijndael(string value)
      {
          try
          {
              string cipherText;
              byte[] plainTextBytes = Encoding.UTF8.GetBytes(value);
  
              var rijndael = new RijndaelManaged
              {
                  Key = Encoding.ASCII.GetBytes(HexStringToString(hexKey)),
                  Mode = CipherMode.ECB,
                  BlockSize = 128,
                  KeySize = 256,
              };
  
              var encryptor = rijndael.CreateEncryptor(rijndael.Key, null);
              MemoryStream memoryStream = new MemoryStream();
              CryptoStream cryptoStream = new CryptoStream
              (
                  memoryStream,
                  encryptor,
                  CryptoStreamMode.Write
              );
              cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
              cryptoStream.FlushFinalBlock();
              byte[] cipherTextBytes = memoryStream.ToArray();
  
              cipherText = Encoding.ASCII.GetString(cipherTextBytes);
              //cipherText = StringToHexString(tempText);
  
              return cipherText;
          }
          catch (Exception ex)
          {
              return ex.Message;
          }
      }
  
      public static string HexStringToString(string hexString)
      {
          try
          {
              string ascii = string.Empty;
  
              for (int i = 0; i &lt; hexString.Length; i += 2)
              {
                  String hs = string.Empty;
  
                  hs = hexString.Substring(i, 2);
                  uint decval = Convert.ToUInt32(hs, 16);
                  char character = Convert.ToChar(decval);
                  ascii += character;
              }
              return ascii;
          }
          catch (Exception ex)
          {
              return ex.Message;
          }
      }
  
      public static string StringToHexString(string originalString)
      {
          string outp = string.Empty;
          char[] value = originalString.ToCharArray();
          foreach (char L in value)
          {
              int V = Convert.ToInt32(L);
              outp += string.Format("{0:x}", V);
          }
          return outp;
      }
  </code></pre>
  
  <p>PHP result: G .�g��/D>�Y�5</p>
  
  <p>C# result: ???u\0??9?`???</p>
  
  <p>What am I doing wrong ?</p>
  
  <p>Thanks for all the help :)</p>